Forked from Olwiba/Microsoft.Powershell_profile.ps1
Created
March 5, 2024 02:28
Get-Json Property - Powershell ⚡
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# catjp - Concatenate JSON property | |
# Made with 💖 - Olwiba | |
function Get-JsonProperty { | |
param ( | |
[Parameter(Mandatory=$true)] | |
[string]$Path, | |
[Parameter(Mandatory=$true)] | |
[string]$Property | |
) | |
if (-Not (Test-Path $Path)) { | |
Write-Host "🛑 File not found at '$Path'." -ForegroundColor Red | |
return | |
} | |
try { | |
$jsonContent = Get-Content $Path | ConvertFrom-Json | |
if ($jsonContent.PSObject.Properties.Name -contains $Property) { | |
Write-Host "{" | |
$jsonContent.$Property.PSObject.Properties | ForEach-Object { | |
Write-Host -NoNewline (" $($_.Name): ") -ForegroundColor Green | |
Write-Host "`"$($_.Value)`"" | |
} | |
Write-Host "}" | |
} else { | |
Write-Host "🥲 Property '$Property' not found in JSON file at '$Path'." -ForegroundColor Yellow | |
} | |
} catch { | |
Write-Host "🚫 Failed to parse JSON from file at '$Path'. Ensure the file is a valid JSON document." -ForegroundColor Red | |
} | |
} | |
Set-Alias catjp Get-JsonProperty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment