Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

PowerCLI VM Peek

Posted on August 17, 2010

Now that I believe I've resolved my hardware issues with my VMware server, I'm expecting to use it much more. I'm also continuing my exploration of the PowerCLI tool set which allows me to manage my virtual infrastructure from a Windows PowerShell session. One task that I frequently need is to identify which virtual machines are running, and especially the guest host name. Last week I tweeted a PowerShell one-liner.
[cc lang="powerShell"]
get-vm | where {$_.powerstate -match "on"} | get-vmguest | select State,VMName,Hostname,OSFullname,IPAddress
[/cc]
This finds all virtual machines that are powered on then pipes that object to the Get-VMGuest cmdlet which returns the information I'm really after. Here's a sample result.
[cc lang="Powershell"]
State : Running
VmName : MyCompany Windows 7
HostName : Win7Desk01.MYCOMPANY.LOCAL
OSFullName : Microsoft Windows 7 (64-bit)
IPAddress : {172.16.10.90}
[/cc]
Now, I could have simply saved that one-liner as a function or script block but naturally I had to take things a step further. I wanted a function that would also include some error handling and handle a connection to my VM host if it wasn't already connected. Thus was born Get-RunningVM.
[cc lang="Powershell"]
Function Get-RunningVM {

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Param([string]$VMhost="ESX",
[string]$protocol="https",
[string]$port="443",
[string]$user="root",
[string]$password)

#verify we're connected to a VM host
Try {
if (get-vmhost -Server $VMhost -State connected -erroraction "Stop") {
$connected=$True
}
}
Catch {
#Try to connect
Try {
$viserver=Connect-VIserver -Server $VMhost -protocol $protocol -port $port -user $user -password $password -errorAction "Stop"
$connected=$True
}
Catch {
$msg="Failed to connect to server {0} on {1}:{2} as user {3}" -f $vmhost,$protocol,$port,$user
Write-Warning $msg
Write-Warning $error[0].Exception.Message
}
}

#if we're finally connected to a host, get running VMs
if ($connected) {
#get the powered on VMs and display VMGuest information
get-vm | where {$_.powerstate -match "on"} | get-vmguest |
select State,VMName,Hostname,OSFullname,IPAddress
}

} #end function
[/cc]
The function requires that the VMware snapin already be loaded. I have a nested set of Try/Catch constructs to check if I'm connected to VM host and if not, to do so. Once connected I execute my one-liner. I'm using basic, default connection information for my host since I'm not in a large enterprise environment. You might need to tweak the parameters. In fact, since I only have a single VM host, I'm not sure what other tweaks might be necessary for a multiple server environment. I'd love to get some real-world feedback on that. In fact, you will have to edit the default parameter values since the default VMHost is my server name. Using default values saves a lot of typing.

Download Get-RunningVM.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “PowerCLI VM Peek”

  1. Pingback: Tweets that mention PowerCLI VM Peek | The Lonely Administrator -- Topsy.com

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d