
[英]Email formatting with powershell using send-mailmessage
[英]Email body cut off when using Send-Mailmessage using Powershell
所以我有一个 PS 脚本,我用它来检测计算机正在使用哪些显示器。 目标是通过 RMM 服务在客户的计算机上远程运行这些脚本,以在不联系客户的情况下获取该信息。 为了获得 output,我想通过 email 将信息发送给自己。 我设法得到一个 email 发送,但 email 的主体被切断,与 outputA9531B834AD47B 中所示我正在使用 $body 变量打印到命令行和 email 正文,所以它们应该是相同的,但它们不是。 有没有人看到我的代码有什么问题(除了 email 信息因隐私而被删除)?
$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"] 行,如果 email 跳过该部分,它发送就好了。 Cpt。 Whale 建议它与 ``n 字符有关,但它在我使用它的其他地方工作得很好。 所以我认为 ForEach 循环的第二次迭代出了点问题,但我没有足够的 PS 经验来知道它可能是什么。 注释掉该行的结果
这一切都考虑到了。 这是非常谨慎/有价值的,将代码块的一个步骤作为一项任务,以确保您在进入下一步之前获得预期的结果。
例如,通过你的,使用 PowerShell 变量挤压将结果分配给变量,同时将 output 分配给屏幕,以便实时监控正在发生的事情和正在生产/通过的事情(我确实重构了你的代码少量):
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
您确定最终结果是您所追求的,成为 email 或更有限版本的一部分吗? 这意味着该循环将重复的东西。
根据您的错误评论进行更新。
改成这个来处理那个...
[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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.