Skip to content

Instantly share code, notes, and snippets.

@kayura-se
Created April 5, 2020 07:56
Show Gist options
  • Save kayura-se/50b3766fe0ed5df2736446deac462b13 to your computer and use it in GitHub Desktop.
Save kayura-se/50b3766fe0ed5df2736446deac462b13 to your computer and use it in GitHub Desktop.
その他の仮想マシンのパワーオフ(VMware Tools導入無し)
#その他仮想マシンをシャットダウン(パワーオフ) ※VMwareToolsが入って居ないものも考慮する
############################################
Write-Host "Other Shutdown virtual machine (power off)"
Get-Date -Format "yyyy-MM-dd-HH:mm:ss"
foreach ($getVM2 in Get-VM){
   #仮想マシンの稼働状況を確認する
   if($getVM2.PowerState -eq "PoweredOff"){
     $isPoweredOff = $true
     $getVM2.Name | Out-File $PoweroffVMlist2 -Append
   }
   else {
     $isPoweredOff = $false
   }
   # 除外対象のVMかどうかを判定してフラグをセットする
   $isExclusionVm2 = $false
   foreach ($ex in $exclusionVM){
     if($ex -eq $getVM2.Name){
       $isExclusionVm2 = $true
       break
     }
   }
   # シャットダウン対象外のVMと2回目確認からパワーオフ状態だったVMを除き、パワーオフする
   if((-not $isExclusionVm2) -and (-not $isPoweredOff)){
     Get-Date -Format "yyyy-MM-dd-HH:mm:ss"
     Write-Host $getVM2.Name
     Stop-VM $getVM2 -Confirm:$false
   }
}
#仮想マシンの稼働状況を確認する(その他パワーオフ終了後)
Write-Host "Check the operating status of the virtual machine(Other after power off)"
Get-Date -Format "yyyy-MM-dd-HH:mm:ss"
Get-VM | Format-Table -AutoSize -Wrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment