使用Powershell发送邮件时电子邮件正文被切断
所以我有一个PS脚本,我用它来检测计算机正在使用什么显示器。目标是通过RMM服务在客户端的计算机上远程运行这些脚本,以便在不联系客户端的情况下获取这些信息。为了获得输出,我想通过电子邮件将信息发送给自己。我设法获得了一封要发送的电子邮件,但与Powershell中显示的输出相比,电子邮件的正文被切断。我使用$body变量打印到命令行和电子邮件正文,因此它们应该是相同的,但它们不是。有人看到我的代码有什么问题吗(除了为了隐私而删除的电子邮件信息) 编辑:我试着对[$body+=“`n$Name,$Serial”]行进行注释,如果跳过了这一部分,电子邮件就可以正常发送。Cpt。Whale建议它与``n字符有关,但在我使用它的其他地方,它工作得很好。所以我认为ForEach循环的第二次迭代出了问题,但是我没有足够的PS经验来知道它可能是什么。使用Powershell发送邮件时电子邮件正文被切断,powershell,email,Powershell,Email,所以我有一个PS脚本,我用它来检测计算机正在使用什么显示器。目标是通过RMM服务在客户端的计算机上远程运行这些脚本,以便在不联系客户端的情况下获取这些信息。为了获得输出,我想通过电子邮件将信息发送给自己。我设法获得了一封要发送的电子邮件,但与Powershell中显示的输出相比,电子邮件的正文被切断。我使用$body变量打印到命令行和电子邮件正文,因此它们应该是相同的,但它们不是。有人看到我的代码有什么问题吗(除了为了隐私而删除的电子邮件信息) 编辑:我试着对[$body+=“`n$Name
所有这些都考虑到了。这是非常谨慎/有价值的,将代码块作为单个任务进行一步,以确保您在进入下一步之前得到预期的结果 例如,单步执行您的操作,使用PowerShell变量压缩将结果分配给变量,同时将结果输出到屏幕,以便实时监控正在发生的事情以及正在生成/传递的内容(我确实对您的代码进行了一些重构):
如果您在文本编辑器中查看电子邮件,是否会看到正文内容没有显示?一个常见的问题是html格式不符合
`n
我编辑了我的问题。所以我不明白为什么会是``n字符,因为我在多行中使用它,如果我在ForEach循环中注释掉[$body+=“`n$Name,$Serial”],它们会正确显示。这让我觉得在循环的第二次迭代中出现了一些问题,但我对powershell的了解还不够,无法知道它可能是什么。我运行了您的脚本,发现以下错误:“发送邮件:无法转换”System.Object[]参数“Body”所需的类型“System.String”。不支持指定的方法。在C:\PS\u Scripts\Monitor.ps1:140 char:18+Send-MailMessage@sendMailMessageSplat+~~~~~~~~~~~~~~~~~~~~~+CategoryInfo:InvalidArgument:(:)[Send-MailMessage],ParameterBindingException+FullyQualifiedErrorId:CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage“另外,我的原始脚本没有复制任何内容,因此我不确定您为什么要将该“功能”添加到解决方案中。嗯,我没有测试Send MailMessage的内容,所以,嗯,好吧,但对于添加任何功能,我没有补充什么。然而,使用多监视器设置(我的包括一个4端口便携式显示端口),您将获得重复的信息(按照for循环,“$videooutput”中每个项目1个)。在单个监视器上进行此设置将不起作用。
$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
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