How can I export free space on a datastore and used space on a datastore in my script below - PowerCLi
This line works to pull the datastore itself but I am not sure how to get the free space and used space on the datastore itself.
$VMInfo.Datastore = ($Datastores | where {$_.ID -match (($vmview.Datastore | Select -First 1) | Select Value).Value} | Select Name).Name
===============================================================================
"@
filter Get-FolderPath {
$_ | Get-View | % {
$row = "" | select Name, Path
$row.Name = $_.Name
$current = Get-View $_.Parent
# $path = $_.Name # Uncomment out this line if you do want the VM Name to appear at the end of the path
$path = ""
do {
$parent = $current
if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
$current = Get-View $current.Parent
} while ($current.Parent -ne $null)
$row.Path = $path
$row
}
}
$VCServerName = "spvcenter11.reyesholdings.com"
$VC = Connect-VIServer $VCServerName -user RH\-password
$VMFolder = "RFS"
$ExportFilePath = "C:\users\wkieca\desktop\True-Up2018\Export-spvcenter11.csv"
$Report = @()
$VMs = Get-VM
$Datastores = Get-Datastore | select Name, Id
$VMHosts = Get-VMHost | select Name, Parent
ForEach ($VM in $VMs) {
$VMView = $VM | Get-View
$VMInfo = {} | Select VMName,Powerstate,OS,Folder,IPAddress,ToolsStatus,ToolsVersion,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb, DiskFree, DiskUsed, BusinessUnit
$VMInfo.VMName = $vm.name
$VMInfo.Powerstate = $vm.Powerstate
$VMInfo.OS = $vm.Guest.OSFullName
$VMInfo.Folder = ($vm | Get-Folderpath).Path
$VMInfo.IPAddress = $vm.Guest.IPAddress[0]
$VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
$VMInfo.ToolsVersion = $VMView.Guest.ToolsVersion
$VMInfo.Host = $vm.VMHost.Name
$VMInfo.Cluster = $vm.host.Parent.Name
$VMInfo.Datastore = ($Datastores | where {$_.ID -match (($vmview.Datastore | Select -First 1) | Select Value).Value} | Select Name).Name
$VMInfo.NumCPU = $vm.NumCPU
$VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)
$VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)
$VMInfo.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpace -Sum).Sum / 1GB),2)
$VMInfo.DiskUsed = $VMInfo.DiskGb - $VMInfo.DiskFree
$VMInfo.BusinessUnit = $vm.CustomFields.Item("Business Unit")
$Report += $VMInfo
}
$Report = $Report | Sort-Object VMName
IF ($Report -ne "") { $report | Export-Csv $ExportFilePath -NoTypeInformation
} $VC = Disconnect-VIServer -Confirm:$False
Try something like this
filter Get-FolderPath {
$_ | Get-View | % {
$row = "" | select Name, Path
$row.Name = $_.Name
$current = Get-View $_.Parent
# $path = $_.Name # Uncomment out this line if you do want the VM Name to appear at the end of the path
$path = ""
do {
$parent = $current
if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
$current = Get-View $current.Parent
} while ($current.Parent -ne $null)
$row.Path = $path
$row
}
}
$VCServerName = "spvcenter11.reyesholdings.com"
$VC = Connect-VIServer $VCServerName -user RH\-password
$VMFolder = "RFS"
$ExportFilePath = "C:\users\wkieca\desktop\True-Up2018\Export-spvcenter11.csv"
$Report = @()
$VMs = Get-VM
$Datastores = Get-Datastore
$VMHosts = Get-VMHost | select Name, Parent
ForEach ($VM in $VMs) {
$VMView = $VM | Get-View
$ds = $Datastores | where {$_.ID -match ($vmview.Datastore | Select -First 1).Value}
$VMInfo = {} | Select VMName,Powerstate,OS,Folder,IPAddress,ToolsStatus,ToolsVersion,Host,Cluster,Datastore,DatastoreFree,DatastoreUsed,NumCPU,MemMb,DiskGb, DiskFree, DiskUsed, BusinessUnit
$VMInfo.VMName = $vm.name
$VMInfo.Powerstate = $vm.Powerstate
$VMInfo.OS = $vm.Guest.OSFullName
$VMInfo.Folder = ($vm | Get-Folderpath).Path
$VMInfo.IPAddress = $vm.Guest.IPAddress[0]
$VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
$VMInfo.ToolsVersion = $VMView.Guest.ToolsVersion
$VMInfo.Host = $vm.VMHost.Name
$VMInfo.Cluster = $vm.VMhost.Parent.Name
$VMInfo.Datastore = $ds.Name
$VMInfo.DatastoreFree = [Math]::Round($ds.FreeSpaceGB,2)
$VMInfo.DatastoreUsed = [Math]::Round($ds.CapacityGb - $ds.FreeSpaceGB,2)
$VMInfo.NumCPU = $vm.NumCPU
$VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)
$VMInfo.DiskGb = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum),2)
$VMInfo.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum),2)
$VMInfo.DiskUsed = $VMInfo.DiskGb - $VMInfo.DiskFree
$VMInfo.BusinessUnit = $vm.CustomFields.Item("Business Unit")
$Report += $VMInfo
}
$Report = $Report | Sort-Object VMName
IF ($Report -ne "") {
$report | Export-Csv $ExportFilePath -NoTypeInformation
}
$VC = Disconnect-VIServer -Confirm:$False
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Could you please help NAA ids for datastore and RDM details to above script?
Not sure how you want to display that in a similar report?
A VM can sit on multiple datastores and can have multiple vDisks.
And RDM info would by definition be per vDisk.
Perhaps open another thread with your specific request, and provide a sample layout of what you want to see in the report?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can this be modified to show all datastores connected to each VM? Currently it will default to the first connected datastore only. I have VM's connected to multiple datastores and I need the disk usage breakdown for each. Thanks
That would imply quite a bit of redundant information in the report.
Perhaps open a new thread and provide a sample layout of the report, meaning the properties you want listed.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference