Hello guys,
i need help to extract from my VCenter, with powercli script , all information like this :
Name | State | Uptime | NIC | PortGroup |
| IP | Host | Provisioned Space | Used Space | CPU | RAM |
Reservation - RAM/CPU | VMDK / Datastore | Host CPU - MHz | Host Mem - MB | Notes | Fault Tolerance | Snapshot | VMWare Tools | NB_LAST_BACKUP | Tags |
and send email on cvs and html format.
Please help me.
Thx
Davide
My bad, I obviously forgot the script is using Get-View.
This should return all IP addresses in that case
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
worked like a charm - thankyou - stay safe
Hi LucD,
Could you change the original code to include the full folder path and If SRM Placeholder or not using the get-view virtualmachine?
Thanks,
Try like this
$report = @()
foreach ($vm in Get-View -ViewType Virtualmachine){
$vms = "" | Select-Object VMName, Hostname, IPAddress, OS, Boottime, VMState, TotalCPU, CPUAffinity,
CPUHotAdd, CPUShare, CPUlimit, OverallCpuUsage, CPUreservation, TotalMemory, MemoryShare, MemoryUsage,
MemoryHotAdd, MemoryLimit, MemoryReservation, Swapped, Ballooned, Compressed, TotalNics, ToolsStatus,
ToolsVersion, HardwareVersion, TimeSync, CBT, Portgroup, VMHost, ProvisionedSpaceGB, UsedSpaceGB, Datastore,
Notes, FaultTolerance, SnapshotName, SnapshotDate, SnapshotSizeGB, Owner, NB_last_backup, FolderPath, SRMPlaceHolder
$vms.VMName = $vm.Name
$vms.Hostname = $vm.guest.hostname
$vms.IPAddress = $vm.guest.ipAddress
$vms.OS = $vm.Config.GuestFullName
$vms.Boottime = $vm.Runtime.BootTime
$vms.VMState = $vm.summary.runtime.powerState
$vms.TotalCPU = $vm.summary.config.numcpu
$vms.CPUAffinity = $vm.Config.CpuAffinity
$vms.CPUHotAdd = $vm.Config.CpuHotAddEnabled
$vms.CPUShare = $vm.Config.CpuAllocation.Shares.Level
$vms.TotalMemory = $vm.summary.config.memorysizemb
$vms.MemoryHotAdd = $vm.Config.MemoryHotAddEnabled
$vms.MemoryShare = $vm.Config.MemoryAllocation.Shares.Level
$vms.TotalNics = $vm.summary.config.numEthernetCards
$vms.OverallCpuUsage = $vm.summary.quickStats.OverallCpuUsage
$vms.MemoryUsage = $vm.summary.quickStats.guestMemoryUsage
$vms.ToolsStatus = $vm.guest.toolsstatus
$vms.ToolsVersion = $vm.config.tools.toolsversion
$vms.TimeSync = $vm.Config.Tools.SyncTimeWithHost
$vms.HardwareVersion = $vm.config.Version
$vms.MemoryLimit = $vm.resourceconfig.memoryallocation.limit
$vms.MemoryReservation = $vm.resourceconfig.memoryallocation.reservation
$vms.CPUreservation = $vm.resourceconfig.cpuallocation.reservation
$vms.CPUlimit = $vm.resourceconfig.cpuallocation.limit
$vms.CBT = $vm.Config.ChangeTrackingEnabled
$vms.Swapped = $vm.Summary.QuickStats.SwappedMemory
$vms.Ballooned = $vm.Summary.QuickStats.BalloonedMemory
$vms.Compressed = $vm.Summary.QuickStats.CompressedMemory
$vms.Portgroup = Get-View -Id $vm.Network -Property Name | Select-Object -ExpandProperty Name
$vms.VMHost = Get-View -Id $vm.Runtime.Host -Property Name | Select-Object -ExpandProperty Name
$vms.ProvisionedSpaceGB = [math]::Round(($vm.Summary.Storage.Committed + $vm.Summary.Storage.UnCommitted) / 1GB, 2)
$vms.UsedSpaceGB = [math]::Round($vm.Summary.Storage.Committed / 1GB, 2)
$vms.Datastore = $vm.Config.DatastoreUrl[0].Name
$vms.Notes = $vm.Config.Annotation
$vms.FaultTolerance = $vm.Runtime.FaultToleranceState
$vms.SnapshotName = & { $script:snaps = Get-Snapshot -VM $vm.Name; $script:snaps.Name -join ',' }
$vms.SnapshotDate = $script:snaps.Created -join ','
$vms.SnapshotSizeGB = $script:snaps.SizeGB -join ','
$vms.Owner = (Get-TagAssignment -Category Owner -Entity $vm.Name).Tag.Name
$vms.NB_last_backup = Get-VM -Name $vm.Name | Select-Object -ExpandProperty Customfields | Where-Object { $_.Key -eq 'Last Backup' } | Select-Object -ExpandProperty Value
$vms.FolderPath = & {
$current = Get-View $vm.Parent
$path = $_.Name
do {
$parent = $current
if ($parent.Name -ne "vm") { $path = $parent.Name + "\" + $path }
$current = Get-View $current.Parent
} while ($current.Parent -ne $null)
$path
}
$vms.SRMPlaceHolder = $vm.Config.ManagedBy.ExtensionKey -eq 'com.vmware.vcDr'
$Report += $vms
}
$report
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Folder path works great, thanks. Could you include in the filter, any VMs that are true for SRMPlaceholder?
Thanks!
Try like this
foreach ($vm in Get-View -ViewType Virtualmachine -Filter @{'Config.ManagedBy.ExtensionKey' = "com\.vmware\.vcDr" }) {
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry, I didn't realize until after I ran it with your code. I need it to return VMs not SRM managed, so SRMPlaceholder equal to False.
Try with this RegEx negative lookahead
foreach ($vm in Get-View -ViewType Virtualmachine -Filter @{'Config.ManagedBy.ExtensionKey' = "^((?!com\.vmware\.vcDr).)*$" }) {
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
When I do that, it only returns two VMs, when a couple thousand should be returned. I guess, looking at the current VM loaded into $vm, $vm.config.managedby is not populated. So, I think I want all the VMs that have nothing populated for config.managedby. If they are managed, those should be excluded. When I do,
The Filter expects a hash table, the Value part (right side) is a RegEx expression.
Try something like this to match an empty string
-Filter @{'Config.ManagedBy' = "^.{0}$"}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Using that still returns nothing back when it should return a few thousand servers.
Try with a Where-clause instead of the Filter
foreach ($vm in (Get-View -ViewType Virtualmachine | Where-Object {$_.Config.ManagedBy.EXtensionKey -eq ''}) ) {
It could be that the following is a better test
foreach ($vm in (Get-View -ViewType Virtualmachine | Where-Object {$_.Config.ManagedBy -eq $null}) ) {
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The second statement seems to be working. Thanks for the help
Hello again,
Wondering if you know of a more optimal way of collecting all of this information on about 3000 VMs? Currently the script I have takes about 8 hours to run. While this can work, if need be, the quicker the better. Any help would be appreciated. Thanks.
$audit = @()
foreach($vm in Get-View -ViewType Virtualmachine | ? {$_.Config.ManagedBy -eq $null} <#| select -First 500#>){
$VMs = "" | Select-Object Hostname,OSGuestID,Powerstate,Notes,FolderPath,IPAddress,OSType,OSConfig,OSTools,Cluster,VMHost,VMWareToolsVersion,CMDB,NumNetworks,Template,TotalCPU,TotalMemoryMB, UsedSpaceGB, ProvisionedSpaceGB,Tags,SnapshotName,SnapshotDate,SnapshotSizeGB
$VMs.Hostname = $vm.Name
$VMs.OSConfig = $vm.Config.GuestFullName
$VMs.OSTools = $vm.Guest.GuestFullName
$VMs.Powerstate = $vm.summary.runtime.powerState
$VMs.TotalCPU = $vm.summary.config.numcpu
$VMs.TotalMemoryMB = $vm.summary.config.memorysizemb
$VMs.VMHost = Get-View -Id $vm.Runtime.Host -property Name | select -ExpandProperty Name
$VMs.VMWareToolsVersion = $vm.config.tools.toolsversion
$VMs.UsedSpaceGB = [math]::Round($vm.Summary.Storage.Committed/1GB,2)
$VMs.Notes = $vm.Config.Annotation
$VMs.NumNetworks = $vm.summary.config.numEthernetCards
$VMs.ProvisionedSpaceGB = [math]::Round(($vm.Summary.Storage.Committed + $vm.Summary.Storage.UnCommitted)/1GB,2)
$VMs.CMDB = ($vm.Value | ? Key -eq ($vm.AvailableField | ? Name -eq "CMDB_ID").Key).Value
$VMs.Template = $vm.config.Template
$VMs.Cluster = Get-Cluster -vm $vm.name
$VMs.SnapshotName = &{$script:snaps = Get-Snapshot -VM $vm.Name; $script:snaps.Name -join ','}
$VMs.SnapshotDate = $script:snaps.Created -join ','
$VMs.SnapshotSizeGB = $script:snaps.SizeGB -join ','
#$VMs.Tags = (Get-TagAssignment -Entity $vm.Name).Tag.Name -join ','
$VMs.OSGuestID = $vm.Config.Guestid
$VMs.OSType = $vm.Guest.GuestFamily
#Full Folder Path
$VMs.FolderPath = & {
$current = Get-View $vm.Parent
$path = $_.Name
do {
$parent = $current
if ($parent.Name -ne "vm") { $path = $parent.Name + "\" + $path }
$current = Get-View $current.Parent
} while ($current.Parent -ne $null)
$path
}
#SRM - True or False
#$VMs.SRMPlaceHolder = $vm.Config.ManagedBy.ExtensionKey -eq 'com.vmware.vcDr'
#IP Address
$VMs.IPAddress = $vm.summary.Guest.IpAddress -join ','
#VMs.IPAddress = $vm.Guest.Net.IpAddress -join ','
$vm.Name
write-host "Current time:" $stopwatch.Elapsed`n $i
$i += 1
$audit += $VMs
}
#Will eventually output to folder before the RVTools folder
$audit | Export-Csv -path $filePath
One way would be to introduce some parallelism, in other words, split up the job into multiple parts.
For example, 6 jobs doing 500 VMs each.
Each part could then be run as a separate background job with Start-Job.
I did a short post on the concept in Running a background job
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry quite new to Powershell and PowerCLI, any more advice on background jobs? I like the idea, especially if it cuts down the total time to complete.
Thanks
Not sure what advice you are looking for.
That post I mentioned shows how you can use Start-Job with PowerCLI code.
It's a matter of breaking up the original Get-View into separate chunks and then calling Start-Job with the original code to get the properties for each of these chunks.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I keep getting this error for this section of code:
$VMs.Tags = (Get-TagAssignment -Entity $vm.Name).Tag.Name -join ','
Error code string:
Get-TagAssignment: C:\CP\Scripts\Combination Exports\Gather VMWare Details.ps1:37:18
Line |
37 | $VMs.Tags = (Get-TagAssignment -Entity $vm.Name).Tag.Name -join ' …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| 2/20/2023 3:42:16 PM Get-TagAssignment com.vmware.vapi.std.errors.unauthenticated {'messages': [com.vmware.vapi.std.localizable_message {'id': vapi.method.authentication.required, 'default_message': This method requires
| authentication., 'args': [], 'params': , 'localized':}], 'data': , 'error_type': UNAUTHENTICATED, 'challenge':}
That is a rather generic error and could be produced for many reasons.
Some things to try:
- stop/start your PowerShell/PowerCLI session
- uninstall/install all PowerCLI modules
- restart your VCSA
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference