Hi guys,
I am trying to create a script to provide a table with the below details
hostname, OS, IP address, state, vm hard disk information
this is what i have so far
$f_guest = @()
Get-Cluster <cluster> | Get-VM |
%{
$guest = $_ | Get-VMGuest | select hostname, osfullname, ipaddress, state
$disk = $_ | Get-HardDisk | select filename, capacitykb
$f_guest += $guest
}
$f_guest
i can get the hostname, OS, IP address and state no problem, but i am struggling to see how i can incorportate the disk info into the same line in the table.
Any help would be appreciated.
Cheers
D
You can use a new temporary object to collect all properties and then add this one to the array.
Something like this
$f_guest = @() Get-Cluster "Cluster 3.5" | Get-VM |%{ $guest = $_ | Get-VMGuest | select hostname, osfullname, ipaddress, state $disk = $_ | Get-HardDisk | select filename, capacitykb $row = "" | Select hostname, osfullname, ipaddress, state, filename, capacitykb $row.hostname = $guest.HostName $row.osfullname = $guest.OSFullName $row.ipaddress = $guest.IPAddress $row.state = $guest.State $row.filename = $disk.Filename $row.capacitykb = $disk.CapacityKB $f_guest += $row } $f_guest
Note1: the IPAddress property is an array. You will not see any actual IP addresses this way in the output.
Noet2: several of the properties returned by Get-VMGuest are only available when the guest is running and when the VMware Tools are installed on the guest.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can use a new temporary object to collect all properties and then add this one to the array.
Something like this
$f_guest = @() Get-Cluster "Cluster 3.5" | Get-VM |%{ $guest = $_ | Get-VMGuest | select hostname, osfullname, ipaddress, state $disk = $_ | Get-HardDisk | select filename, capacitykb $row = "" | Select hostname, osfullname, ipaddress, state, filename, capacitykb $row.hostname = $guest.HostName $row.osfullname = $guest.OSFullName $row.ipaddress = $guest.IPAddress $row.state = $guest.State $row.filename = $disk.Filename $row.capacitykb = $disk.CapacityKB $f_guest += $row } $f_guest
Note1: the IPAddress property is an array. You will not see any actual IP addresses this way in the output.
Noet2: several of the properties returned by Get-VMGuest are only available when the guest is running and when the VMware Tools are installed on the guest.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thats brillant!!
For my own knowledge. would it be ok if you explained a couple of things in the script?
In the line
$row = "" | Select hostname, osfullname, ipaddress, state, filename, capacitykb
What does the "" give you?
Also why are there comparison operatiors used at this line?
<code>$f_guest += $row</code>
Thanks again LucD
The line
$row = "" | Select hostname, osfullname, ipaddress, state, filename, capacitykb
is a shortcut to add properties to an object. It uses a side-effect of the Select-Object cmdlet.
What is does, we start with an empty $row object and then we add the properties that are listed after the Select (alias of Select-Object) cmdlet.
This line
$f_guest += $row
is shorthand for
$f_guest = $f_guest + $row
.
It's an operator that you also find in several C-language flavors.
Similarly you have "-=", "*=", "/=" and "%=".
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
OK,
Now it makes more sense.
Thanks again appreciate the help.
LucD