VMware Cloud Community
hanlin
Enthusiast
Enthusiast
Jump to solution

PowerCLI export VM+Datastore(RDM)+DiskGB+NAAID

Hi all,

Can someone help me in getting this script to pull the every datastore field of per VM please?

I have some trouble with inputting datastore and naaid, and how get naaid if a vm attached RDM devices?

$Report = @()

$ExportFilePath = "C:\test001.csv"

$VMs = Get-VM |Sort-Object Name

$Datastores = Get-Datastore | select Name, Id

$ScsiCanonicalNames = Get-VM | Get-HardDisk | Select Parent,Name,DiskType,ScsiCanonicalName,DeviceName

$VMHosts = Get-VMHost | select Name, Parent

ForEach ($VM in $VMs) {

$VMInfo = {} | Select VMName, @{N="Folder";E={$_.Folder.Name}}, Host, Cluster,datastore, ScsiCanonicalName, DiskGb

$VMInfo.VMName = $vm.name

$VMInfo.Folder = $vm.folder

$VMInfo.Host = $vm.host.name

$VMInfo.Cluster = $vm.host.Parent.Name

$VMInfo.Datastore = $vm.Datastore

$VMInfo.ScsiCanonicalName = $vm.ScsiCanonicalName

$VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)

$Report += $VMInfo

}

$Report = $Report | Sort-Object VMName

IF ($Report -ne "") { $Report | Export-Csv $ExportFilePath -NoTypeInformation }

Reply
0 Kudos
1 Solution

Accepted Solutions
hanlin
Enthusiast
Enthusiast
Jump to solution

Many thanks, I tried this script.. It's show VMName + Diskname + DiskType + Datastore + NAA, but how pull the folder name of the per VM?

@{N="Folder";E={$_.Folder.Name}} is not available on Get-HardDisk.

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM | Get-HardDisk |

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

    Name,DiskType,

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

    @{N='NAA';E={$_.ScsiCanonicalName}} |

Export-Csv C:\test001.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
hanlin
Enthusiast
Enthusiast
Jump to solution

Many thanks, I tried this script.. It's show VMName + Diskname + DiskType + Datastore + NAA, but how pull the folder name of the per VM?

@{N="Folder";E={$_.Folder.Name}} is not available on Get-HardDisk.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

On the objects returned by Get-Harddisk, the Parent property gives you access to the Get-VM object.

So you can find it under $_.Parent.Folder.


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

Reply
0 Kudos
hanlin
Enthusiast
Enthusiast
Jump to solution

LucD,

Thanks a bunch, do you have some advise me to learning how to write some scripts or useful resources?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would suggest to start with the PowerShell basics.

There is a good, free ebook available here.

What I always find a good method to learn is to set yourself a practical target, i.e. something that would be useful in your environment.

You'll probably hit some walls, but use a search engine and start reading the numerous blogs and communities that are out there.

If you have more concrete targets I could perhaps give you some more specific resources.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

How Do I get LUN number into this script ?

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Hi LuCD,

How I modify the below script to get LUN ID ?

$lun = Get-ScsiLun -VmHost $row.VMHost -CanonicalName $row.HDDisplayName

                $row.LUN = $lun.RuntimeName.SubString($lun.RuntimeName.LastIndexof("L")+1)

So that It can match with @{N="LUN";E={Get-ScsiLun -VmHost $row.VMHost -CanonicalName $row.HDDisplayName}}

Reply
0 Kudos