Jul 20 2022 05:24 AM - edited Jul 20 2022 05:36 AM
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?
Jul 20 2022 08:02 AM
Jul 20 2022 08:07 AM
@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.
Jul 20 2022 08:18 AM
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 @
Jul 24 2022 09:13 AM
Jul 24 2022 11:38 PM
@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