Azure Runbook 输出到电子邮件
Azure runbook output to email
我正在尝试将 Azure 自动化 Runbook 的 VM 状态输出发送到电子邮件中,我使用以下代码:
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'
谁能帮忙,如何在电子邮件中获取字符串形式的对象内容?
请更改
$VMStatus = Get-AzureRmVM -Name "vm0" -ResourceGroupName "TestRG" -Status
到
$VMStatus = Get-AzureRmVM -Name "vm0" -ResourceGroupName "TestRG" -Status | select -expand Statuses
第一个命令 returns 一个 PSVirtualMachineInstanceView 对象,而第二个命令 returns 一个字符串数组。
将变量引用(例如 $MyVar)添加到电子邮件正文中,默认情况下只是 return 对象类型,其中 PowerShell 中的输出是一个特殊的管道 activity 呈现内容作为列表或 table。为了在电子邮件中包含内容,我们必须单独引用不同的属性。
下面是一个例子:
[string]$EmailBody = (“Property 1 = [{0}], Property 2 = [{1}], Property 3 = [{2}]” -f $MyObject.Property1, $MyObject.Property2, $MyObject.Property3)
上面的行将设置变量 $EmailBody,它是一个字符串,包含名为 $MyObject 的对象变量的三个属性。如果我们只是为 PowerShell 输出引用 $MyObject,所有属性都会显示,但要在电子邮件中包含这些属性,我们必须单独引用它们。
我正在尝试将 Azure 自动化 Runbook 的 VM 状态输出发送到电子邮件中,我使用以下代码:
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'
谁能帮忙,如何在电子邮件中获取字符串形式的对象内容?
请更改
$VMStatus = Get-AzureRmVM -Name "vm0" -ResourceGroupName "TestRG" -Status
到
$VMStatus = Get-AzureRmVM -Name "vm0" -ResourceGroupName "TestRG" -Status | select -expand Statuses
第一个命令 returns 一个 PSVirtualMachineInstanceView 对象,而第二个命令 returns 一个字符串数组。
将变量引用(例如 $MyVar)添加到电子邮件正文中,默认情况下只是 return 对象类型,其中 PowerShell 中的输出是一个特殊的管道 activity 呈现内容作为列表或 table。为了在电子邮件中包含内容,我们必须单独引用不同的属性。
下面是一个例子:
[string]$EmailBody = (“Property 1 = [{0}], Property 2 = [{1}], Property 3 = [{2}]” -f $MyObject.Property1, $MyObject.Property2, $MyObject.Property3)
上面的行将设置变量 $EmailBody,它是一个字符串,包含名为 $MyObject 的对象变量的三个属性。如果我们只是为 PowerShell 输出引用 $MyObject,所有属性都会显示,但要在电子邮件中包含这些属性,我们必须单独引用它们。