簡體   English   中英

Azure Runbook命令

[英]Azure Runbook Commands

因此,我有一本運行手冊,可以在周末自動關閉和啟動我的Azure VM。 然后,這會發送交易電子郵件,確認已關閉/啟動了VPS。

我已經按照說明設置了參數。 是否有理由說明為什么它在主題行中正確指出了我的虛擬機的名稱(突出顯示),但是在電子郵件正文(突出顯示)中卻使用了完全不同的名稱。

邏輯要求$VM.NAME是VPS的名稱,而不是某些隨機命令行,所以這是為什么呢? 它正確顯示在主題行中,但未正確顯示在電子郵件正文中。

   param (

    [Parameter(Mandatory=$false)] 
    [String] $VMName ="ITAMTRADINGVPS",

    [Parameter(Mandatory=$false)] 
    [String] $ResourceGroupName
)

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}


# If there is a specific resource group, then get all VMs in the resource group,
# otherwise get all VMs in the subscription.
if ($ResourceGroupName -And $VMName) 
{ 
    $VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName
}
elseif ($ResourceGroupName)
{
    $VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroupName

}
else 
{ 
    $VMs = Get-AzureRmVM
}

$vms
# Start each of the VMs
# Stop each of the VMs
foreach ($VM in $VMs)
{
    $StopRtn = $VM | Stop-AzureRmVM -Force -ErrorAction Continue
    $StopRtn
    Write-Output " this is $StopRtn "

    if ($StopRtn.IsSuccessStatusCode -eq 'True')
    {
        # The VM stopped, so send notice
        Write-Output ($VM.Name + " has been stopped")
        $Username ="xxx"

        $Password = ConvertTo-SecureString "xxx" -AsPlainText -Force

        $credential = New-Object System.Management.Automation.PSCredential $Username, $Password

        $SMTPServer = "xxx"

        $EmailFrom = "xxxx

        [string[]]$EmailTo = "xxx"
        $Subject = $VM.NAME + " notification of scheduled deallocation"

        $Body = "We'd like to let you know that your Virtual Machine $VM.NAME has successfully deallocated.
        <br>This could either be due to maintenance or a scheduled shutdown. If you were expecting this notification, please disregard this email.
        <br><br>If you need any further assistance, please contact the system administrator on xxx<br><br>Yours Sincerely<br><br>The Technical Design Team<br>xxx<br><br>"

        Send-MailMessage -smtpServer $SMTPServer -Credential $credential -Usessl -Port 587 -from $EmailFrom -to $EmailTo -subject $Subject -Body $Body -BodyAsHtml
        Write-Output "Email sent succesfully."

    }
    else
    {
        # The VM failed to stop, so send notice
        Write-Output ($VM.Name + " failed to stop")
    }
}

插圖

電子郵件正文中的變量引用(例如$ VM.Name),默認情況下將僅返回對象類型, 而PowerShell的輸出是特殊的管道活動 ,它將以列表或表格的形式呈現內容。 為了在電子郵件中包含內容, 我們必須引用屬性

[string]$EmailBody = (“VMNAME IS = [{0}]” -f $VM.Name)

C# string.format相似

請參閱本SO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM