PowerShell将对象添加到电子邮件正文中

时间:2014-05-28 18:56:18

标签: powershell outlook

我希望能够从此脚本中收集信息并将其放入电子邮件正文中。在大多数情况下,它似乎工作,除了我想要序列号,osversion,模型,登录电子邮件正文中的不同行。

[CmdletBinding()]
param ( 
    $computername = (Read-Host "Enter Computer Name"), 


    $subject = 'Serial Numbers',

    $to = 'test@test.com',

    $bcc = 'test@test.com',

    $body = $Collection,

    #$body = (Get-Content -path "c:\powershell\serialnumber\SerialNumber.csv"),

    #$mail = "mailto:$to&subject=$subject&body=$body",




    [string]$ErrorLog = 'c:\powershell\useful\errorlog\retry.txt',

    [switch]$LogErrors
    )

 [Array]$Collection = foreach($computer in $computername)

 {

 $os = Get-WmiObject `
        Win32_OperatingSystem -computer $computer


 $bios = Get-WmiObject `
         Win32_BIOS -computer $computer


 $model = Get-WmiObject `
        Win32_ComputerSystem -computer $computer

 $Monitor = Get-WmiObject -computer $computer WmiMonitorID -Namespace root\wmi | 
    ForEach-Object {($_.UserFriendlyName -notmatch 0 | 
    foreach {[char]$_}) -join ""; ($_.SerialNumberID -notmatch 0 | 
    foreach {[char]$_}) -join ""}



   $body = New-Object -TypeName PSObject -Property @{
   Computername = $computer;
   LoggedIn = $Model.username;
   OSVersion = $os.Caption;
   SerialNumber = $bios.SerialNumber;
   Model = $Model.Model;
   Monitor = $Monitor;
   } 






 #$obj | convertTo-csv | out-string

 #$obj = $body
 #$Collection = [string]$body


 #[String]$Collection

 [string]$body





 Start-Process -FilePath "mailto:$to&bcc=$bcc&subject=$subject&body=$body"
}


$Collection

1 个答案:

答案 0 :(得分:2)

您无法直接将对象用作电子邮件正文,它必须是[字符串]。试试这个:

$body = New-Object -TypeName PSObject -Property @{
   Computername = $computer;
   LoggedIn = $Model.username;
   OSVersion = $os.Caption;
   SerialNumber = $bios.SerialNumber;
   Model = $Model.Model;
   Monitor = $Monitor;
   } | format-list | out-string