首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure Runbook命令

Azure Runbook命令
EN

Stack Overflow用户
提问于 2018-10-07 12:45:10
回答 1查看 153关注 0票数 1

因此,我有一个运行簿,自动关闭和启动我的Azure VM在周末。这将发送一封事务性电子邮件,确认VPS已关闭/启动。

如图所示,我已经设置了参数。为什么它在主题行中正确地声明了我的虚拟机的名称(高亮显示),但在电子邮件正文(突出显示)中,它有一个完全不同的名称,这有什么原因吗?

逻辑会规定$VM.NAME将是VPS的名称,而不是一些随机命令行,那么为什么呢?它正确地显示在主题行中,而不是电子邮件正文中。

代码语言:javascript
复制
   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")
    }
}

插图

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-08 08:04:20

变量引用(例如$VM.Name)进入电子邮件主体,默认情况下它只返回对象类型,其中在PowerShell中的输出是一个特殊的管道活动,它将将内容呈现为列表或表。为了在电子邮件中包含内容,必须引用属性

代码语言:javascript
复制
[string]$EmailBody = (“VMNAME IS = [{0}]” -f $VM.Name)

,它与string.format中的C#类似。

请参阅此SO

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52688617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档