2017-05-09 31 views
0

我有一切就位,至少我認爲是這樣。我想給自己發送Get-WmiObject win32命令的輸出。例如:得到wmiobject的win32:我如何通過電子郵件的輸出

$OS = "." 
(Get-WmiObject Win32_OperatingSystem).Name |Out-String 

$secpasswd = ConvertTo-SecureString "mypassword" -AsPlainText -Force 
    $mycreds = New-Object System.Management.Automation.PSCredential 
("[email protected]", $secpasswd) 

    Send-MailMessage -To "[email protected]" -SmtpServer 
"smtp.office365.com" -Credential $mycreds -UseSsl "Backup Notification" - 
Body $body -From "[email protected]" 

我曾嘗試以下:

$body = (
    Write-Host "Computer Info:" -Message $PCinfo 
) -join "`r`n" 
    Write-Verbose -Message $body 

返回錯誤:「在‘身體’參數無法驗證論點的論據是null或空。」

任何方向,建議或例子,將不勝感激。 謝謝

回答

1

Write-Host繞過通常的PowerShell數據路由(管道);你可能想看看Get-Help Out-StringGet-Help Out-Default尋找可能的選擇。

在使用Write-Host繞過流水線時,您將作業保留爲「空」 - 即沒有要分配給變量的數據。由於$null是一個變量的合法值,因此在變量用於不允許空值的上下文中(例如Send-MailMessage)之前,這不會引發錯誤。

+0

謝謝每個人的幫助! – Wchristner

2

此格式爲您提供更豐富的信息。這需要在Win32_OperatingSystem類的內容,並將其轉換成HTML表格,將其追加到$body變量,用你的「計算機信息:」文字上面:

$body = "Computer Info: <br>" 
$body += Get-WmiObject -Class Win32_OperatingSystem | ConvertTo-HTML -Fragment 

通過其管道到Out-String有效地再現$body。這將確保它是一個字符串對象,其中Send-MailMessage-Body參數要求:

$Body = $Body | Out-String 

最後,調用Send-MailMessage使用-BodyAsHTML參數,以確保郵件在發送的HTML電子郵件:

Send-MailMessage -To "[email protected]" -From "[email protected]" -SmtpServer "smtp.office365.com" -Credential $mycreds -UseSsl "Backup Notification" -Body $body -BodyAsHTML