How to pass values from csv into az cli deployment as parameters - powershell

I don't have much experience with PowerShell and this simple issue has been driving me up the wall. I'm hoping someone can point me in the right direction.
I have a CSV-file with IP-range values
I wish to pass these IP values as a parameter to a Bicep template
The parameter is of type array, see code snippets below
CSV-file:
IP,Comment
10.0.0.1, Comment blabla
10.0.0.52, Comment more blabla
I wish to pass the IP-values into a Azure Bicep template with the following parameter:
param ipArray array
The cli command is as follows:
az deployment group validate -g test-rg -f .\main.bicep -p ipArray=$ipRange
I am unable to populate $ipRange properly. I have tested the following and know it works:
az deployment group validate -g test-rg -f .\main.bicep -p ipArray="['10.0.0.1','10.0.0.52']"
So I need to figure out how to build my Powershell variable according to above syntax
$ipRange = ((Get-Content .\ip_list.csv) | ConvertFrom-Csv).IP
Failed to parse string as JSON:
10.0.0.1 10.0.0.52
Error detail: Extra data: line 1 column 6 (char 5)
Any nudge in the right direction will be greatly appreciated
Thanks!

This code will convert the ip range as you asked for:
$ipRange = ((Get-Content C:\Temp\ip.csv) | ConvertFrom-Csv).IP | ConvertTo-Json
$ipRange = $ipRange.ToString() -replace '"',"'"
$ipRange
one final thing, in your param, it is mentioned as vlkIpArray and in deployment it is mentioned as ipArray. is this a typo error?
param vlkIpArray array and
az deployment group validate -g test-rg -f .\main.bicep -p ipArray="['10.0.0.1','10.0.0.52']"

Related

aqquire localhost.json from azufunction (powershell commands install failed and I found no az-command for cmd.exe) Any Solutions?

I installed last year the powershell az core tools with big problems but got it working.
I used this 2 Commands to get the local.settings.json iwth actual values:
Connect-AzAccount -Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' -SubscriptionId 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
func azure functionapp fetch-app-settings '<function-name>' --output-file local.settings.json
On installing the powershell core tools we spend a few hours and failed , so i tried to find similar command für standard command line (CMD.exe) but I found no similar command that worked so.
I found only this:
az login --tenant "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx"
this sets the correct tenant and this worked.
I found additionally thsis commad to set the subscription but i got no response:
# change the active subscription using the subscription ID
az account set --subscription "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
And I could request the config setting on screen with that command:
az functionapp config appsettings list --name MyFunctionApp --resource-group MyResourceGroup
But i found no way to create with the az command the local.setings.json and the file is in wrong format so piping it in a file will not work too.
[
...
{
"name": "FUNCTIONS_WORKER_RUNTIME",
"slotSetting": false,
"value": "dotnet"
},
...
]
Can anyone help me to get the file local.settings.json with actual config values with the az commands from standard windows shell.
Generally, local.settings.json file is ignored while publishing to Azure Function App as it is specified in the .gitignore file:
While publishing/deploying the function app, make sure to publish with the setting --publish-local-settings -i.
Syntax:
func azure functionapp publish <FunctionAppName> --publish-local-settings -i
Then, all the local.settings.json config values can be published and the values using the PowerShell cmdlet will be displayed:
Get-AzFunctionAppSetting -Name FunctionAppName -ResourceGroupName RGName | ConvertTo-Json | Out-File "local.settings.json"
OR use the same Az cmdlet to get the correct format of config settings defined in local.settings.json that are published to Azure function app.
to get the file local.settings.json with actual config values
Using az command for saving the published local.settings.json config values in a file:
az functionapp config appsettings list --name MyFunctionApp --resource-group MyResourceGroup --output json > local.settings.json

Anyone know how to escape spaces in Azure Cli?

