【问题标题】:PowerShell add objects to body of emailPowerShell 将对象添加到电子邮件正文
【发布时间】:2014-05-29 03:05:01
【问题描述】:

我希望能够从此脚本中收集信息并将其放入电子邮件正文中。在大多数情况下,它似乎可以工作,除了我想要序列号、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

【问题讨论】:

    标签: powershell outlook


    【解决方案1】:

    您不能将对象直接用作电子邮件的正文,它必须是 [string]。试试这个:

    $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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-24
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多