Hello team
How to get folder name for each VM for below script?
$allLines = @() # Line added
Get-VM |
ForEach-Object {
$VM = $_
$VMview = $VM | Get-View
$VMResourceConfiguration = $VM | Get-VMResourceConfiguration
$VMHardDisks = $VM | Get-HardDisk
$HardDisksSizesGB = @()
$Temp = $VMHardDisks | ForEach-Object { $HardDisksSizesGB += [Math]::Round($_.CapacityKB/1MB) }
$VmdkSizeGB = ""
$Temp = $HardDisksSizesGB | ForEach-Object { $VmdkSizeGB += "$_+" }
$VmdkSizeGB = $VmdkSizeGB.TrimEnd("+")
$TotalHardDisksSizeGB = 0
$Temp = $HardDisksSizesGB | ForEach-Object { $TotalHardDisksSizeGB += $_ }
$VMDKnames = @()
$Temp = $VMHardDisks | ForEach-Object { $VMDKnames += $_.Filename.Split("/")[1] }
$Snapshots = $VM | Get-Snapshot
$Report = "" | Select-Object VMname,GuestState,ToolsStatus,UsedSpaceGB,ProvisionedSpaceGB,Version,DNSname,ESXname,ClusterName,MemoryGB,vCPUcount,vNICcount,vNICType,
IPaddresses,VMXname,VMDKname,VmdkSizeGB,TotalVmdkSizeGB,DatastoreName,ToolsVersion,ToolsUpdate,NumCpuShares,
CpuLimitMHZ,CpuReservationMHZ,NumMemShares,ReservationsMB,LimitMB,SnapshotCount,GuestOS,VLANid,Portgroup,
RDMPath,GuestDisks,GuestDiskspaceGB,GuestDiskspaceUsedGB,GuestDiskspaceUsed
$Report.VMName = $VM.name
$Report.GuestState = $VM.guest.State
$Report.ToolsStatus = $VM.guest.extensiondata.toolsstatus
$Report.UsedSpaceGB = $VM.UsedSpaceGB
$Report.ProvisionedSpaceGB = $vm.ProvisionedSpaceGB
$Report.Version = $vm.ExtensionData.config.version
$Report.DNSNAME = $vm.Guest.ExtensionData.Hostname
$Report.ESXname = $VM.Host
$Report.ClusterName = ($VM | Get-Cluster).Name
$Report.MemoryGB = $VM.MemoryMB/1KB
$Report.vCPUcount = $VM.NumCpu
$Report.vNICcount = $VM.Guest.Nics.Count
$report.vNicType = [string]::Join(',',($vm.NetworkAdapters | %{$_.Type}))
$Report.IPaddresses = [string]::Join(',',$VM.Guest.IPAddress)
$Report.VMXname = $vm.Extensiondata.Config.Files.VmPathName.Split("/")[1]
$Report.VMDKname = [string]::Join(',',$VMDKnames)
$Report.VmdkSizeGB = $VmdkSizeGB
$Report.TotalVmdkSizeGB = $TotalHardDisksSizeGB
$Report.DatastoreName = [string]::Join(',',($vm.Extensiondata.Config.DatastoreUrl | %{$_.Name}))
$Report.ToolsVersion = $vm.guest.ExtensionData.ToolsVersion
$Report.ToolsUpdate = $vm.Extensiondata.Guest.ToolsStatus
$Report.NumCpuShares = $VMResourceConfiguration.NumCPUShares
$Report.CpuLimitMHZ = &{if($VMResourceConfiguration.CpuLimitMhz -eq -1){"No Limit"}else{$VMResourceConfiguration.CpuLimitMhz}}
$Report.CpuReservationMHZ = $VMResourceConfiguration.CpuReservationMhz
$Report.NumMemShares = $VMResourceConfiguration.NumMemShares
$Report.ReservationsMB = $VMResourceConfiguration.MemReservationMB
$Report.LimitMB = &{if($VMResourceConfiguration.MemLimitMB -eq -1){"No Limit"}else{$VMResourceConfiguration.MemLimitMB}}
$Report.SnapshotCount = (@($VM | Get-Snapshot)).Count
$Report.GuestOS = $VM.Guest.OSFullName
$Report.VLANid = [string]::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.VlanId}))
$Report.Portgroup = [string]::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.Name}))
$RDMPaths = $vm | Get-HardDisk | where {$_.DiskType -like "Raw*"}
$Report.RDMPath = &{if($RDMPaths){[string]::Join(',',($RDMPaths | %{$_.ScsiCanonicalName}))}else{"No RDM"} }
$Report.GuestDisks = [string]::Join(',',($VM.Guest.Disks | %{$_.Path}))
$Report.GuestDiskspaceGB = [string]::Join(',',($VM.Guest.Disks | %{"{0:f0}" -f ($_.Capacity/1GB)}))
$Report.GuestDiskspaceUsedGB = [string]::Join(',',($VM.Guest.Disks | %{"{0:f0}" -f (($_.Capacity - $_.FreeSpace)/1GB)}))
$Report.GuestDiskspaceUsed = [string]::Join(',',($VM.Guest.Disks | %{"{0:p0}" -f (($_.Capacity - $_.FreeSpace)/$_.Capacity)}))
$allLines += $Report # Line changed
}
$allLines | Export-Csv "GDCVMreport.csv" -NoTypeInformation -UseCulture
I have a VIProperty for that (I assume you want the blue folder path).
New-VIProperty -Name 'BlueFolderPath' -ObjectType 'VirtualMachine' -Value {
param($vm)
function Get-ParentName{
param($object)
if($object.Folder){
$blue = Get-ParentName $object.Folder
$name = $object.Folder.Name
}
elseif($object.Parent -and $object.Parent.GetType().Name -like "Folder*"){
$blue = Get-ParentName $object.Parent
$name = $object.Parent.Name
}
elseif($object.ParentFolder){
$blue = Get-ParentName $object.ParentFolder
$name = $object.ParentFolder.Name
}
if("vm","Datacenters" -notcontains $name){
$blue + "/" + $name
}
else{
$blue
}
}
(Get-ParentName $vm).Remove(0,1)
} -Force | Out-Null
Get-VM -Name MyVM | select Name,BlueFolderPath
With that, it is easy to integrate it in your script. Just add the line
$report.Folder = $VM | Select BlueFolderPath
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have a VIProperty for that (I assume you want the blue folder path).
New-VIProperty -Name 'BlueFolderPath' -ObjectType 'VirtualMachine' -Value {
param($vm)
function Get-ParentName{
param($object)
if($object.Folder){
$blue = Get-ParentName $object.Folder
$name = $object.Folder.Name
}
elseif($object.Parent -and $object.Parent.GetType().Name -like "Folder*"){
$blue = Get-ParentName $object.Parent
$name = $object.Parent.Name
}
elseif($object.ParentFolder){
$blue = Get-ParentName $object.ParentFolder
$name = $object.ParentFolder.Name
}
if("vm","Datacenters" -notcontains $name){
$blue + "/" + $name
}
else{
$blue
}
}
(Get-ParentName $vm).Remove(0,1)
} -Force | Out-Null
Get-VM -Name MyVM | select Name,BlueFolderPath
With that, it is easy to integrate it in your script. Just add the line
$report.Folder = $VM | Select BlueFolderPath
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you again 🙂
one small help i didn't noticed to you one more error in my script, i am not getting VLANID -
line is below
$Report.VLANid = [string]::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.VlanId}))
is there any thing error on above line?
error:
Exception calling "Join" with "2" argument(s): "Value cannot be null.
Parameter name: values"
Do you have VMs that are perhaps not connected to any portgroup, or that are connected to a distributed switch portgroup ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
ahh my bad .. yes they are connected to distributed port 🙂
You can list the VLANId for both type of switches (VSS and VDS).
See Re: Script to get DVportgroup or Standardswtich portgroup Name, Vlan id, total ports and available p...Re: Script to get DVportgroup or Standardswtich portgroup Name, Vlan id, total ports and available ports
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thank you very much Lucd 🙂
LucD
can I know how to add additon information such as guest level service pack OSHAL, Netmask and gateway informations for my above script?
thanks
Dhanu