在写入主机中被覆盖的字符串变量位置



如果我运行下面的代码,$SRN可以写为输出或添加到另一个变量中,但尝试包含另一个变量或常规文本会导致它从行首被覆盖。我假设这与我最初分配$autocode和$SRN的方式有关,但无法说出它想要做什么。

# Load the property set to allow us to get to the email body.
$item.load($psPropertySet) # Load the data.
$bod = ($item.Body.Text -creplace '(?m)^s*r?n','') -split "n" # Get the body text, remove blank lines, split on line breaks to create an array (otherwise it is a single string).
$autocode = $bod[4].split('-')[2] # Get line 4 (should be Title), split on dash, look for 3rd element, this should contain our automation code.
$SRN = $bod[1] -replace 'ID: ','' # Get line 2 (should be ID), find and replace the preceding text.
# Skip processing if autocode does not match our list of handled ones.
if ($autocode -cin $autocodes)
{       
write-host "$SRN $autocode"
write-host "$autocode $SRN"
write-host "$SRN test"
$var = "$SRN $autocode"
$var
}

代码导致这种情况,您可以查看$SRN是否不在行首,则没问题。不确定多余的空格来自哪里:

KRNE8385
KRNE SR1788385
test8385
KRNE8385

我希望看到这个:

SR1788385 KRNE
KRNE SR1788385
SR1788385 test
SR1788385 KRNE

LotPings 为我指明了正确的道路,两个变量中仍然有"0D"或"\r"。我的正则表达式替换只是在空白行上摆脱它们,我只在""上拆分数组。将原始代码中的第 3 行更改为下面的行似乎已经解决了该问题。第一次看到格式十六进制,但它似乎非常适合解决此类问题。

$bod = ($item.Body.Text -creplace '(?m)^s*r?n','') -split "rn"

最新更新