VMware Cloud Community
RMichael
Contributor
Contributor

I am a complete PowerCLI noob and could use some help

I found this script, did not write it myself and it works wonderfully except it does not pull in the Networks if there are more than one adapter installed.  I also want to pull in IP addresses. In a perfect world I'd have a new line for each network but I don't care all that much how it's presented at this point. I've been reading for a couple of days and decided to see if anyone here  can offer a solution. 

Get-View -ViewType VirtualMachine | %{
New-Object PSObject -Property @{
Name = $_.Name
Host = (Get-View $_.Summary.Runtime.Host).Name
Datastore = [system.String]::Join(",",($_.Storage.PerDatastoreUsage | %{Get-View $_.Datastore} | %{$_.Name}))
Size = (Get-VM $_.Name | Select-Object -Property ProvisionedSpaceGB).ProvisionedSpaceGB #($_.Storage.PerDatastoreUsage | Measure-Object -Property Committed -Sum).Sum
CPU = (Get-VM $_.Name | Select-Object -Property numCPU).NumCpu
Memory = (Get-VM $_.Name | Select-Object -Property MemoryMB).MemoryMB
Network = (Get-NetworkAdapter $_.Name | Select-Object -Property NetworkName).NetworkName
}
} | Export-Csv "C:\downloads\VM-report.csv" -NoTypeInformation -UseCulture

10 Replies
CRad14
Hot Shot
Hot Shot

I am fairly certain this would work for you...

Just replace your network line with this one and try it out

Network = (Get-NetworkAdapter $_.Name | Select-Object -Property networkname -expand)

For IPs you probably want to do something similar with Get-VMguestnetworkinterface

IPs=Get-VMGuestNetworkInterface $_.name | Where-Object {$_.ip -ne $null} |select -property ip -expand

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
RMichael
Contributor
Contributor

When I add these lines I get the following errors.  I admit I am totally capable of having made a mistake in the copy process but I don't think so based on the error.

Select-Object : Missing an argument for parameter 'ExpandProperty'. Specify a p
arameter of type 'System.String' and try again.
At line:9 char:84
+ Network = (Get-NetworkAdapter $_.Name | Select-Object -Property networkname -
expand <<<< )
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterB
   indingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.Se
   lectObjectCommand
Reply
0 Kudos
vkaranam
Enthusiast
Enthusiast

Hey Michael,

The script below gets you  following details of the Vm's in the environment. I found this in one of the forums. http://communities.vmware.com/thread/421109?tstart=0

VMName -- FolderName with full Path --PowerState --HostName --ClusterName --OSVersion --GuestIPAddress-- ToolsStatus --NumCPU --RAMMB-- DataStore1 --TotalDiskSize1 --DiskFree1--DiskUsed1 --DataStore2 --TotalDiskSize2 --DiskFree2 --DiskUsed2 and so on for Datastore and   HardDrive

connect-viserver -server "xxx" -user "xxx" -password "xxx"
$report = @()
$vms = Get-View -ViewType "virtualmachine"
foreach($vm in $vms){
     foreach($dev in $vm.Config.Hardware.Device){
          if(($dev.gettype()).Name -eq "VirtualDisk"){
                    $row = "" | select VMName, FolderPath, PowerState, HostName, ClusterName, OSVersion, GuestIPAddress, ToolsStatus, NumCPU, MemoryMB, DeviceInfo, CapacityInGB, Datastore
   $row.VMName = $vm.Name
   $current = Get-View $vm.Parent
   $path = $vm.Name
   do {
     $parent = $current
     if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}
     $current = Get-View $current.Parent
   } while ($current.Parent -ne $null)
   $row.FolderPath = $path
   $row.PowerState = $vm.runtime.powerstate
   $row.HostName = (get-view -id $vm.runtime.host).name
   $clu = (get-view -id $vm.Runtime.Host).parent
   $row.ClusterName = (Get-View -id $clu).name
   $row.OSVersion = $vm.summary.guest.guestfullname
   $row.GuestIPAddress = $vm.guest.ipaddress
   $row.ToolsStatus = $vm.guest.toolsstatus
   $row.NumCPU = $vm.config.hardware.numcpu
   $row.MemoryMB = $vm.config.hardware.MemoryMB
   $row.DeviceInfo = $dev.deviceinfo.label
   $row.CapacityInGB = [system.math]::Round($dev.CapacityInKB / 1048576)
   $row.Datastore = $dev.backing.filename.split("]")[0].trim("[")
   $report += $row
        }
    }
}
$report | Export-Csv C:\exportvms.csv -NoTypeInformation -UseCulture
This might through errors and takes times to run but once it runs completely it is generating the output correctly.
If you want to remove any data from the output it generated remove those parameters in the script.
Thanks
VK
Reply
0 Kudos
CRad14
Hot Shot
Hot Shot

Ugh sorry, I was being silly

With -expand you don't need the "-property"

