powershell Hyper-V VM AutomaticStopAction

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell Hyper-V VM AutomaticStopAction相关的知识,希望对你有一定的参考价值。

#determine if current VM AutomaticStopAction settings is using up a lot of harddrive space
#---------------------------------------------------------
$vmMemory = 0
#---------------------------------------------------------
try {
    $vms = Get-VM -ErrorAction Stop
    foreach ($vm in $vms) {
        if ($vm.AutomaticStopAction -eq "Save") {
            $vmMemory += [math]::round($vm.MemoryAssigned / 1GB, 0)
        }
    }
    Write-Output "VM .bin files are reserving a total of: $vmMemory GB of Hard Drive Space"
}
catch {
    Write-Output "An error was encountered getting VM information:"
    Write-Error $_
}


#*********************************************************
#the following code will interact with the user to determine if they would like to change
#the AutomaticStopAction for all VMs on the server

#warn user
Write-Output "This will SHUT-DOWN all VMs that are set to AutomaticStopAction 'Save',"`
    "change to 'Shutdown', and the start the VMs back up"
#ask user if they are ABSOLUTELY sure they want to do this
while ("Y", "N", "y", "n" -notcontains $choice) {
    $choice = Read-Host "Are you ABSOLUTELY sure you want to proceed? (Y/N)"
}
if ($choice -eq "Y" -or $choice -eq "y") {
    try {
        $vms = Get-VM -ErrorAction Stop
        Write-Output "----------------------------------"
        foreach ($vm in $vms) {
            if ($vm.AutomaticStopAction -eq "Save") {
                $vmname = $vm.Name
                Write-Output "Stopping $vmname ..."
                Stop-VM -Name $vmname -ErrorAction Stop | Out-Null
                Write-Output "Changing AutomaticStopAction to ShutDown..."
                Set-VM -Name $vmname -AutomaticStopAction ShutDown -ErrorAction Stop
                Write-Output "Starting $vmname ..."
                Start-VM -Name $vmname -ErrorAction Stop
                Write-Output "Started."
                Write-Output "----------------------------------"
            }
        }
    }
    catch {
        Write-Output "An error was encountered:"
        Write-Error $_
    }
}
else {
    Write-Output "No action taken."
}

以上是关于powershell Hyper-V VM AutomaticStopAction的主要内容,如果未能解决你的问题,请参考以下文章

powershell 使用用户提示创建新的Hyper-V VM

使用PowerShell Direct从Hyper-V主机管理VM

powershell 仅启用Hyper-V PowerShell

powershell 各种Hyper-V相关的PowerShell

Windows Server 2012 R2 Hyper-V PowerShell远端安装Hyper-V

Hyper-V 2016 系列教程39 在 Windows 10中使用 Hyper-V 和 Windows PowerShell