kev
kev

Reputation: 43

Powershell azure - Importing csv file into appsettings

Does anyone know how to import a csv file into Azure app settings using powershell?

This is what I currently have but does not work.

Function Import-AppSettings 
{
Step-TimeBlock {  

    $csv = "C:\appsettings.csv"
    $appSettingsList = Import-Csv $csv -Encoding UTF8 -Delimiter '|'

    $appSettingsHash = @{}
    Write-Host "current app settings" -foreground yellow;
    ForEach ($kvp in $appSettingsList) {
        $appSettingsHash[$kvp.Name] = $kvp.Value
        write-host $kvp.Name ": " $kvp.Value -foreground green;
    }

    Set-AzureRMWebApp -ResourceGroupName myresourcegroup -Name mywebapp -AppSettings $appSettingsHash
}
}

Upvotes: 1

Views: 294

Answers (1)

Shui shengbao
Shui shengbao

Reputation: 19223

You need move Set-AzureRmWebApp to Foreach loop.

The following script works for me.

$csv = "d:\webapp.csv"
$appSettingsList = Import-Csv $csv 

$appSettingsHash = @{}
Write-Host "current app settings" -foreground yellow;
ForEach ($kvp in $appSettingsList) {
    $appSettingsHash[$kvp.Name] = $kvp.Value
    write-host $kvp.Name ": " $kvp.Value -foreground green;
    Set-AzureRMWebApp -ResourceGroupName shuiweb -Name shuiweb -AppSettings $appSettingsHash
}

My csv file like below:

Name,Value
shuivnet1,shuitest1
shuivnet3,shuitest2

Note: If I use -Encoding UTF8 -Delimiter '|', your script does not work. For test, I suggest you could get one $appSettingHash and check your hash value. Like below:

$appSettingsHash[$appSettingsList.Name[0]]=$appSettingsList.Value[0] 
$appSettingsHash

The value should like below:

PS C:\Users\v-shshui> $appSettingsHash

Name                           Value
----                           -----
shuivnet1                      shuitest1

Upvotes: 2

Related Questions