我正在尝试将 VM 状态的输出从 Azure 自动化运行手册发送到电子邮件中,我使用以下代码:
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'
有人可以帮忙,如何在电子邮件中将对象内容作为字符串获取?