Network = (Get-NetworkAdapter $_.Name | Select-Object -expand networkname)
Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
Reply
0 Kudos
RMichael
Contributor
Contributor

Thank you guys, it was very nice of both of you to help.  I've been downloading and looking at scripts for a couple of days.  This is an awesome communitee.

Reply
0 Kudos
RMichael
Contributor
Contributor

NP thank you so much.

Michael Millard

CBL

Manager of Network Administration /

Operations of Information Technology

423-490-8610

Reply
0 Kudos
snteran
Contributor
Contributor

This script is very useful and I was hoping to get some assistance changing it up a bit.  I would like to add ProvisionedSpaceGB and UsedSpaceGB.  Not being an experienced scripter, I would like to get the SUM of the CapacityInGB.

I started off with adding the ProvisionedSpaceGB and UsedSpaceGB but got errors:

$report = @()

$vms = Get-View -ViewType "virtualmachine"

foreach($vm in $vms){

     foreach($dev in $vm.Config.Hardware.Device){

          if(($dev.gettype()).Name -eq "VirtualDisk"){

                    $row = "" | select VMName, PowerState, HostName, ClusterName, OSVersion, ToolsStatus, NumCPU, MemoryMB, DeviceInfo, CapacityInGB, ProvisionedSpaceGB, UsedSpaceGB, Datastore

   $row.VMName = $vm.Name

   $current = Get-View $vm.Parent

   $path = $vm.Name

   do {

     $parent = $current

     if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}

     $current = Get-View $current.Parent

   } while ($current.Parent -ne $null)

   $row.PowerState = $vm.runtime.powerstate

   $row.HostName = (get-view -id $vm.runtime.host).name

   $clu = (get-view -id $vm.Runtime.Host).parent

   $row.ClusterName = (Get-View -id $clu).name

   $row.OSVersion = $vm.summary.guest.guestfullname

   $row.ToolsStatus = $vm.guest.toolsstatus

   $row.NumCPU = $vm.config.hardware.numcpu

   $row.MemoryMB = $vm.config.hardware.MemoryMB

   $row.DeviceInfo = $dev.deviceinfo.label

   $row.CapacityInGB = [system.math]::Round($dev.CapacityInKB / 1048576)

   $row.ProvisionedSpaceGB = (Summary.Storage.Committed + $_.Summary.Storage.Uncommitted) / 1GB

   $row.UsedSpaceGB = Summary.Storage.Committed / 1GB

   $row.Datastore = $dev.backing.filename.split("]")[0].trim("[")

   $report += $row

        }

    }

}

$report | Export-Csv C:\scripts\schtasks\exportvms.csv -NoTypeInformation -UseCulture

Errors:

The term 'Summary.Storage.Committed' is not recognized as the name of a cmdlet, function, script file, or operable prog

ram. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At line:23 char:56

+    $row.ProvisionedSpaceGB = (Summary.Storage.Committed <<<<  + $_.Summary.Storage.Uncommitted) / 1GB

    + CategoryInfo          : ObjectNotFound: (Summary.Storage.Committed:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

The term 'Summary.Storage.Committed' is not recognized as the name of a cmdlet, function, script file, or operable prog

ram. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At line:24 char:48

+    $row.UsedSpaceGB = Summary.Storage.Committed <<<<  / 1GB

    + CategoryInfo          : ObjectNotFound: (Summary.Storage.Committed:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

The term 'Summary.Storage.Committed' is not recognized as the name of a cmdlet, function, script file, or operable prog

ram. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At line:23 char:56

+    $row.ProvisionedSpaceGB = (Summary.Storage.Committed <<<<  + $_.Summary.Storage.Uncommitted) / 1GB

    + CategoryInfo          : ObjectNotFound: (Summary.Storage.Committed:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

Help please.

Reply
0 Kudos
LucD
Leadership
Leadership

You are not referring to the correct variable, that should be

$row.ProvisionedSpaceGB = ($vm.Summary.Storage.Committed + $vm.Summary.Storage.Uncommitted) / 1GB

$row.UsedSpaceGB = $vm.Summary.Storage.Committed / 1GB


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

Reply
0 Kudos
snteran
Contributor
Contributor

As always, thank you very much for your input and knowledge.

I do have one more question if I might:

Within the report there is a process to get CarpacityInGB, however this results in individual amounts and I'm hoping to get a Sum.

   $row.CapacityInGB = [system.math]::Round($dev.CapacityInKB / 1048576)

I tried to integrate a SUM process but of course I'm just failing with every try I make, could you please advise on how to integrate into the script?  I would be replacing the individual calculation with the SUM:

@{n="HardDiskSizeGB"; e={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}}

Thanks for your assistance,

Reply
0 Kudos
LucD
Leadership
Leadership

Try like this

@{N="HardDiskSizeGB";E={(Get-HardDisk -VM $_ | Measure-Object -Property CapacityGB -Sum).Sum}}


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