Search code examples
powershellazure-powershellazure-appservice

Powershell - save value from hashtable to a @{}


i have some azure app service and each one have their config. I would like to get that setting and store it into a variable @{}. So when i run:

    $srcapp = Get-AzWebApp -ResourceGroupName "myrg" -Name "myapp"
    $srcapp.SiteConfig.AppSettings

i got the config in a Hashtable:

Name   Value
----   -----
Tetst3 78910
Test2  4567
Test   1234

after that i would like to put it in a @{} to define the hashtable:

@{"Test"='1234';"Test2"='4567';"Tetst3"='78910'}

is it possible?

Thanks


Solution

  • $srcapp.SiteConfig.AppSettings returns an object of type System.Collections.Generic.IList[Microsoft.Azure.Management.WebSites.Models.NameValuePair]:

    $webapp = Get-AzWebApp -ResourceGroupName "myrg" -Name "myapp";
    
    write-host ($webapp.SiteConfig | gm -Name "AppSettings").Definition;
    # System.Collections.Generic.IList[
    #    Microsoft.Azure.Management.WebSites.Models.NameValuePair
    # ] AppSettings {get;set;}
    
    $appsettings = $webapp.SiteConfig.AppSettings
    
    $appsettings
    # Name   Value
    # ----   -----
    # Tetst3 78910
    # Test2  4567
    # Test   1234
    
    

    Note you can also make one for testing like this:

    $appsettings = [System.Collections.Generic.List[Microsoft.Azure.Management.WebSites.Models.NameValuePair]] @(
        [Microsoft.Azure.Management.WebSites.Models.NameValuePair]::new("Tetst3", "78910"),
        [Microsoft.Azure.Management.WebSites.Models.NameValuePair]::new("Test2",  "4567"),
        [Microsoft.Azure.Management.WebSites.Models.NameValuePair]::new("Test",   "1234")
    );
    
    $appsettings
    # Name   Value
    # ----   -----
    # Tetst3 78910
    # Test2  4567
    # Test   1234
    

    To convert this to a System.Collections.Hashtable you could do something like this:

    $hashtable = $appsettings | foreach-object `
        -Begin   { $tmp = @{}; } `
        -Process { $tmp.Add($_.Name, $_.Value); } `
        -End     { return $tmp; }
    
    $hashtable | Format-Table -AutoSize;
    # Name   Value
    # ----   -----
    # Tetst3 78910
    # Test2  4567
    # Test   1234
    

    Sure, it looks the same when it gets serialised for output, but they're completely different underlying types in memory...

    If you specifically want a stringified version of the hashtable you can do this:

    $psobject = new-object pscustomobject -Property $hashtable;
    $output = "$psobject";
    $output
    # @{Tetst3=78910; Test2=4567; Test=1234}
    

    or, if you need it to be stringified exactly like your example, you can do this:

    $result = "@{" + `
        (($hashtable.GetEnumerator() | foreach-object {
             "`"$($_.Name)`"=`"$($_.Value)`""
        }) -join ";") + `
    "}";
    
    $result
    # @{"Tetst3"="78910";"Test2"="4567";"Test"="1234"}
    

    But be aware that it will probably break if your data has special characters like quotes in it...