Search code examples
azurepowershellazure-powershellazure-automationazure-runbook

Azure Devops pipeline throwing error while running powershell script - The string is missing the terminator: "


I'm trying to register schedule for Azure automation runbook by using below PS command

Register-AzureRmAutomationScheduledRunbook -AutomationAccountName $automationAccount -ResourceGroupName $automationRg -RunbookName $runBookName -ScheduleName $scheduler.Name -Parameters @{"RESOURCEGROUP"="test-rg";"APPNAME"="test-app-del";"SUBSCRIPTIONID"="test-sub";"BRAND"="MS";"ENV"="dev"} -ErrorAction Stop

The script is executing fine in local machine and able to register schedule for runbook. But when the same code being executed for azure devops pipeline I'm getting below error. The string is missing the terminator: ".

I tried different suggestions provided to find if there are any special characters in the commandlet. But no luck.

enter image description here

Could you please help in resolving the error

I'm using task: AzureCLI@2 and below is the part code within the task. The issue happens when I add this line of code Register-AzureRmAutomationScheduledRunbook

$automationAccount = $rbEnv+"-aa-ng-infra"
$automationRg=$rbEnv+"-rg-ng-infra-shared"
$webSiteName = "pre${{parameters.brand}}-website-${{parameters.brand}}"
$webSiteRG = $rbEnv+"-rg-ng-pre${{parameters.brand}}-website-eau"
$runBookName = "Retire-Container-App"
# check automation runbook exists
$runbook = Get-AzureRmAutomationRunbook -AutomationAccountName $automationAccount -Name $runBookName" -ResourceGroupName $automationRg
$TimeZone = "Australia/Sydney"
$startTime = ([System.DateTimeOffset]::Now.AddHours("${{parameters.deleteContainerAppIn}}"))
$scheduler = New-AzureRmAutomationSchedule -AutomationAccountName $automationAccount -Name "${{parameters.brand}}-aa-sch-eau" -OneTime -ResourceGroupName $automationRg -StartTime $startTime -TimeZone $TimeZone

Register-AzureRmAutomationScheduledRunbook -AutomationAccountName $automationAccount -ResourceGroupName $automationRg -RunbookName $runBookName -ScheduleName $scheduler.Name -Parameters @{'RESOURCEGROUP'='test-rg';'APPNAME'='test-app-del';'SUBSCRIPTIONID'='test-sub';'BRAND'='MS';'ENV'='dev'} -ErrorAction Stop

Solution

  • As I mentioned in the comment. The error is throwing related to unwanted " in $runbook variable declaration's -Name $runBookName" .

    Below is my .yml file for Azure DevOps, which worked for me.

    trigger:
    - master
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
    - script: echo Hello, world!
      displayName: 'Run a one-line script'
    
    - task: PowerShell@2
      displayName: Powershell script
      inputs:
        targetType: 'inline'
        script: |
          Install-Module -Name Az -AllowClobber -Force
          $TenantId = 'xxxxxxxxxxxxxxxx'
          $ApplicationId = 'xxxxxxxxxxxxxxxx'
          $clientSecret = "xxxxxxxxxxxxxx"
          $securePassword = ConvertTo-SecureString $clientSecret -AsPlainText -Force
          $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ApplicationId, $SecurePassword
          Connect-AzAccount -ServicePrincipal -TenantId $TenantId -Credential $Credential
          $automationAccount = "powshellscript"
          $RgName= "xxxxxxxxxxxx"
          $runBookName = "testscript"
          $runbook = Get-AzureRmAutomationRunbook -AutomationAccountName $automationAccount -Name $runBookName -ResourceGroupName $RgName
          $TimeZone = "Australia/Sydney"
          $schedule = New-AzAutomationSchedule -AutomationAccountName $automationAccount -Name "Schedule01" -StartTime "23:00Z" -OneTime -ResourceGroupName $RgName -TimeZone $TimeZone
          Register-AzAutomationScheduledRunbook -AutomationAccountName $automationAccount -RunbookName  -ScheduleName $schedule.Name -ResourceGroupName $RgName
    

    OUTPUT: