Forum Discussion
majeranr
Jul 20, 2022Copper Contributor
Mapping cmdlet to variable as string
Hi, I'm having a trouble with a part of code for script. I've got a function: function Add-Resource
{
Param(
[Parameter(Mandatory = $true)] [String] $resourceToC...
majeranr
Jul 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.
farismalaeb
Jul 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