首页 > 解决方案 > 使用 Powershell 使用 Send-Mailmessage 时电子邮件正文被截断

问题描述

所以我有一个 PS 脚本,我用它来检测计算机正在使用哪些显示器。目标是通过 RMM 服务在客户的计算机上远程运行这些脚本,以在不联系客户的情况下获取该信息。为了获得输出,我想通过电子邮件将信息发送给自己。我设法发送了一封电子邮件,但与 Powershell 中显示的输出相比,电子邮件的正文被截断了。我正在使用 $body 变量打印到命令行和电子邮件正文,所以它们应该是相同的,但它们不是。是否有人发现我的代码有任何问题(除了出于隐私原因删除电子邮件信息)?

这是命令行的输出

这是电子邮件中的输出

$body = "Name, Serial"
function Decode {
    If ($args[0] -is [System.Array]) {
        [System.Text.Encoding]::ASCII.GetString($args[0])
    }
    Else {
        "Not Found"
    }
}

ForEach ($Monitor in Get-WmiObject WmiMonitorID -Namespace root\wmi) {  
        $Name = Decode $Monitor.UserFriendlyName -notmatch 0
    $Serial = Decode $Monitor.SerialNumberID -notmatch 0

#       echo "$Name, $Serial"
    $body += "`n$Name, $Serial"
}

function Get-MonitorConnectionType ($connector){
    switch ($connector) {
            '-2' {'Uninitialized'} 
            '-1' {'Other'}
            0 {'VGA'}
            1 {'SVideo'}
            2 {'Composite'}
            3 {'Component'}
            4 {'DVI'}
            5 {'HDMI'}
            6 {'LVDS'}
            8 {'D_JPN'}
            9 {'SDI'}
            10 {'DisplayPort'}
            11 {'DisplayPort (Embedded)'}
            12 {'UDI'}
            13 {'UDI (Embedded)'}
            14 {'SD TV Dongle'}
            15 {'Miracast'}
            16 {'Indirect Wired'}
            '0x80000000,' {'Internal'}
            'SVIDEO,' {'SVideo (4/7 Pin)'}
            'COMPOSITE_VIDEO' {'RF'}
            'COMPONENT_VIDEO' {'RCA/BNC'}
            default {"Unknown($_)"}
    }
}

$connections = get-ciminstance -namespace root/wmi -classname WmiMonitorConnectionParams
$videooutput = $connections.videooutputtechnology
$body += "`nDetected $($connections.count) monitor(s) attached to this computer."
$body += "`nThe following monitor connection types may be in use:"
foreach ($output in $videooutput){
    $body += " $(get-monitorconnectiontype $output)"
}

$body += "`n"
write-host "$body"
$secpasswd = ConvertTo-SecureString "" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("", $secpasswd)
Send-Mailmessage -smtpServer "smtp.office365.com" -Credential $cred -Port 587 -from "" -to "" -subject "PS Email Test" -body $body  -UseSsl

编辑:我尝试注释掉 [$body += "`n$Name, $Serial"] 行,如果它跳过该部分,电子邮件发送就好了。Cpt。Whale 建议它与 ``n 字符有关,但它在我使用它的其他地方工作得很好。所以我认为 ForEach 循环的第二次迭代出了点问题,但我没有足够的 PS 经验来知道它可能是什么。 注释掉该行的结果

标签: powershellemail

解决方案


这一切都考虑到了。这是非常谨慎/有价值的,将代码块的一个步骤作为一项任务,以确保您在进入下一步之前获得预期的结果。

例如,通过你的,使用 PowerShell 变量挤压将结果分配给变量,并同时输出到屏幕,以便实时监控正在发生的事情和正在生产/传递的事情(我确实重构了你的代码少量) :

function Decode 
{
    If ($args[0] -is [System.Array]) 
    {[System.Text.Encoding]::ASCII.GetString($args[0])}
    Else {'Not Found'}
}

