PowerShell console host displays strings inconsistently - powershell

When I run this in PowerShell ISE, both strings are displayed identically.
When I run it in the console host, string $a is missing the empty line.
Why is that, and can I work around it?
I want to format blocks of text without using `n
$a = "there should be one emtpy line
between these two lines of text"
$b = "there should be one emtpy line`n`nbetween these two lines of text"
$a
$b

If you type that into the console host, you're correct; it won't work as expected. But if you save it in a file, then it does work as expected (the blank line is preserved).
I believe that this is because of the way the console host is using blank lines to interpret your input, since it needs to detect blank lines to tell when to stop input in some cases.
I don't know enough to know if it's a bug.
Something else that may not be clear is that when you write the way you're assigning it to $a, you're actually inserting CR and LF ("`r`n") when in the ISE host, whereas with $b you're just using LF. In the console host, a LF only is used.
To see that, I like to use [RegEx]::Escape:
[RegEx]::Escape($a)
[RegEx]::Escape($b)
Using this I was able to confirm that using the console host to enter the strings, the second LF is actually not present in the $a string; it's not simply a display issue.

Related

I have a problem trying to put single quotes around a text

I have a password that has special character with an exclamation point.
ABcD!987zyz12388
I'd like to put single quotes around it. This password is fetched and saved to a variable.
$mypass=`fetchpwd $sourceserver $loginacct`;
$mypass="'$mypass'";
print "My password is: $mypass\n";
The return looks like this
My Password is 'ABcD!987zyz12388
'
The end single quote went on the next line. How can I have the last single quote right after the last 8 to something like this 'ABcD!987zyz12388'
Use chomp to remove the newline character which is added by your fetchpwd command. Do this before you add single quotes.
$mypass=`fetchpwd $sourceserver $loginacct`;
chomp $mypass;
$mypass="'$mypass'";
print "My password is: $mypass\n";
You have a good answer already, but I wanted to share a little programmer trick that might have helped you work out what the problem was by yourself.
You thought that it was adding the quote to your string that was causing the problem. And your program demonstrates that:
$mypass=`fetchpwd $sourceserver $loginacct`;
$mypass="'$mypass'";
print "My password is: $mypass\n";
When you're investigating data being "corrupted" like this, one good first step is to identify exactly when the corruption occurs. And you can do that by printing your data whenever there's a chance that it changes. In this case, it's a good idea to print $mypass as soon as it is first set and before you do anything with it:
$mypass=`fetchpwd $sourceserver $loginacct`;
print "My password is: $mypass\n";
$mypass="'$mypass'";
print "My password is: $mypass\n";
Now, that would have given you two newline characters (the one you explicitly print, but also the extra one that you get from fetchpwd - the one that is causing your problems. And sometimes, two consecutive newlines are hard to spot. So it's a good idea to put characters around values that you're trying to print. I like to use <...>:
$mypass=`fetchpwd $sourceserver $loginacct`;
print "My password is: <$mypass>\n";
$mypass="'$mypass'";
print "My password is: <$mypass>\n";
That would have shown you that the extra newline was present right from the start and, therefore, you adding quotes around it wasn't what was corrupting it. And, hopefully, it would have led to you investigating exactly what was returned by fetchpwd.

how to extract strings in between period, asterisk and words

I have this powershell script. The idea is to generate a report but the I need to split the string on "certificate.common_name" and put on separate column.
here's my script, and this works okay, however, I cannot assume the client code will always start in position 7.
$WriteStr = $order.certificate.common_name, $order.date_created, $order.certificate.days_remaining, $order.certificate.common_name.Substring(7,2), $Tag -join, ","
so basically, I want to get "1z" on this string(certificate.common_name) "*.test.1z.sample.com"

Print back quote in Powershell

I need to print back quotes ` between values in PowerShell. I'm trying to do this because I am sending data to Azure DevOps and need certain values to be marked as code (it uses Markdown).
For example:
$names = #("A","B","C")
$names should print just how the letter B had the back quote first
`A, `B, `C
I've tried adding the back quote in several different ways to display it but nothing works
`$_.names
'\
Can printing the back-quote even be achieved?

String variable position being overwritten in write-host

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
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"

Xcode breakpoint shell command argument length

Trying to pass a large string to a shell script using a breakpoint in Xcode
let value = Array(repeating: "a", count: 1500).joined()
let string = "{\"key\": \"\(value)\"}"
Unfortunately, the string is being truncated. Is this limitation documented and can it be overcome?
It's been nearly a year since you asked this, and I'm not sure if it will solve your question, but I've recently had a similar problem so thought I'd share my solution.
I had two issues:
LLDB was truncating any arguments to my shell script (and string variables printed in the console using po foo) to 1023 characters. I believe this is the issue to which your question relates.
Xcode was incorrectly confusing a comma , in my string as a separator for multiple arguments (e.g. passing foo, bar, and baz as arguments to the script wouldn't work correctly if any of the variables contained a , as Xcode would try to create another argument).
So, firstly, the LLDB issue...
It seems that by default LLDB has a limit on the character length that it will print to the console (or pass to a shell script via a breakpoint argument) of around 1023 characters. You can easily change this to something larger by setting another breakpoint before the breakpoint that uses your variable and running (lldb) set set target.max-string-summary-length 10000 in the console. This can be a bit annoying so I created a ~/.lldbinit file and placed set set target.max-string-summary-length 10000 in there instead so I don't have to keep setting it in the console.
Secondly, the comma issue...
Inside the Edit breakpoint... menu that you provided a screenshot of above there is the option to not only provide a path to a script but to also provide arguments. I can see from your question that you provided the argument #string#. For my script, I was passing multiple arguments, which Xcode allows you to do using a comma separated list, e.g. #foo#, #bar#, #baz#. Each of these arguments was a string.
I noticed that sometimes one or more of these strings would truncate if they contained a comma: ,.
So the string:
{ "num_friends" : "7" }
would be passed to my script as expected. But the string:
{ "num_friends" : "7", "num_deleted_friends" : "1" }
would truncate and would be passed to my script as two separate arguments. It seems that Xcode would split any string with a , even when entered using #string#.
I validated this in my script by simply using something like:
for var in "$#"
do
echo "$var"
echo "===="
done
Where $# expands to contain each argument. From this I could see that #string# was being correctly passed to my script but separated as multiple arguments wherever there was a ,. So if #string# contained a comma my script would print:
#"{ \"num_friends\" : \"7\"
====
\"num_deleted_friends\" : \"1\" }"
instead of what I expected which was:
#"{ \"num_friends\" : \"7\", \"num_deleted_friends\" : \"1\" }"
So it seems like it might be a bug in how Xcode passes strings inside # expressions in the breakpoint editor window.
My crude solution has been to just replace any commas with another character and then replace them back again inside my script. There's probably a better way to do this but I don't require it for my needs.