You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何部分字符串操作失效?PowerShell邮件日志格式异常排查

问题:PowerShell邮件日志格式异常排查

场景描述

编写PowerShell脚本记录操作日志,当任务失败时向团队发送邮件通知。其中邮件正文中的3项任务日志,仅有1项显示格式异常(所有行连在一起),另外2项正常换行。

复现代码

日志定义与组装

$changeLog1 = "Failed to disable and/or move AD account
Failed to remove all groups from AD account
Failed to remove all groups and disable AD admin account
Failed to disable AzureAD account
Failed to disable AzureAD admin account
Failed to remove extension attribute fields in AD
Failed to disable contact in USD
User has no devices in CMDB under his contact"

$changeLog2 = "Found CIs linked to user: 
Successfully used manager from AD
Failed to send notification mail to manager"

$changeLog3 = "Failed to obtain all user aliases
Failed to obtain all users from Kanban
User not found in Kanban
Failed to disable the user in Kanban-STAID"

$emailLog = @()

$emailLog += "Processing change: $($openTicket.change_ref)`r`n`r`n"
$emailLog += $changeLog1 + "`r`n`r`n"
$emailLog += $changeLog2 + "`r`n`r`n"
$emailLog += $changeLog3 + "`r`n`r`n"

发送邮件函数

function sendMailSD {
    param ($log, $list)
    $smtp = ""
    $from = ""
    $to = ""
    $cc = ""
    $subject = "EmployeeOUT automation failed on $(Get-Date -Format "dd/MM/yyyy")"
    $body = "Hello all,

EmployeeOUT automation failed for the following changes:

$($list -join "`n")

The script run log file is the following

$(($log.replace("
",".,") -replace "," "`n").replace("."," "))

Please assist with processing what automation failed to do.
Thank you!"

    Send-MailMessage -SmtpServer $smtp -To $to -From $from -cc $cc -Subject $subject -Body $body -Priority high
}

异常邮件显示结果

Processing change:  
 
Failed to disable and/or move AD account Failed to remove all groups from AD account Failed to remove all groups and disable AD admin account Failed to disable AzureAD account Failed to disable AzureAD admin account Failed to remove extension attribute fields in AD Failed to disable contact in USD User has no devices in CMDB under his contact 
 
Found CIs linked to user:  
Successfully used manager from AD
Failed to send notification mail to manager 
 
Failed to obtain all user aliases
Failed to obtain all users from Kanban
Failed to disable the user in Kanban
Failed to disable the user in Kanban-STAID 

问题根源

  1. 数组隐式转换破坏格式:$emailLog是数组类型,当传递给sendMailSD的$log参数后,执行-replace操作时,PowerShell会自动将数组转换为字符串,数组元素之间用空格分隔,导致$changeLog1所在的元素内部换行被破坏。
  2. 换行符处理不统一:你使用的replace("n",".,")仅替换LF换行符(n),若$changeLog1中的换行符为CRLF(rn,Windows默认)则无法被匹配,后续替换逻辑无法还原换行,最终导致所有行连在一起。
  3. 冗余替换逻辑引入风险:绕弯的替换步骤(换行→.,→逗号→换行→点→空格)完全没必要,反而放大了格式转换的不确定性。

解决方案

1. 先将数组转为标准字符串

调用函数时,先把数组用标准Windows换行符(rn`)连接成单个字符串,避免隐式转换的空格分隔:

sendMailSD -log ($emailLog -join "`r`n") -list $yourListVariable

2. 简化换行符处理逻辑

统一将所有类型的换行符(LF/CRLF)替换为rn`,确保邮件客户端能正确解析:

# 在sendMailSD函数中替换日志处理部分
$formattedLog = $log -replace "`r?`n", "`r`n"

3. 优化后的完整函数

function sendMailSD {
    param ($log, $list)
    $smtp = ""
    $from = ""
    $to = ""
    $cc = ""
    $subject = "EmployeeOUT automation failed on $(Get-Date -Format "dd/MM/yyyy")"
    
    # 统一处理所有换行符为CRLF
    $formattedLog = $log -replace "`r?`n", "`r`n"
    # 格式化任务列表
    $formattedList = $list -join "`r`n"

    $body = @"
Hello all,

EmployeeOUT automation failed for the following changes:

$formattedList

The script run log file is the following

$formattedLog

Please assist with processing what automation failed to do.
Thank you!
"@

    Send-MailMessage -SmtpServer $smtp -To $to -From $from -Cc $cc -Subject $subject -Body $body -Priority High
}

4. 额外优化:直接用字符串存储日志

避免使用数组,直接用单个字符串变量组装日志,彻底消除隐式转换问题:

$emailLog = "Processing change: $($openTicket.change_ref)`r`n`r`n"
$emailLog += $changeLog1 + "`r`n`r`n"
$emailLog += $changeLog2 + "`r`n`r`n"
$emailLog += $changeLog3 + "`r`n`r`n"

内容的提问来源于stack exchange,提问作者Adelin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 08:53:10