Dynamic Key Value Pairs (Members): Unterschied zwischen den Versionen
Aus Wiki-WebPerfect
Admin (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „ <source lang="powershell"> # Define PSCustomObject as a "Template" for the structure $Result = [PSCustomObject]@{ PSTypeName = "Metric" Measure = "ass…“) |
Admin (Diskussion | Beiträge) |
||
Zeile 41: | Zeile 41: | ||
# Use the function | # Use the function | ||
Add-DynamicKeyValues -result $result -InputObject (Get-ChildItem -Path "C:\Windows" -File | Select-Object -First 10) -ObjectPath "Metrics" -Key "Name" -KeyValue "Length" | Add-DynamicKeyValues -result $result -InputObject (Get-ChildItem -Path "C:\Windows" -File | Select-Object -First 10) -ObjectPath "Metrics" -Key "Name" -KeyValue "Length" | ||
+ | </source> | ||
+ | |||
+ | |||
+ | == Result as JSON Object to get the depth == | ||
+ | <source lang="json" | ||
+ | { | ||
+ | "Measure": "asset_appliance_oneview", | ||
+ | "Metrics": { | ||
+ | "bfsvc.exe": 82944, | ||
+ | "dfsradmin.exe.config": 1315, | ||
+ | "hh.exe": 18432, | ||
+ | "dfsradmin.exe": 232960, | ||
+ | "explorer.exe": 4388592, | ||
+ | "mib.bin": 43131, | ||
+ | "lsasetup.log": 1380, | ||
+ | "helppane.exe": 1072128, | ||
+ | "bootstat.dat": 67584, | ||
+ | "dtcinstall.log": 4056 | ||
+ | } | ||
+ | } | ||
</source> | </source> |
Version vom 22. Oktober 2021, 11:25 Uhr
# Define PSCustomObject as a "Template" for the structure $Result = [PSCustomObject]@{ PSTypeName = "Metric" Measure = "asset_appliance_oneview" Metrics = @{} } # Function to loop over all properties and add the Keys and Values to the "Template" PSCustomObject Function Add-DynamicKeyValues { param( [Parameter(Mandatory=$true,HelpMessage="PSCustomObject template and result")] [PSCustomObject]$Result, [Parameter(Mandatory=$true,HelpMessage="Object with multiple properties")] [System.Object]$InputObject, [Parameter(Mandatory=$false,HelpMessage="Object with multiple properties")] [string]$ObjectPath = "Metrics", [Parameter(Mandatory=$true,HelpMessage="Property to extract as the Key")] [string]$Key, [Parameter(Mandatory=$false,HelpMessage="String to add as suffix to the Key name")] [string]$KeySuffix, [Parameter(Mandatory=$true,HelpMessage="Property to extract as the Value")] [string]$KeyValue ) Process { Foreach ($_InputObject in $InputObject) { $Result.$ObjectPath.Add(($_InputObject.($Key) + $KeySuffix).ToLower(), $_InputObject.($KeyValue)) } } } # Use the function Add-DynamicKeyValues -result $result -InputObject (Get-ChildItem -Path "C:\Windows" -File | Select-Object -First 10) -ObjectPath "Metrics" -Key "Name" -KeyValue "Length"
Result as JSON Object to get the depth
Ungültige Sprache.
Die gewünschte Sprache muss wie folgt definiert werden: <source lang="html4strict">...</source>
Unterstützte Sprachen für die Syntaxhervorhebung: