Forum Discussion

majeranr's avatar
majeranr
Copper Contributor
Jul 20, 2022

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] $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?

5 Replies

  • farismalaeb's avatar
    farismalaeb
    Steel Contributor
    When using the " " you are telling PowerShell the type and how to execute the argument.
    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.
    • majeranr's avatar
      majeranr
      Copper 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's avatar
        farismalaeb
        Steel Contributor

        majeranr 

        Any update on the provided answer. 

        If it helps you please mark it as Best Reponse.

        Thanks

         

Resources