Ma Hu
Ma Hu

Reputation: 1

Just get some content out of a Json File

I need to get only some Information out of my JSON content, but with normal select-object and where-object my PowerShell prompt gives my nothing.

What I do:

I get a JSON output from a webpage and then only need the .Content.

$get_all_attributes = $result.Content | Out-String | ConvertFrom-Json | Select Attributes

When asking PowerShell to give me one Particular Object like $get_all_attributes.Attributes.Slot1 everything is fine.

But now I need to get all Slots (Slot1 - SlotX) without the Bif (eg Slot1 but not Slot1Bif). Afterwards I like to find all disabled ones. But for now I even do net get the Slots.

I converted it in some ways from and to Json with String and whatever but now I'm kinda stuck.

Nice looking JSON

{
"Attributes":  {
                   "AcPwrRcvry":  "Last",
                   "AcPwrRcvryDelay":  "Immediate",
                   "AesNi":  "Enabled",
                   "AssetTag":  "",
                   "BootMode":  "Uefi",
                   "BootSeqRetry":  "Enabled",
                   "CollaborativeCpuPerfCtrl":  "Disabled",
                   "ConTermType":  "Vt100Vt220",
                   "ControlledTurbo":  "Disabled",
                   "Slot1":  "Enabled",
                   "Slot1Bif":  "DefaultBifurcation",
                   "Slot2":  "Enabled",
                   "Slot2Bif":  "DefaultBifurcation",
                   "Slot3":  "Enabled",
                   "Slot3Bif":  "DefaultBifurcation",
                   "Slot4":  "Enabled",
                   "Slot4Bif":  "DefaultBifurcation",
                   "Slot5":  "Enabled",
                   "Slot5Bif":  "DefaultBifurcation",
                   "Slot6":  "Enabled",
                   "Slot6Bif":  "DefaultBifurcation",
                   "Slot7":  "Enabled",
                   "Slot7Bif":  "DefaultBifurcation"
               }
}

My Converted Stuff

$get_all_attributes | FL

Attributes : @{AcPwrRcvry=Last; AcPwrRcvryDelay=Immediate; AesNi=Enabled; AssetTag=; BootMode=Uefi; BootSeqRetry=Enabled; CollaborativeCpuPerfCtrl=Disabled; 
         ConTermType=Vt100Vt220; ControlledTurbo=Disabled; CorrEccSmi=Enabled; CpuInterconnectBusLinkPower=Enabled; CurrentEmbVideoState=Enabled; 
         DcuIpPrefetcher=Enabled;Slot1=Enabled; Slot1Bif=DefaultBifurcation; Slot2=Enabled; Slot2Bif=DefaultBifurcation; Slot3=Enabled; Slot3Bif=DefaultBifurcation; Slot4=Enabled; 
         Slot4Bif=DefaultBifurcation; Slot5=Enabled; Slot5Bif=DefaultBifurcation; Slot6=Enabled; Slot6Bif=DefaultBifurcation; Slot7=Enabled; 
         Slot7Bif=DefaultBifurcation}

Upvotes: 0

Views: 76

Answers (2)

em.h
em.h

Reputation: 71

You're almost there, just use the switch "ExpandProperty".

$get_all_attributes = $result.Content | Out-String | ConvertFrom-Json | Select -ExpandProperty Attributes

After, this, the easiest way is to simply select the property you're interested in to get all the fields... $get_all_attributes.Attributes.BootSeqRetry

... or get more granular for a specific sub-property:

$get_all_attributes.Attributes.BootSeqRetry

(In this case, it returns Enabled)

Upvotes: 1

rufer7
rufer7

Reputation: 4119

The following code should solve your problem.

$attributes = $get_all_attributes.Attributes;
$filteredAttributes = $attributes | Select-Object -Property "slot*" -ExcludeProperty "*Bif";
$slots = @{};
$filteredAttributes.psobject.properties | Foreach { $slots[$_.Name] = $_.Value };
$disabledSlots = $slots.GetEnumerator() |? Value -eq "Disabled";

Upvotes: 0

Related Questions