Skip to content

Instantly share code, notes, and snippets.

@gsscoder
Last active April 6, 2020 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsscoder/f4d19b9c7cf69fa2c4a9c235bf7029db to your computer and use it in GitHub Desktop.
Save gsscoder/f4d19b9c7cf69fa2c4a9c235bf7029db to your computer and use it in GitHub Desktop.
Properly way to test object properties existence in PowerShell with strict mode activated
Set-StrictMode -Version Latest
function Test-Property([Parameter(ValueFromPipeline)] $object, [string] $name) {
try { $name -cin $object.PSObject.Properties.Name }
catch { $false }
}
$path = '/Users/someone/temp/file.json'
$json = $path | Get-Content -Raw | ConvertFrom-Json
[bool] $exists = $json.someKey | Test-Property -name 'someOtherKey'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment