Powercli simple

From UVOO Tech Wiki
Revision as of 21:06, 12 September 2022 by Busk (talk | contribs) (Created page with "# Run command on all Linux (Not Windows) ``` $vCenters = @("vmc01", "vmc02") Connect-VIServer -Server $vCenters $hostName="foo" $GuestUser="foo@example.com" $GuestPassword...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 } }