Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

How to escape special characters in PowerShell

Ways to escape special characters in PowerShell are different between its context, and so complecated...

# Context Escaped char " ' \ ` ``
1 String with " "
2 String with " $
3 String with " '
4 String with " `
5 String with ' "
6 String with ' $
7 String with ' '
8 String with ' `
9 Operand of -like with " *
10 Operand of -like with ' *
11 Operand of -match with " *
12 Operand of -match with ' *
13 Placeholder of -F operator *1 { and }

legend

  • ✓ … You can use it to escape
  • ✘ … You cannot use it to escape
  • - … You don't have to escape

Invoke-RestMethod to GitLab API causes mojibake

Context

In PowerShell, You can call REST API with Invoke-RestMethod like:

Invoke-RestMethod -Headers $headers -Method Get -Timeout 10 -Uri "https://api.github.com/users/octocat/orgs"

Problem

The result of Invoke-RestMethod causes mojibake when the response contains non-ASCII characters like Japanese/Chinese characters.

Reason

  • Invoke-RestMethod treats the charset of the HTTP response as ISO-8859-1 rather than UTF-8, when the response doesn't have Content-Type: ... charset=utf-8 in HTTP header.
  • The response of GitLab API doesn't have Content-Type HTTP header.

Solution

Use Invoke-WebRequest rather than Invoke-RestMethod. You can decode the content of HTTP response as UTF-8 explicitly.

$res = (Invoke-WebRequest -Headers $headers -Method Get -Timeout 10-Uri "https://gitlab.example.com/api/v4/projects")
$con = [System.Text.Encoding]::Utf8.GetString([System.Text.Encoding]::GetEncoding("ISO-8859-1").GetBytes($res.Content))
ConvertFrom-Json $con

Format operator with string contains "{" and "}"

Context

In PowerShell, you can format a string with -F operator like:

  Get-Content foo.csv | ConvertFrom-Csv -Header Name,Code,Address | `
  ForEach-Object {
    "{0} `n {1} {2}" -F $_.Name,$_.Code,$_.Address
  }

Problem

-F operator returns error like below when the formatted string contains When "{" and "}".

  Get-Content foo.csv | ConvertFrom-Csv -Header Name,Code,Address | `
  ForEach-Object {
    '\addaddress{{0}}{様}{{1}}{{2}}' -F $_.Name,$_.Code,$_.Address
  }
Error formatting a string: Input string was not in a correct format..
At /home/xxxx/foobar.ps1:31 char:5
+     '\addaddress{{0}}{様}{{1}}{{2}}' -F $_.Name,$_.Code,$_.Address
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (\addaddress{0}{様}{1}{2}:String) [], RuntimeException
    + FullyQualifiedErrorId : FormatError

Solution

You should use "{{" and "}}" to output "{" and "}" with format operator, like:

  ForEach-Object {
    '\addaddress{{ {0} }}{{様}}{{ {1} }}{{ {2} }}' -F $_.Name,$_.Code,$_.Address
  }

How to see an AOP pointcut matches to a method signature

Situation

  • You are developing an Java application with Spring Framework.
  • You are trying to write an AOP pointcut to weave some logic into a method invocation, but the weaved logic doesn't seems be running.

Problem

Mistakes are sometimes in the method signature, sometimes in the pointcut notation, or sometimes you are just running and watching a wrong method to test the weaved logics. So you have to break down the cause of this problem, but (it seems) there is no formulated way to see whether an AOP pointcut matches to a method signature or not.

Solutions

There are some tools and procedures you can use:

  • You can use Eclipse debug perspective to see whether an aspect is weaved or not. Set a break point in the target method and run some test. If the aspect method is in the call stack view, it is weaved properly.

There are no properties for ordinary characters in PropList.txt

Problem

When run the script in next URL with PropList.txt on unicode.org, result file did not contain character properties for ordinary characters like 'x', 'y', or 'z'. http://satob.hatenablog.com/entry/2017/11/21/002957

Reason

PropList.txt does not cover all the characters in the Unicode Standard. The properties for all the characters are in Scripts.txt.

Note there are no mention for Scripts.txt on UNICODE CHARACTER DATABASE page.

Solution

Use Scripts.txt rather than PropList.txt.