I'm very new to powershell + powercli and am in need of a hand.
We have 3 /4 hosts connected to a vCenter instance, we would like to run a powershell script that identifies which VMs are running, log the names to a file, suspends (or shutdown) the machines.
Next time the script is run, it reads the names list and powers on the relevant VMs...what is the simplest way of doing this in the PowerCLI/Powershell envrinoment.
I was thinking streamreader /writer but that seems convoluted!
Consider using Join-Path
, Set-Content
and Add-Conent
for more Powershell way. Like so,
# Combine $filepath and MTS_ON.txt, adds slashes if need be
$pfile = join-path $filePath "MTS_ON.txt"
# Create empty file or truncate existing one
set-content -path $pfile -value $([String]::Empty)
foreach($objHost in $ESXHost) {
$PoweredVMs += Get-VMHost -Name $objHost | Get-VM | where {$_.Powerstate -eq "PoweredON" }
foreach($objVM in $PoweredVMs) {
add-content -path $pfile -value $objVM
Get-VM -Name $objVM | Suspend-VM
}
} # No need to close the pfile