VMware Cloud Community
chiisaryuu
Enthusiast
Enthusiast
Jump to solution

Get virtual machine files in a datastore with powercli

Hello,

How to list the virtual machine files on a datastore with PowerCLI?

I have a virtual machine and I want see all file in a datastore (like .vmdk, .-flst.vmdk, .log, .nvram) using powercli. Is it possible?

Tks.

Tags (1)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try the following, it uses a function from my Friendly Units post.

function Get-FriendlyUnit{

<#

.SYNOPSIS  Convert numbers into smaller binary multiples

.DESCRIPTION The function accepts a value and will convert it

  into the biggest binary unit available.

.NOTES  Author:  Luc Dekens

.PARAMETER Value

  The value you want to convert.

  This number must be positive.

.PARAMETER IEC

  A switch to indicate if the function shall return the IEC

  unit names, or the more commonly used unit names.

  The default is to use the commonly used unit names.

.EXAMPLE

  PS> Get-FriendlyUnit -Value 123456

.EXAMPLE

  PS> 123456 | Get-FriendlyUnit -IEC

.EXAMPLE

  PS> Get-FriendlyUnit -Value 123456,789123, 45678

#>

  param(

  [CmdletBinding()]

  [parameter(Mandatory = $true, ValueFromPipeline = $true)]

  [double[]]$Value,

  [switch]$IEC

  )

  begin{

    $OldUnits = "B","KB","MB","GB","TB","PB","EB","ZB","YB"

    $IecUnits = "B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"

    if($IEC){$units = $IecUnits}else{$units=$OldUnits}

  }

  process{

    $Value | %{

      if($_ -lt 0){

        write-Error "Numbers must be positive."

        break

      }

      if($value -gt 0){

        $modifier = [math]::Floor([Math]::Log($_,1KB))

      }

      else{

        $modifier = 0

      }

      New-Object PSObject -Property @{

        Value = $_ / [math]::Pow(1KB,$modifier)

        Unit = &{if($modifier -lt $units.Count){$units[$modifier]}else{"1KB E{0}" -f $modifier}}

      }

    }

  }

}

foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,'LayoutEx.File')){

    $vm.LayoutEx.File |

    Select @{N='VM';E={$vm.Name}},

        @{N='Name';E={$_.Name.Split(' ')[1].Split('/')[1]}},

        @{N='Path';E={$_.Name}},

        @{N='FileType';E={$_.Type}},

        @{N='Datastore';E={$_.Name.Split(']')[0].TrimStart('[')}},

        @{N='Size';E={

            $val = Get-FriendlyUnit -Value $_.Size

            "{0:n2} {1}" -f $val.Value,$val.Unit

            }}

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

8 Replies
LucD
Leadership
Leadership
Jump to solution

You can use the Datastore Provider that comes with PowerCLI.

Something like this, it will list all files for the VM named MyVM on datastore MyDS

$ds = Get-Datastore "MyDS"

New-PSDrive -Location $ds -Name ds -PSProvider VimDatastore -Root "\"

Get-ChildItem -Path ds:/myVM -Recurse

Remove-PSDrive -Name ds -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
chiisaryuu
Enthusiast
Enthusiast
Jump to solution

Tks LucD,

But is possible get this information like tab "Storage Views" in the vSphere Client?

This view contains important information for me.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean like this ?

$ds = Get-Datastore 'MyDS'

$vmName = 'MyVM'

New-PSDrive -Location $ds -Name ds -PSProvider VimDatastore -Root "\" | Out-Null

Get-ChildItem -Path "ds:/$($vmName)" |

Select Name,DatastoreFullPath,ItemType,Datastore,@{N='Size';E={$_.Length}}

Remove-PSDrive -Name ds -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
chiisaryuu
Enthusiast
Enthusiast
Jump to solution

LucD,

When I use your script show me this information:

FS5TDWTDA001-VM.nvram

FS5TDWTDA001-VM.vmx

FS5TDWTDA001-VM_7.vmdk

FS5TDWTDA001-VM1-internal.vmdk

vmware-1.log

vmware-2.log

FS5TDWTDA001-VM_7-checkpoint.vmdk

vmware.log

FS5TDWTDA001-VM.vmxf

FS5TDWTDA001-VM.vmsd

FS5TDWTDA001-VM_7-sesparse.vmdk

FS5TDWTDA001-VM1-internal-flat.vmdk

FS5TDWTDA001-VM_7-checkpoint-sesparse.vmdk

But in the vCenter show like it:

Storage_Views.png

Is possible show it with powerCli?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, you're looking at linked-clone VDI stations, correct ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
chiisaryuu
Enthusiast
Enthusiast
Jump to solution

Yes, correct.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try the following, it uses a function from my Friendly Units post.

function Get-FriendlyUnit{

<#

.SYNOPSIS  Convert numbers into smaller binary multiples

.DESCRIPTION The function accepts a value and will convert it

  into the biggest binary unit available.

.NOTES  Author:  Luc Dekens

.PARAMETER Value

  The value you want to convert.

  This number must be positive.

.PARAMETER IEC

  A switch to indicate if the function shall return the IEC

  unit names, or the more commonly used unit names.

  The default is to use the commonly used unit names.

.EXAMPLE

  PS> Get-FriendlyUnit -Value 123456

.EXAMPLE

  PS> 123456 | Get-FriendlyUnit -IEC

.EXAMPLE

  PS> Get-FriendlyUnit -Value 123456,789123, 45678

#>

  param(

  [CmdletBinding()]

  [parameter(Mandatory = $true, ValueFromPipeline = $true)]

  [double[]]$Value,

  [switch]$IEC

  )

  begin{

    $OldUnits = "B","KB","MB","GB","TB","PB","EB","ZB","YB"

    $IecUnits = "B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"

    if($IEC){$units = $IecUnits}else{$units=$OldUnits}

  }

  process{

    $Value | %{

      if($_ -lt 0){

        write-Error "Numbers must be positive."

        break

      }

      if($value -gt 0){

        $modifier = [math]::Floor([Math]::Log($_,1KB))

      }

      else{

        $modifier = 0

      }

      New-Object PSObject -Property @{

        Value = $_ / [math]::Pow(1KB,$modifier)

        Unit = &{if($modifier -lt $units.Count){$units[$modifier]}else{"1KB E{0}" -f $modifier}}

      }

    }

  }

}

foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,'LayoutEx.File')){

    $vm.LayoutEx.File |

    Select @{N='VM';E={$vm.Name}},

        @{N='Name';E={$_.Name.Split(' ')[1].Split('/')[1]}},

        @{N='Path';E={$_.Name}},

        @{N='FileType';E={$_.Type}},

        @{N='Datastore';E={$_.Name.Split(']')[0].TrimStart('[')}},

        @{N='Size';E={

            $val = Get-FriendlyUnit -Value $_.Size

            "{0:n2} {1}" -f $val.Value,$val.Unit

            }}

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

chiisaryuu
Enthusiast
Enthusiast
Jump to solution

Great LucD is exactly I needed.

Thank you very much

0 Kudos