Lazy Diary @ Hatena Blog

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

Entries from 2017-03-01 to 1 month

Characters treated as a part of a variable name

Context: You can expand value of variables when you write variable name in double-quoted strings like: > $var = "123" > Write-Host "$var" 123 Problem: Some characters are treated as a part of a variable name, so you can’t use these charact…

You can't expand array[index] in a string

Context: You can expand variables in a string like: > $var = "123" > Write-Host "$var" 123 > Problem: You can’t expand array with index: > $arr = @("456", "789") > Write-Host "$arr[0]" 456 789[0] It is also ineffective to add braces: > Wri…

The creator of branch is not contained in return values of GitHub API

Context: On GitHub, you can see which branches have you created (see “Your branches” section of “Branches” page). Problem: In GitHub API, there is no way to get the creator of the branches. The return values of GitHub References API don’t …

powershell -Version is unsupported in Linux environment

Context: In Windows environment, you can use -Version option to get Powershell pretend to be an older version. Problem: In Linux environment, -Version option is unsupported. Reason: manpage of powershell(1) doesn’t have any content about -…

Size field is folded when the size > 10GB in Get-ChildItem

Context: You use Powershell version 4 or earlier. You have a file that the size is > 10GB. Problem: Size field is folded when you Get-ChildItem to the file that the size is > 10GB. Mode LastWriteTime Length Name ---- ------------- ------ -…

Ways to convert relative paths to absolute paths

Some methods (e.g. System.IO.File.WriteAllLines()) needs absolute paths for parameters, so you have to convert from relative paths to absolute paths. In PowerShell, there is more than one way to do it ($RelativePath contains relative path …

Return value of Invoke-RestMethod or ConvertFrom-Json looks like a list but it's not a list

Context: Return value of Invoke-RestMethod looks like a list of objects: > $headers = @{ Accept = "application/json"; Authorization = "Bearer 0123456789abcdef0123456789abcdef01234567"; } > Invoke-RestMethod -Headers $headers -Method Get -U…

Delombok removes super(); in default constructors

Context: You have a class that has one or more constructors other than default constructor, so you have to define the default constructor explicitly. You use Lombok to generate some methods/fields. You use static code analyzers that warn e…

You can't access HTTP responce headers with Invoke-RestMethod

Context: You can call API of web services (e.g. GitHub) with Invoke-RestMethod. GitHub pagination API contains the last page number in HTTP responce headers. Problem: You can’t access HTTP responce headers with Invoke-RestMethod or the ret…

If you pass an empty string to ConvertFrom-Json

According to the spec of JSON, an empty string is not a valid JSON. If you pass an empty string to ConvertFrom-Json, it returns $null (doesn’t raise an exception). If you pass an empty JSON (“[]”) to ConvertFrom-Json, it returns empty list…

Set-Variable doesn't overwrite a variable when an empty list passed through pipe

Context: You can set a value to variable with Set-Variable. > "foo" | Set-Variable var > $var foo You can also overwrite a value of variable like this: > "foo" | Set-Variable var > $null | Set-Variable var > $var Problem: The value of vari…

ConvertFrom-Json with empty JSON returns an empty list

Context: You want to receive an JSON and check whether the JSON is empty or not. Problem: You cannot check empty JSON with -eq operator, and -eq doesn’t return true/false. > $hoge = (Get-Content ./empty.json | ConvertFrom-Json) > $hoge -eq…

Difference between Remove-Item -Force and rm -f

Context: You want to remove a file with Remove-Item. You want to ignore errors even if the file doesn’t exist. Problem: You get an error when execute Remove-Item -Force in the file that does not exist. Reason: The behavior is by design. rm…

Use "`n" (not "`r`n") to split here document in PowerShell

Context: You have to write "`r`n" to write CRLF to a file. You can make here document (multi-line string literals) with @‘~’@. Problem: Some articles in web use "`r`n" to split here document in PowerShell like: @' abcde fghij '@.Split("`r`…