Forum Discussion
Mapping cmdlet to variable as string
as a quick response, you have to you the double-quote to pass the string.
Passing the string without the double-quote, PowerShell will execute it and start it.
- majeranrJul 20, 2022Copper Contributor
farismalaeb that's why I used this:
$dnsVnetLink = "az network private-dns link vnet create" ` + " --resource-group $resourceGroupName" ` + " --zone-name $dnsZoneName" ` + " --name `"$vnetName-DnsLink`"" ` + " --virtual-network $vnetId" ` + " --registration-enabled `"false`""As an input finally, but is there a way to make it up into single string? As far as I know there's no escape character inside of the string, since backtick ( ` ) doesn't work inside strings. I'm looking for the simplest way to pass it as a string or, if someone knows a better way, for a better solution to write a re-usable code instead of using DoUntil loop function.
- farismalaebJul 24, 2022Iron Contributor
- majeranrJul 25, 2022Copper Contributor
farismalaeb Unfortunately no answer was helpful, I've found the solution myself.
I made the $resourceToCreate parameter non mandatory and instead of passing cmdlets as parameter to the function, I'm defining the $resourceToCreate variable every single time right before I want to invoke the function.
E.g.
$resourceToCreate = az network private-dns link vnet create ` --resource-group $resourceGroupName ` --zone-name $dnsZoneName ` --name "$vnetName-DnsLink" ` --virtual-network $vnetId ` --registration-enabled "false" Add-Resource $resourceToCreate = az network private-dns record-set a create ` --resource-group $resourceGroupName ` --zone-name $dnsZoneName ` --name $storageAccountName Add-Resource
- farismalaebJul 20, 2022Iron Contributor
You can use the Here-String
something like this
$dnsVnetLink =@" az network private-dns link vnet create --resource-group $resourceGroupName --zone-name $dnsZoneName" ` --name `"$vnetName-DnsLink`"" ` --virtual-network $vnetId" ` --registration-enabled `"false`" "@It start with @ and double-quote and ends with double-quote and @