Azure Runbook输出到电子邮件 [英] Azure runbook output to email

查看:150
本文介绍了Azure Runbook输出到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Azure自动化运行手册中VM状态的输出发送到电子邮件中,我使用以下代码:

I am trying to send the output of a VM status from Azure automation runbook into email, I use the below code:

function Send-EMail {
Param (
    [Parameter(Mandatory=$true)]
    [String]$EmailTo,
    [Parameter(Mandatory=$true)]
    [String]$Subject,
    [Parameter(Mandatory=$true)]
    [String]$Body,
    [Parameter(Mandatory=$false)]
    [String]$EmailFrom="noreply@idlebytes.com",  #This gives a default value to the $EmailFrom command
    [parameter(Mandatory=$false)]
    [String] $SmtpServer = (Get-AutomationVariable -Name 'SmtpHost'),
    [parameter(Mandatory=$false)]
    [String] $SmtpUsername = (Get-AutomationVariable -Name 'SmtpUsername'),
    [parameter(Mandatory=$false)]
    [String] $SmtpPassword = (Get-AutomationVariable -Name 'SmtpPassword')
)

    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 
    $SMTPClient.EnableSsl = $true 
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SmtpUsername, $SmtpPassword); 
    $SMTPClient.Send($SMTPMessage)
    Remove-Variable -Name SMTPClient
    Remove-Variable -Name SmtpPassword

} #End Function Send-EMail

$AutomationCredentialAssetName = "PScredential"

# Get the credential asset with access to my Azure subscription
$Cred = Get-AutomationPSCredential -Name $AutomationCredentialAssetName

# Authenticate to Azure Service Management and Azure Resource Manager

Add-AzureRmAccount -Credential $Cred | Out-Null

$VMStatus = Get-AzureRmVM -Name "vm0" -ResourceGroupName "TestRG" -Status


Send-EMail -EmailTo "admin@idlebytes.com" -Body "$VMStatus" -Subject "vm0 Status"

我希望电子邮件输出打印出输出的确切状态,而它会打印对象Microsoft.Azure.Commands.Compute.Models.PSVirtualMachineInstanceView'

I expect the email output to print the exact status of the output, whereas, it prints the object Microsoft.Azure.Commands.Compute.Models.PSVirtualMachineInstanceView'

有人可以帮忙,如何在电子邮件中以字符串形式获取对象内容?

Can someone help, how to get the object content as string in email?

推荐答案

将变量引用(例如$ MyVar)添加到电子邮件正文中,默认情况下,该变量引用将仅返回对象类型,而PowerShell中的输出是特殊的管道活动,将内容呈现为列表或表格.为了在电子邮件中包含内容,我们必须分别引用不同的属性.

Adding the variable reference (e.g. $MyVar) into the email body which by default will just return the object type where the output in PowerShell is a special pipeline activity that renders the content either as a listing or as a table. In order to include content in an email we would have to reference the different properties individually.

下面是一个示例:

[string]$EmailBody = ("Property 1 = [{0}], Property 2 = [{1}], Property 3 = [{2}]" -f $MyObject.Property1, $MyObject.Property2, $MyObject.Property3)

上面的行将设置变量$ EmailBody,它是一个字符串,其中包含对象变量$ MyObject的三个属性.如果我们仅引用$ MyObject作为PowerShell输出,则将显示所有属性,但是要在电子邮件中包含这些属性,我们必须分别引用它们.

The above line would set the variable $EmailBody which is a string to include three properties of an object variable called $MyObject. If we were to just reference $MyObject for PowerShell output, all the properties would display, but to include the properties in an email, we have to reference them individually.

这篇关于Azure Runbook输出到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