Powercli simple

From UVOO Tech Wiki
Jump to navigation Jump to search

Run command on all

Linux (Not Windows)

$vCenters = @("vmc01", "vmc02")
Connect-VIServer -Server $vCenters

$hostName="foo"
$GuestUser="foo@example.com"
$GuestPassword="MyPass"
$cmd="set +o; echo $GuestPassword | sudo -S service consul-template-agent stop"

foreach ($i in get-vm){
  $os=$i.GuestId
  # if( ("$os" -notlike "*windows*") -and ($i.PowerState -eq "PoweredOn") -and ($i.Name -eq "$hostName" ){
  if( ("$os" -notlike "*windows*") -and ($i.PowerState -eq "PoweredOn") ){
    echo $i.Name; Invoke-VMScript -vm $i.Name -ScriptText $cmd -ScriptType Bash -GuestUser $GuestUser -GuestPassword $GuestPassword
  }
}

Windows

$vCenters = @("vmc01", "vmc02")
Connect-VIServer -Server $vCenters

$cred = Get-Credential; $cmd="stop-service consul"; foreach($i in get-vm){ $os=$i.GuestId; if( ("$os" -like "*windows*") -and ($
i.PowerState -eq "PoweredOn") ){Invoke-VMScript -vm $i.Name -ScriptText $cmd -ScriptType Powershell -GuestCredential $cred } }