Share via

Automation runbook sql input variable flexible

Sarita Sharma 21 Reputation points
2021-09-09T20:09:14.77+00:00

I have multiple runbooks named $runbbokname.ps1
using different input variable file for different runbook
$runBookName-Variable.ps1
$runBookName-Variable.sh
$runBookName-Variable.sql

Using publishrunbook.ps1 to deploy them to Azure. As of now one input variable file for one runbook.

if ((Test-Path "$runBookScriptFolder\$runBookName-Variable.ps1"))
{
$runBookRemoteScriptVariable = "$runBookName-Variable-ps1"
$runBookRemoteScriptName = "$runBookName-Variable.ps1"
}

if ((Test-Path "$runBookScriptFolder\$runBookName-Variable.sh"))
{
$runBookRemoteScriptVariable = "$runBookName-Variable-sh"
$runBookRemoteScriptName = "$runBookName-Variable.sh"
}

if ((Test-Path "$runBookScriptFolder\$runBookName-Variable.sql"))
{
$runBookRemoteScriptVariable = "$runBookName-Variable-sql"
$runBookRemoteScriptName = "$runBookName-Variable.sql"
}

I want to use multiple variables - but don't make the variable name hard coded, it should be flexible.

Please suggest

Azure Automation
Azure Automation

An Azure service that is used to automate, configure, and install updates across hybrid environments.


1 answer

Sort by: Most helpful
  1. Sarita Sharma 21 Reputation points
    2021-09-29T18:44:50.9+00:00

    To make the deployment for runbook via PowerShell and using multiple variable, I used for each loop

    Previous version was this

    if ((Test-Path "path\runBookName-Variable.ps1"))
    {
    $runBookRemoteScriptVariable = "runBookName-Variable-ps1"
    $runBookRemoteScriptName = "runBookName-Variable.ps1"
    }
    }
    Current more flexible way is this:

    $targetVarFiles = @(Get-ChildItem "path\nameofrunbook-variable*.*")

    if ($targetvarfiles.Count -gt 0)
    {
    foreach ($targetFile in $targetvarfiles) {
    $baseName = $targetFile.BaseName
    $extension = $targetFile.Extension -replace '.','-'
    [string] $runBookRemoteScriptVariable = ($baseName + $extension)

        #get the correct file content from target file  
        [string] $script = Get-Content -Path $targetFile -Raw -Encoding "UTF8"  
          
        #create automation variable and deploy it to Azure (below link for this)  
       https://learn.microsoft.com/en-us/azure/automation/shared-resources/variables?tabs=azure-powershell  
    }  
    

    }

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.