#requires -version 2.0 #requires -pssnapin VMware.VimAutomation.Core # Jeffery Hicks # http://jdhitsolutions.com/blog # follow on Twitter: http://twitter.com/JeffHicks # # "Those who forget to script are doomed to repeat their work." # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** #note I've hard-coded default values so I don't have to type as much #You will need to edit. Function Get-RunningVM { 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