Skip to content

Instantly share code, notes, and snippets.

@BenNeise
Last active January 8, 2017 11:32
Show Gist options
  • Save BenNeise/5442438 to your computer and use it in GitHub Desktop.
Save BenNeise/5442438 to your computer and use it in GitHub Desktop.
Simple example of using PowerCLI
# Get the name of the machine from the user
$strVM = Read-Host "Please enter the VM name"
# Attempt to get a machine-object with that name, continue silently if no machine found
$objectVM = Get-VM -Name $strVM -ErrorAction SilentlyContinue
# If there is a machine found
If ($objectVM) {
# Display the machine object name on screen
Write-Host "Machine object name:" $objectVM.Name
# Display the power state on screen
Write-Host "Power State:" $objectVM.PowerState
# Attempt to get the object representing the virtual machine's guest
$objVMGuest = Get-VMGuest -VM $objectVM -ErrorAction SilentlyContinue
# If that object is retreived (i.e., if VMWare tools has been running)
If ($objVMGuest){
# Display the guest hostname on screen
Write-Host "Hostname:" $objVMGuest.Hostname
# Get the IP address(es), and write them to screen
$objVMGuest.IPAddress | ForEach-Object {
# If more than one IP address is found, this will write each one to screen on a seperate line
Write-Host "IP Address:" $_
}
}
}
# If there is no machine found with the name
Else {
# Display an error message
Write-Host "ERROR No machine found with the name:" $strVM
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment