Last active
January 8, 2017 11:32
-
-
Save BenNeise/5442438 to your computer and use it in GitHub Desktop.
Simple example of using PowerCLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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