1

If I run the below code, $SRN can be written as output or added to another variable, but trying to include either another variable or regular text causes it to be overwritten from the beginning of the line. I'm assuming it's something to do with how I'm assigning $autocode and $SRN initially but can't tell what it's trying to do.

# 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
}

The code results in this, you can see if $SRN isn't at the start of the line it is fine. Unsure where the extra spaces come from either:

 KRNE8385
KRNE SR1788385
 test8385
 KRNE8385

I would expect to see this:

SR1788385 KRNE
KRNE SR1788385
SR1788385 test
SR1788385 KRNE
4
  • Please provide the input data. Commented May 29, 2018 at 15:57
  • 1
    To me it looks like there are still line feeds/carriage returns in the strings. What does $SRN| Format-Hex or $autocode|Format-Hex return? If there are 0A or 0D try $SRN -replace "`r?`n?","" Commented May 29, 2018 at 16:47
  • LotPings was correct, $SRN had an extra 0D. Is there a reason {-creplace "(?m)^\s*\r?\n",""} would not have caught it originally? Also as a point of interest, replacing double quotes with single quotes in what you wrote makes that stop working, but converting my single quotes to doubles has no effect. Commented May 29, 2018 at 17:38
  • I'd try examining $bod|formt-hex and eventually my escaping variant with a backtick. Commented May 29, 2018 at 17:46

1 Answer 1

1

LotPings pointed me down the right path, both variables still had either "0D" or "\r" in them. My regex replace was only getting rid of them on blank lines, and I split the array on "\n" only. Changing line 3 in the original code to the below appears to have resolved the issue. First time seeing Format-Hex, but it appears to be excellent for troubleshooting such issues.

$bod = ($item.Body.Text -creplace '(?m)^\s*\r?\n','') -split "\r\n"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.