I am running
$nameLine= az boards iteration team list --team "DevOpsTesting Team" --timeframe current --project DevOpsTesting | findstr "name"
$nameOnly = (select-string ":(.*)" -inputobject $name).Matches.Groups[1].Value.Replace(",","")
az boards work-item create --title Test --type Task --project DevOpsTesting --iteration DevOpsTesting\\$nameOnly --assigned-to A.B#gmail.com --fields "Description=PR is open, please test the new exe version and approve Priority=2"
But because the value of nameOnly is "Sprint 7" I am getting an error
az : ERROR: unrecognized arguments: 7
At line:4 char:1
..?
thx.
If spaces are used within a iteration, then enclose in double-quotes, in your case you could add the doule-quotes like:
--iteration "DevOpsTesting\\$nameOnly"

Bicep/ARM XML parameter as string not parsed correctly

In my Bicep file, I have a parameter:
#secure()
param securityToken string
This parameter is an XML value provided as a string from an environment variable, not read from a file. I have trouble correctly setting this parameter from my YML pipeline. I have this piece of code:
az deployment sub create --template-file main.bicep --parameters securityToken='<xml>'
Unfortunately, this returns an error:
< was unexpected at this time.
Ok, so I somehow have to escape this parameter? I also tried:
az deployment sub create --template-file main.bicep --parameters securityToken='<xml>'
But this gives the following error:
The system cannot find the file specified.
So my questions is:
How can I provide an XML string parameter to my Bicep / ARM deployment?
Thanks!
Deployments to subscriptions need the --location <location> parameter, which is missing in the examples above.
When I run the following on Windows, Git Bash, the deployment succeeds.
az deployment sub create --location eastus --template-file main.bicep --parameters securityToken='<xml>'
Result
main.bicep
targetScope = 'subscription'
#secure()
param securityToken string
output exposedSecret string = securityToken
So I found the problem. Apparently, the < and > needs to be escaped, and I finally found out how: putting it between ". So, the value "<"xml">" did the trick. Now my working code is this:
$token = '<xml></xml>'
$token = $token .replace('<', '"<"') // = "<"xml>"<"/xml>
$token = $token .replace('>', '">"') // = "<"xml">""<"/xml">"
$token = $token .replace('""', '" "') // The double "" only escapes the " itself. Therefore, add an extra space: "<"xml">" "<"/xml">"

Passing parameters using AZ CLI for ARM template deployment

I am trying to use az group deployment create to perform an ARM template deployment and I want to pass in parameters where the values are defined in variables. I can do a single parameter with no issues using the syntax below:
--parameters parameter1=$var1
But when I try to add additional parameters using the syntax below, it fails:
--parameters parameter1=$var1, parameter2=$var2
The syntax below fails as well since it will not use the values of the variables:
--parameters '{
"parameter1": { "value": "$var1" },
"parameter2": { "value": "$var2" }
}'
Does anyone know if what I am trying to do is possible and what the correct syntax would be?
I was fighting a combination of a corrupt shell and slightly incorrect syntax. The correct syntax for what I was trying to do is listed below:
--parameters parameter1=$var1 parameter2=$var2
Or, for a cleaning view when several parameters are involved:
--parameters parameter1=$var1 `
parameter2=$var2 `
parameter3=$var3

Update-AzureRmEventGridSubscription setup subject prefix/postfix to string empty

I have PowerShell script to update EventGrid subscriptions. One of possible scenarios is to set subscription subject prefix/postfix to define value.
Update-AzureRmEventGridSubscription -ResourceGroup $ResourceGroupName -TopicName $EventGridTopicName -EventSubscriptionName $Subscription.name -SubjectEndsWith $Subscription.subjectEndsWith
When value is not null or empty then it works fine. But when it needs to be set to '' it throws
Cannot validate argument on parameter 'SubjectBeginsWith'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again
Should I recreate subscription?
This looks to be a bug in the Update-AzureRmEventGridSubscription implementation, thanks for reporting this! I have filed an issue at https://github.com/Azure/azure-powershell/issues/6331 to track this issue.
Until this is fixed in the EventGrid PowerShell module, the workaround would be to:
1) Either delete and re-create the event-subscription, or
2) to use the equivalent command in Azure CLI (az eventgrid event-subscription update --resource-group your-rg-name --topic-name your-topic-name --name your-event-subscription-name --subject-ends-with "").