VMware Cloud Community
ccalhoun12
Contributor
Contributor
Jump to solution

Get-VMGuest Script Tweak Please

Here is my function that needs just a few tweaks I hope to correct the one value that is not displayed below in red. I'm new to powershell, but have several other quick functions that gather WMI info and hoped to get something similar for my VM's. Can someone please look over my scripting and see what needs to be tweaked please?

Function VM

{

param ($strComputer)

Connect-VIServer myvcenter

$VM = Get-VM $strComputer | select Name,PowerState,NumCPU,MemoryMB,Host,HardDisks,CustomFields

$VMG = Get-VMGuest $strComputer | select OSFullName,IPAddress,Nics

Write-Host ""

Write-Host "Server Name" = $VM.Name -foregroundcolor Green

Write-Host "IP Address " = $VMG.IPAddress -foregroundcolor Green

Write-Host "VLAN ID " = $VMG.Nics -foregroundcolor Green

Write-Host "Power State" = $VM.PowerState -foregroundcolor Green

Write-Host "OS " = $VMG.OSFullName -foregroundcolor Green

Write-Host "Hard Drives" = $VM.HardDisks -foregroundcolor Green

Write-Host "# of CPUs " = $VM.NumCPU -foregroundcolor Green

Write-Host "Memory(MB) " = $VM.MemoryMB -foregroundcolor Green

Write-Host "Host Server" = $VM.Host -foregroundcolor Green

Write-Host "Custom Info" = $VM.CustomFields -foregroundcolor Green

Write-Host ""

}

-


Output below in red is where I should see the $VMG.Nics value.

Server Name = myvmserver

IP Address = xxx.xxx.xxx.xxx

VLAND ID = VMWare.VimAutomation.Client20.NicInfoImp <-- Should read like "PRD VLAN 123 VM Network"

-


When I execute just the command: get-vmguest myvmserver | select Nics

I actually get the results that I want instead of the red text above. Just need help figuring out if it's storing the wrong type object for that value. Thanks for your time and help.

-Chris

PowerCLI 4.0.1 | Win 7 PSv2

Tags (2)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The Nics property is always, also if your guest has onle 1 network card, an array of NicInfoImpl object(s).

If you do a Write-Host of an array, PowerShell will display the TypeName (NicInfoImpl in this case).

I'm not exactly sure what you want to display in this line, but if these should really be the VLanIds then you would have to use the Get-VirtualPortGroup cmdlet.

Something like this for example

function Get-MyVMProperties
{
	param ($strComputer)

	$vmImpl = Get-VM $strComputer
	$VM = $vmImpl | select Name,PowerState,NumCPU,MemoryMB,Host,HardDisks,CustomFields
	$VMG = Get-VMGuest $strComputer | select OSFullName,IPAddress,Nics

	Write-Host ""
	Write-Host "Server Name" = $VM.Name -foregroundcolor Green
	Write-Host "IP Address " = $VMG.IPAddress -foregroundcolor Green
	Write-Host "VLAN ID " = (Get-VirtualPortGroup -VM $vmImpl | %{$_.VLanId}) -foregroundcolor Green
	Write-Host "Power State" = $VM.PowerState -foregroundcolor Green
	Write-Host "OS " = $VMG.OSFullName -foregroundcolor Green
	Write-Host "Hard Drives" = $VM.HardDisks -foregroundcolor Green
	Write-Host "# of CPUs " = $VM.NumCPU -foregroundcolor Green
	Write-Host "Memory(MB) " = $VM.MemoryMB -foregroundcolor Green
	Write-Host "Host Server" = $VM.Host -foregroundcolor Green
	Write-Host "Custom Info" = $VM.CustomFields -foregroundcolor Green
	Write-Host ""
}

Connect-VIServer myvcenter

# Sample call
Get-MyVMProperties MyVM

Note that I also change the name of the function to be more inline with the PowerShell standard of using a verb-noun construction.

Also note that I moved the Connect-VIServer cmdlet outside the function, otherwise you would connect each time you call the function.


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

The Nics property is always, also if your guest has onle 1 network card, an array of NicInfoImpl object(s).

If you do a Write-Host of an array, PowerShell will display the TypeName (NicInfoImpl in this case).

I'm not exactly sure what you want to display in this line, but if these should really be the VLanIds then you would have to use the Get-VirtualPortGroup cmdlet.

Something like this for example

function Get-MyVMProperties
{
	param ($strComputer)

	$vmImpl = Get-VM $strComputer
	$VM = $vmImpl | select Name,PowerState,NumCPU,MemoryMB,Host,HardDisks,CustomFields
	$VMG = Get-VMGuest $strComputer | select OSFullName,IPAddress,Nics

	Write-Host ""
	Write-Host "Server Name" = $VM.Name -foregroundcolor Green
	Write-Host "IP Address " = $VMG.IPAddress -foregroundcolor Green
	Write-Host "VLAN ID " = (Get-VirtualPortGroup -VM $vmImpl | %{$_.VLanId}) -foregroundcolor Green
	Write-Host "Power State" = $VM.PowerState -foregroundcolor Green
	Write-Host "OS " = $VMG.OSFullName -foregroundcolor Green
	Write-Host "Hard Drives" = $VM.HardDisks -foregroundcolor Green
	Write-Host "# of CPUs " = $VM.NumCPU -foregroundcolor Green
	Write-Host "Memory(MB) " = $VM.MemoryMB -foregroundcolor Green
	Write-Host "Host Server" = $VM.Host -foregroundcolor Green
	Write-Host "Custom Info" = $VM.CustomFields -foregroundcolor Green
	Write-Host ""
}

Connect-VIServer myvcenter

# Sample call
Get-MyVMProperties MyVM

Note that I also change the name of the function to be more inline with the PowerShell standard of using a verb-noun construction.

Also note that I moved the Connect-VIServer cmdlet outside the function, otherwise you would connect each time you call the function.


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

Reply
0 Kudos
ccalhoun12
Contributor
Contributor
Jump to solution

LucD,

Thanks for your time. I didn't understand how 'NicInfoImpl' worked. Makes sense now. Tested and even added another property from the array.

Thanks,

Chris

Reply
0 Kudos