Skip to content

Instantly share code, notes, and snippets.

@SteveL-MSFT
Last active March 29, 2022 21:22
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 SteveL-MSFT/e5ecd21b8ec75b36082710695b5cfa20 to your computer and use it in GitHub Desktop.
Save SteveL-MSFT/e5ecd21b8ec75b36082710695b5cfa20 to your computer and use it in GitHub Desktop.
param(
[Parameter(ValueFromPipeline)]
[string]$json,
[Parameter(Mandatory,Position=0)]
[string]$find
)
$obj = ConvertFrom-Json -InputObject $json -Depth 100
function Test-Member ($prop, $text) {
Write-Verbose "Checking $($prop.Name) = $($prop.TypeNameOfValue) = $($prop.MemberType)"
if ($prop.TypeNameOfValue -eq 'System.Object' -or $prop.TypeNameOfValue -eq 'System.Management.Automation.PSCustomObject') {
$newObj = @{}
if ($prop.TypeNameOfValue -eq 'System.Management.Automation.PSCustomObject') {
$properties = $prop.Value.psobject.properties
}
else {
$properties = $prop.psobject.properties
}
if ($null -ne $prop.Value) {
foreach ($child in $properties) {
$found, $property, $value = Test-Member $child $text
if ($true -eq $found) {
$newObj += @{ $property = $value }
}
}
if ($newObj.count -gt 0) {
return $true, $prop.Name, $newObj
}
else {
return $false, $null, $null
}
}
elseif ($prop.Name -like "*$text*") {
return $true, $prop.Name, $null
}
else {
return $false, $null, $null
}
}
elseif ($prop.TypeNameOfValue -eq 'System.Object[]') {
Write-Verbose "Array $($prop.Name) = $($prop.TypeNameOfValue)"
$newArr = [System.Collections.Generic.List[object]]::new()
foreach ($element in $prop.Value) {
if ($element.GetType().Name -eq 'PSCustomObject') {
$newObj = @{}
foreach ($child in $element.psobject.properties) {
$found, $property, $value = Test-Member $child $text
if ($true -eq $found) {
$newObj += @{ $property = $value }
}
}
if ($newObj.Count -gt 0) {
$newArr.Add($newObj)
}
}
else {
if ($element.ToString() -like "*$text*") {
$newArr.Add($element)
}
}
}
if ($newArr.Count -gt 0) {
return $true, $prop.Name, $newArr
}
else {
return $false, $null, $null
}
}
else {
$value = $null
if ($null -ne $prop.Value) {
$value = $prop.Value.ToString()
}
if ($prop.Name -like "*$text*" -or $value -like "*$text*") {
return $true, $prop.Name, $prop.Value
}
}
return $false, $null, $null
}
$returnObj = @{}
foreach ($prop in $obj.psobject.properties) {
Write-Verbose "Initial check $($prop.Name) = $($prop.TypeNameOfValue)"
$found, $property, $value = Test-Member $prop $find
if ($true -eq $found) {
$returnObj += @{ $property = $value }
}
}
$returnObj | ConvertTo-Json -Depth 100 | Select-String $find -AllMatches
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment