Dynamic Key Value Pairs (Members): Unterschied zwischen den Versionen

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche
 
(4 dazwischenliegende Versionen des gleichen Benutzers werden nicht angezeigt)
Zeile 4: Zeile 4:
  
 
<source lang="powershell">
 
<source lang="powershell">
# 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 to loop over all properties and add the Keys and Values to the "Template" PSCustomObject
 
Function Add-DynamicKeyValues {
 
Function Add-DynamicKeyValues {
Zeile 21: Zeile 13:
 
         [System.Object]$InputObject,
 
         [System.Object]$InputObject,
  
         [Parameter(Mandatory=$false,HelpMessage="Object with multiple properties")]
+
         [Parameter(Mandatory=$false,HelpMessage="Path where the key & value pairs are added")]
 
         [string]$ObjectPath = "Metrics",
 
         [string]$ObjectPath = "Metrics",
  
Zeile 41: Zeile 33:
 
}
 
}
  
 +
 +
# Define PSCustomObject as a "Template" for the structure
 +
$Result = [PSCustomObject]@{
 +
    PSTypeName = "Metric"
 +
    Measure = "asset_appliance_oneview"
 +
    Metrics = @{}
 +
}
  
 
# Use the function
 
# Use the function
Zeile 48: Zeile 47:
  
  
== Result as JSON Object to get the depth ==
+
=== Result as JSON Object to get the depth ===
 
<pre>
 
<pre>
 
{
 
{
Zeile 66: Zeile 65:
 
}
 
}
 
</pre>
 
</pre>
 +
 +
 +
 +
== With Type Conversion ==
 +
''If you want using this function in a monitoring context where the data type is important, you can add the datatype and remove units "MB, GB, .."''. <br>
 +
 +
*Replace following line in the function above:
 +
<source lang="powershell">$Result.$ObjectPath.Add(($_InputObject.($Key) + $KeySuffix).ToLower(), $_InputObject.($KeyValue))</source>
 +
*With following line:
 +
<source lang="powershell">$Result.$ObjectPath.Add(($_InputObject.($Key) + $KeySuffix).ToLower(), [single]($_InputObject.($KeyValue) -replace "(\d+\.*\d*).*", '$1'))</source>
 +
 +
 +
 +
 +
[[Kategorie:PowerShell]]

Aktuelle Version vom 22. Oktober 2021, 13:12 Uhr

In the following example I create a PowerShell object with dynamic Key Value Pairs of the first 10 files in the path "C:\Windows".
The key is filename "Name" and keyvalue is the size "Length".


# 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="Path where the key & value pairs are added")]
        [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))
        }
    }
}
 
 
# Define PSCustomObject as a "Template" for the structure
$Result = [PSCustomObject]@{
    PSTypeName = "Metric"
    Measure = "asset_appliance_oneview"
    Metrics = @{}
}
 
# Use the function
$Files = (Get-ChildItem -Path "C:\Windows" -File | Select-Object -First 10)
Add-DynamicKeyValues -result $result -InputObject $Files -ObjectPath "Metrics" -Key "Name" -KeyValue "Length"


Result as JSON Object to get the depth

{
    "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
                }
}


With Type Conversion

If you want using this function in a monitoring context where the data type is important, you can add the datatype and remove units "MB, GB, ..".

  • Replace following line in the function above:
$Result.$ObjectPath.Add(($_InputObject.($Key) + $KeySuffix).ToLower(), $_InputObject.($KeyValue))
  • With following line:
$Result.$ObjectPath.Add(($_InputObject.($Key) + $KeySuffix).ToLower(), [single]($_InputObject.($KeyValue) -replace "(\d+\.*\d*).*", '$1'))