PowerShell发送邮件后文本文件字符间多余空格问题解决
问题
我用以下PowerShell脚本从远程服务器获取数据并保存为文本文件:
$servers = @("28","29","30") # Ensure the directory for the output files exists $outputDir = "C:\temp\ServerOutputs" If (-not (Test-Path -Path $outputDir)) { New-Item -ItemType Directory -Path $outputDir } # Initialize an array to hold the attachment file paths $attachments = @() foreach ($server in $servers) { # Define the path for the output file $outputFile = Join-Path -Path $outputDir -ChildPath "$server.txt" $scriptBlock = { $tempOutputFile = "c:\temp\test.txt" start-process -filepath "mpclaim" -argumentlist "-v c:\temp\test.txt -n" -NoNewWindow -wait Get-Content -path $tempOutputFile } #Capture the output from the remote server $outputFromserver = Invoke-Command -ComputerName $server -ScriptBlock $scriptBlock #Write the output to the local file $outputFromserver | Out-File -FilePath $outputFile # Add the output file to the list of attachments $attachments += $outputFile } # Start the HTML content for the email $htmlContent = "<html><body><p>See attached files for server outputs.</p></body></html>" # Specify mail parameters $messageParameters = @{ Subject = 'Drive Info and Commands Output' Body = $htmlContent From = '' To = '' BodyAsHtml = $true SmtpServer = '' Attachments = $attachments } # Send the mail Send-MailMessage @messageParameters
本地保存的文本文件格式正常,示例如下:
MPIO Storage Snapshot on Tuesday, 15 April 2025, at 11:43:27.268 Registered DSMs: 2 ================ +--------------------------------|-------------------|----|----|----|---|-----+ |DSM Name | Version |PRP | RC | RI |PVP| PVE | |--------------------------------|-------------------|----|----|----|---|-----| |Microsoft DSM |010.0000.17763.0652|0005|0003|0001|030| True| |Dell PowerPath DSM |008.0000.00236.0000|0005|0003|0001|030| True| +--------------------------------|-------------------|----|----|----|---|-----+ Microsoft DSM ============= No devices controlled by this DSM at this time! Dell PowerPath DSM ================== MPIO Disk10: 04 Paths, Vendor-specific, ALUA Not Supported SN: Symm 000297600586 vol 020e Supported Load Balance Policies: FOO RR LQD LB VS Path ID State SCSI Address Weight --------------------------------------------------------------------------- 0000000080050100 Active/Optimized 005|000|001|010 0 Adapter: Emulex LightPulse HBA - Storport Mini... (B|D|F: 094|000|001) Controller: 000297600586 (State: No Controller)
但通过邮件发送后查看附件,文本每个字符间出现多余空格,示例如下:
M P I O S t o r a g e S n a p s h o t o n T u e s d a y , 1 5 A p r i l 2 0 2 5 , a t 1 1 : 4 3 : 1 4 . 6 5 3 R e g i s t e r e d D S M s : 2 = = = = = = = = = = = = = = = = + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | - - - - - - - - - - - - - - - - - - - | - - - - | - - - - | - - - - | - - - | - - - - - + | D S M N a m e | V e r s i o n | P R P | R C | R I | P V P | P V E | | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | - - - - - - - - - - - - - - - - - - - | - - - - | - - - - | - - - - | - - - | - - - - - | | M i c r o s o f t D S M | 0 1 0 . 0 0 0 0 . 1 7 7 6 3 . 0 6 5 2 | 0 0 0 5 | 0 0 0 3 | 0 0 0 1 | 0 3 0 | T r u e | | D e l l P o w e r P a t h D S M | 0 0 8 . 0 0 0 0 . 0 0 2 3 6 . 0 0 0 0 | 0 0 0 5 | 0 0 0 3 | 0 0 0 1 | 0 3 0 | T r u e | + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | - - - - - - - - - - - - - - - - - - - | - - - - | - - - - | - - - - | - - - | - - - - - + M i c r o s o f t D S M = = = = = = = = = = = = = N o d e v i c e s c o n t r o l l e d b y t h i s D S M a t t h i s t i m e ! D e l l P o w e r P a t h D S M = = = = = = = = = = = = = = = = = = M P I O D i s k 1 0 : 0 4 P a t h s , V e n d o r - s p e c i f i c , A L U A N o t S u p p o r t e d S N : S y m m 0 0 0 2 9 7 6 0 0 5 8 6 v o l 0 2 0 e S u p p o r t e d L o a d B a l a n c e P o l i c i e s : F O O R R L Q D L B V S P a t h I D S t a t e S C S I A d d r e s s W e i g h t - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 0 0 0 0 0 0 0 0 8 0 0 5 0 1 0 0 A c t i v e / O p t i m i z e d 0 0 5 | 0 0 0 | 0 0 1 | 0 1 0 0 A d a p t e r : E m u l e x L i g h t P u l s e H B A - S t o r p o r t M i n i . . . ( B | D | F : 0 9 4 | 0 0 0 | 0 0 1 ) C o n t r o l l e r : 0 0 0 2 9 7 6 0 0 5 8 6 ( S t a t e : N o C o n t r o l l e r ) 0 0 0 0 0 0 0 0 8 0 0 5 0 0 0 0 A c t i v e / O p t i m i z e d 0 0 5 | 0 0 0 | 0 0 0 | 0 1 0 0 A d a p t e r : E m u l e x L i g h t P u l s e H B A - S t o r p o r t M i n i . . . ( B | D | F : 0 9 4 | 0 0 0 | 0 0 1 ) C o n t r o l l e r : 0 0 0 2 9 7 6 0 0 5 8 6 ( S t a t e : N o C o n t r o l l e r )
请问该问题的原因是什么,如何解决?
原因分析
这种字符间出现空格的现象本质是编码不匹配:
- PowerShell的
Out-File命令默认使用UTF-16LE(Unicode)编码保存文件,每个ASCII字符会占用两个字节(高字节为0x00)。 - 邮件客户端解析附件时,误将UTF-16LE编码的文件当作单字节编码(如ASCII/UTF-8)读取,把每个字符的高字节(0x00)解析为空格,最终呈现出字符间插空格的效果。
解决方案
修改脚本中保存文件的代码,指定使用单字节编码(UTF-8或ASCII)即可解决问题,以下是两种可行方案:
方案1:修改Out-File的编码参数
将保存文件的代码替换为:
# 使用UTF-8编码保存 $outputFromserver | Out-File -FilePath $outputFile -Encoding utf8
如果内容不含非英文字符,也可以使用ASCII编码:
$outputFromserver | Out-File -FilePath $outputFile -Encoding ascii
方案2:改用Set-Content命令
Set-Content在PowerShell 5.1中默认使用ASCII编码,PowerShell 7+中默认使用UTF-8,也可显式指定编码:
$outputFromserver | Set-Content -Path $outputFile -Encoding utf8
额外优化:简化远程执行逻辑
可以直接捕获mpclaim的输出,无需生成临时文件,减少中间步骤:
$scriptBlock = { # 直接执行命令并返回输出 & mpclaim -v -n }
修改后,保存的文本文件使用单字节编码,邮件客户端解析时就不会出现字符间的多余空格。
内容的提问来源于stack exchange,提问作者snehasish nandy
相关产品推荐
相关产品推荐

