0

需要您的帮助来找到一个运行手册/自动化脚本,通过它我可以按特定计划启动/停止 Azure 中的 VM,然后如果我们不得不延迟特定 VM 的关闭计划,它允许我们这样做。理想情况下,它应该通知最终用户,VM 将在 30 分钟左右关闭,并在需要时提供延迟关闭的选项。

自动化帐户中的 Runbook 库中是否有任何现有的 Runbook?有人可以建议或确认吗?

4

1 回答 1

0

您可以通过创建 power shell runbook 来简单地做到这一点。这是在工作时间内启动 VM。您可以根据需要将其附加到工作簿内的计划中。

param (
    [Parameter(Mandatory=$false)] 
    [String]  $AzureConnectionAssetName = 'AzureRunAsConnection',

    [Parameter(Mandatory=$false)] 
    [String] $ResourceGroupName = 'Your-VM-RG'
)

#Check for Weekends
$dayOfWeek = (Get-Date).DayOfWeek

if($dayOfWeek -eq 'Saturday' -or $dayOfWeek -eq 'Sunday'){
    exit
}

# Get the connection
$Conn = Get-AutomationConnection -Name $AzureConnectionAssetName -ErrorAction Stop     

$null = Add-AzureRmAccount `
    -ServicePrincipal `
    -TenantId $Conn.TenantId `
    -ApplicationId $Conn.ApplicationId `
    -CertificateThumbprint $Conn.CertificateThumbprint `
    -ErrorAction Stop `
    -ErrorVariable err

if($err) {
    throw $err
}

# Vet all VMs in the resource group
$VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroupName

# Start each of the VMs
foreach ($VM in $VMs)
{   
    $StartRtn = $VM | Start-AzureRmVM -ErrorAction Continue

    if ($StartRtn.IsSuccessStatusCode -ne $True)
    {
        # The VM failed to start, so send notice
        Write-Output ($VM.Name + " failed to start")
        Write-Error ($VM.Name + " failed to start. Error was:") -ErrorAction Continue
        Write-Error (ConvertTo-Json $StartRtn) -ErrorAction Continue
    }
    else
    {
        # The VM stopped, so send notice
        Write-Output ($VM.Name + " has been started")
    }
}
于 2021-01-01T05:38:13.500 回答