Azure CLI
1 TopicMapping 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] $resourceToCreate, [Parameter(Mandatory = $false)] [int] $retryCount, [Parameter(Mandatory = $false)] [int] $sleepTime ) if (!$retryCount) { $retryCount = 3 } if (!$sleepTime) { $sleepTime = 30 } $counter = 0 do { try { $resourceToCreate $counter++ Start-Sleep -Seconds $sleepTime } catch { $_ } } until ($null -ne $resourceToCreate -or $counter -ge $retryCount) if ($counter -ge $retryCount) { throw "Resource creation failed!" } } Then I'd like to call it in other scripts, so I've added it using . & path and that's fine. In script where I'm trying to utilize it, I have the Azure CLI cmdlet set as variable: $dnsVnetLink = az network private-dns link vnet create ` --resource-group $resourceGroupName ` --zone-name $dnsZoneName ` --name $vnetName-DnsLink ` --virtual-network $vnetId ` --registration-enabled false I would like to pass this variable as parameter to Add-Resource function, but as soon as I do: Add-Resource -resourceToCreate $dnsVnetLink The cmdlet inside of the variable is being executed before the function even starts. I'd like it to be executed inside of the function only, not earlier as the idea of this function is to "build" DoUntil loop around the cmdlet. I've worked out a solution to pass it as: $dnsVnetLink = "az network private-dns link vnet create" ` + " --resource-group $resourceGroupName" ` + " --zone-name $dnsZoneName" ` + " --name `"$vnetName-DnsLink`"" ` + " --virtual-network $vnetId" ` + " --registration-enabled `"false`"" But it doesn't look good and remembering about quoting cmdlets like that every time I, or anyone else from my team, want to use it might be troublesome. Is there a way to pass the cmdlet as parameter to function without executing it outside of the function first while keeping the code as simple, clear & easy to read as possible?1.4KViews0likes5Comments