function Get-MonitorConnectionType ($connector)
{
    switch ($connector) 
    {
            '-2' {'Uninitialized'} 
            '-1' {'Other'}
            0 {'VGA'}
            1 {'SVideo'}
            2 {'Composite'}
            3 {'Component'}
            4 {'DVI'}
            5 {'HDMI'}
            6 {'LVDS'}
            8 {'D_JPN'}
            9 {'SDI'}
            10 {'DisplayPort'}
            11 {'DisplayPort (Embedded)'}
            12 {'UDI'}
            13 {'UDI (Embedded)'}
            14 {'SD TV Dongle'}
            15 {'Miracast'}
            16 {'Indirect Wired'}
            '0x80000000,' {'Internal'}
            'SVIDEO,' {'SVideo (4/7 Pin)'}
            'COMPOSITE_VIDEO' {'RF'}
            'COMPONENT_VIDEO' {'RCA/BNC'}
            default {"Unknown($_)"}
    }
}

                        ($Body = ForEach ($Monitor in Get-WmiObject WmiMonitorID -Namespace root\wmi) 
{  
    [PSCustomObject]@{
        Name   = Decode $Monitor.UserFriendlyName -notmatch 0
        Serial = Decode $Monitor.SerialNumberID -notmatch 0
    }
})
# Results
<#
Name          Serial          
----          ------          
Not Found     0               
ASUS VE278    B7LMTF125646    
...  
#>


($connections = Get-CimInstance -namespace root/wmi -ClassName WmiMonitorConnectionParams)
# Results
<#
Active InstanceName                            VideoOutputTechnology PSComputerName
------ ------------                            --------------------- --------------
True DISPLAY\LEN4121\4&90cefb8&0&UID265988_0            2147483648               
...
#>

($videooutput = $connections.videooutputtechnology)
# Results
<#
2147483648
...
#>

$DataMessage = "
`nDetected $($connections.count) monitor(s) attached to this computer. 
The following monitor connection types may be in use:
"
($Body += $DataMessage)
# Results
<#
Name          Serial          
----          ------          
Not Found     0               
ASUS VE278    B7LMTF125646    
...        

Detected 4 monitor(s) attached to this computer. 
The following monitor connection types may be in use:
#>

foreach ($output in $videooutput)
{($Body += " $(Get-MonitorConnectionType $output)")}
# Results
<#
Name          Serial          
----          ------          
Not Found     0               
ASUS VE278    B7LMTF125646    
...         

Detected 4 monitor(s) attached to this computer. 
The following monitor connection types may be in use:
    Unknown(2147483648)
Not Found     0               
ASUS VE278    B7LMTF125646    
...         

Detected 4 monitor(s) attached to this computer. 
The following monitor connection types may be in use:
    Unknown(2147483648)
    DisplayPort
...  


Detected 4 monitor(s) attached to this computer. 
The following monitor connection types may be in use:
    Unknown(2147483648)
    DisplayPort
    ...    
...        

Detected 4 monitor(s) attached to this computer. 
The following monitor connection types may be in use:
    Unknown(2147483648)
    DisplayPort
    ...
#>


$secpasswd = ConvertTo-SecureString '' -AsPlainText -Force
$cred      = New-Object System.Management.Automation.PSCredential ('', $secpasswd)

$sendMailMessageSplat = @{
    From       = ''
    To         = ''
    Subject    = 'PS Email Test'
    Body       = $body
    Credential = $cred
    SmtpServer = 'smtp.office365.com'
    Port       = 587
    UseSsl     = $true
}
Send-MailMessage @sendMailMessageSplat

您确定最终结果是您所追求的,成为该电子邮件的一部分或更有限的版本吗?这意味着该循环将重复的东西。

根据您的错误评论进行更新。

改成这个来处理那个...

[string]$Message = foreach ($output in $videooutput)
{($Body += " $(Get-MonitorConnectionType $output)")}

$sendMailMessageSplat = @{
    From       = ''
    To         = ''
    Subject    = 'PS Email Test'
    Body       = $Message
    Credential = $cred
    SmtpServer = 'smtp.office365.com'
    Port       = 587
    UseSsl     = $true
}
Send-MailMessage @sendMailMessageSplat

推荐阅读