问题
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"
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?
回答1:
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.
Below is an example:
[string]$EmailBody = (“Property 1 = [{0}], Property 2 = [{1}], Property 3 = [{2}]” -f $MyObject.Property1, $MyObject.Property2, $MyObject.Property3)
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.
回答2:
Please change
$VMStatus = Get-AzureRmVM -Name "vm0" -ResourceGroupName "TestRG" -Status
To
$VMStatus = Get-AzureRmVM -Name "vm0" -ResourceGroupName "TestRG" -Status | select -expand Statuses
The first command returns a PSVirtualMachineInstanceView object while the second command returns a string array.
来源:https://stackoverflow.com/questions/37948614/azure-runbook-output-to-email