19 March 2022

PowerShell Escape

Everyone need to escape their PowerShell sometimes
especially if a string contains $ or “

David Moss 2022

The here-string

PowerShell has a special construct for multi-line strings called the here-string

$thing = @"
$greeting = "Hello World"
write-output ($greeting)

Above a variable ($thing) is declared and initialised with a multi-line String.
Note the first thing after the = sign is an @ character followed by a double-quote character.
Also note there is nothing else on that line.
The here-string continues across multiple lines and can contain any character sequence, even quote characters.
The here-string is terminated by a double-quote character followed by an @ character on a line by themselves.

The here-string is very useful but it can be tricky. After entering the above in a script, running the command:

write-output $thing

results in

 = "Hello World"
write-output ()

which is not what most people expect. Even worse, trying to use the here-string to construct a scriptblock results in a nasty error:

Invoke-Command -ScriptBlock ([ScriptBlock]::Create($thing))

results in:

Line |
   8 |  Invoke-Command -ScriptBlock ([ScriptBlock]::Create($thing))
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "Create" with "1" argument(s): "At line:2 char:15 + write-output (); +               ~ An expression was expected after '('."

Now that is an obscure error! It is caused by not ‘escaping’ the special characters in the here-string properly.
The escape character in PowerShell is the grave character, `. That is not a flyspeck before the full stop character in the last sentence. It is the grave character, or ‘back-tick’, often found around the top left of the keyboard. On the Mac keyboard it shared a key with the tilde character, ~. Lets try again with escaping:

$thing = @"
`$item = `"hi there`";
write-output (`$item);

Write-Output ($thing)

Invoke-Command -ScriptBlock ([ScriptBlock]::Create($thing))

which results in:

$item = "hi there";
write-output ($item);
hi there

I have escaped the $ character and the ” characters inside the here-string with the ` character.

26 April 2020


Earlier in the week my iPhone had to be sent away for repair. It is back now, but it was a pain that I could not access work VPN as it sent a verification code by SMS.

Today set up my Twilio account to forward received SMS messages sent to my Twilio phone number to my e-mail account.

I tested it just now:

“Hey Siri, SMS my Twilio Account”.
> OK what would you like to say?
“Hi there Twilio”
> Your message is “Hi there Twilio, would you like to send it?
> OK, it’s sent.

  • Siri used my iPhone to send an SMS to my Twilio number.
  • Twilio received the SMS and sent the details in a web request to my website on Amazon.
  • My web server used PHP to talk to my WordPress instance and send an e-mail to my Google account.
  • My Google account received the e-mail and pushed a notification to all my connected devices.
  • The text the SMS was now available on all my devices.

Now if I lose my iPhone I can get work to change my VPN verification number to my Twilio number. When VPN sends a verification code it appears in my e-mail. Or I could just use Twilio instead of my own iPhone number and use VPN from anywhere even if I don’t have my iPhone with me.

15 April 2020

Wolfram One

In the latter part of my long service leave, restricted by social distancing rules, I decided to try the Wolfram language again.

Wolfram One looked interesting. I am entitled to student pricing so it looked like a great way to learn Mathematica and the Wolfram Language. A 15 day free trial made it even more interesting.

Wolfram One combines the web based Wolfram environment with a local desktop version for use on a computer. The “One” is a designation to indicate the convergence of Mathematica, Wolfram Alpha and web based notebooks and APIs.

To be honest I found Wolfram frustrating, but I persevered and eventually crafted a notebook version of my Domestic Violence Policy model. The notebook version can be found in my Wolfram Cloud Repository.

It was a little disappointing this published notebook requires the reader to have a Wolfram account for interaction, but Stephen Wolfram has to make money somehow or there would be no point in providing the computing resources behind the site.

I did paid for a one year subscription to Wolfram One after the free trial so I will use the time to learn the language and see what it offers.