question

Emanuele-0211 avatar image
0 Votes"
Emanuele-0211 asked Emanuele-0211 edited

Azure Automation, problem with webhook and parallel

Hi,

i'm getting some problem (bug?) with azure automation runbook.

Last week i converted this script from 5.1 to 7.1 preview:

 $connectionResult = Connect-AzAccount -Identity
 $connectionResult
    
 ##### SCRIPT START ST
    
 $AppGwlist = Get-AzApplicationGateway
    
 foreach ($AppGw in $AppGwlist)
 {
            
         $Gw = Get-AzApplicationGateway -ResourceGroupName $AppGw.ResourceGroupName -Name $AppGw.Name
         Start-AzApplicationGateway -ApplicationGateway $Gw
    
 }
    
 Start-Sleep -Seconds 10
    
 $Apselist = Get-AzWebApp
    
 foreach ($Apse in $Apselist)
 {
         Start-AzWebApp -ResourceGroupName $Apse.ResourceGroup -Name $Apse.Name
            
         $ApseSlotlist = Get-AzWebAppSlot -ResourceGroupName $Apse.ResourceGroup -Name $Apse.Name
         if ($ApseSlotlist -ne $null){
             foreach ($ApseSlotAll in $ApseSlotlist){
    
                 Start-AzWebAppSlot -ResourceGroupName $Apse.ResourceGroup -Name $Apse.Name -Slot $ApseSlotAll.Name.Split("/")[1]
    
             }    
            
         }    
 }

In this case script start app gateway and app service. I used 7.1 to use parallel foreach and i did this:

 $connectionResult = Connect-AzAccount -Identity
 $connectionResult
    
        
 ##### SCRIPT START ST
    
 $AppGwlist = Get-AzApplicationGateway | ForEach-Object {
     $z = @{}
     foreach($prop in $_.PSObject.Properties) {
         $z[$prop.Name] = $prop.Value
     }
     $z
 }
    
 $AppGwlist | ForEach-Object -Parallel  {
     function Splatting {
         param($Name, $ResourceGroupName)
    
         $Gw = Get-AzApplicationGateway -ResourceGroupName $ResourceGroupName -Name $Name
         Start-AzApplicationGateway -ApplicationGateway $Gw
    
    
     }
    
     Splatting @_
 } -ThrottleLimit 10
    
    
 Start-Sleep -Seconds 60
    
    
 $Apselist = Get-AzWebApp | ForEach-Object {
     $z = @{}
     foreach($prop in $_.PSObject.Properties) {
         $z[$prop.Name] = $prop.Value
     }
     $z
 }
    
 $Apselist | ForEach-Object -Parallel  {
     function Splatting {
         param($Name, $ResourceGroupName)
            
         
         Start-AzWebApp -ResourceGroupName $ResourceGroupName -Name $Name
            
         $ApseSlotlist = Get-AzWebAppSlot -ResourceGroupName $ResourceGroupName -Name $Name
         if ($ApseSlotlist -ne $null){
             foreach ($ApseSlotAll in $ApseSlotlist){
    
         Start-AzWebAppSlot -ResourceGroupName $ResourceGroupName -Name $Name -Slot $ApseSlotlist.Name.Split("/")[1]
            
     }
    
   }
    
     
      
  } 
    Splatting @_
 } -ThrottleLimit 10


Script working, but when i invoke it via webhook, script goes in failed without any error. On postman i got 202, script run but suddenly go in failed. I reconfigured old script with 5.1 and webhook start to work as usual. There is something changeng in webhook invokation with 7.1 runtime?


To bypass webhook problem, now i am using it again 5.1 runtime. I converted my script with start-job and workflow to parallelize it

 $connectionResult = Connect-AzAccount -Identity
 $connectionResult
    
 ### EXCLUSIONS
 $ApseExclusion  = ''
 $AppGwExclusion = ''
    
 ### STOP APSE
 $ScriptBlockApse = {
     param($ApseName,$RgApse) 
                         
     if ($ApseName -notin $ApseExclusion){
            
         Stop-AzWebApp -ResourceGroupName $RgApse -Name $ApseName
            
         $ApseSlotlist = Get-AzWebAppSlot -ResourceGroupName $RgApse -Name $ApseName
         if ($ApseSlotlist -ne $null){
    
             foreach ($ApseSlotAll in $ApseSlotlist){
    
                Stop-AzWebAppSlot -ResourceGroupName $RgApse -Name $ApseName -Slot $ApseSlotAll.Name.Split("/")[1]
    
             }
         }   
     } 
    
 }
    
 $ApseList = Get-AzWebApp
    
 foreach($Apse in $ApseList) {
     # Execute the jobs in parallel
     $ApseName = $Apse.Name
     $RgApse   = $Apse.ResourceGroup  
     Start-Job $ScriptBlockApse -ArgumentList $ApseName, $RgApse
      
 }
    
 # Wait for all to complete
 While (Get-Job -State "Running") { Start-Sleep 5 }
    
 # Display output from all jobs
 $res += (Get-Job | Receive-Job)
    
 # Cleanup
 Remove-Job *
    
    
 Start-Sleep -Seconds 10
    
    
 ### STOP APPGW
 $ScriptBlockAppGw = {
     param($AppGwName,$RgAppGw) 
    
  if ($AppGwName -notin $AppGwExclusion){
    
         $Gw = Get-AzApplicationGateway -ResourceGroupName $RgAppGw -Name $AppGwName
         Stop-AzApplicationGateway -ApplicationGateway $Gw
    
  }
    
 }
    
 $AppGwList = Get-AzApplicationGateway
    
 foreach($AppGw in $AppGwList) {
     # Execute the jobs in parallel
     $AppGwName = $AppGw.Name
     $RgAppGw   = $AppGw.ResourceGroupName  
     Start-Job $ScriptBlockAppGw -ArgumentList $AppGwName, $RgAppGw
      
 }
    
 # Wait for all to complete
 While (Get-Job -State "Running") { Start-Sleep 5 }
    
 # Display output from all jobs
 $res += (Get-Job | Receive-Job)
    
 # Cleanup
 Remove-Job *


with workflow


         $connectionResult = Connect-AzAccount -Identity
         $connectionResult
            
         ### STOP APSE
         workflow test {
            
            
         $ApseList = Get-AzWebApp   
         foreach -parallel ($in in $ApseList){       
            
                   
                 Stop-AzWebApp -ResourceGroupName $in.ResourceGroupName -Name $in.name
                    
                 $ApseSlotlist = Get-AzWebAppSlot -ResourceGroupName $in.ResourceGroupName -Name $in.name
                 if ($ApseSlotlist -ne $null){
            
                     foreach ($ApseSlotAll in $ApseSlotlist){
            
                        Stop-AzWebAppSlot -ResourceGroupName $in.ResourceGroupName -Name $in.name -Slot $ApseSlotAll.Name.Split("/")[1]
            
                     }
                 }   
             } 
            
         }

but i'm getting this error:

Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=mytoken' or one of its dependencies. The system cannot find the file specified.

I'm getting crazy becouse locally, with powershell ise they are working.

I have to install something or there is some broke in my code?

Thanks

















azure-automation
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AnuragSingh-MSFT avatar image
0 Votes"
AnuragSingh-MSFT answered AnuragSingh-MSFT commented

Hi @Emanuele-0211,

Welcome to Microsoft Q&A! I am sorry for the inconvenience you faced with the above-mentioned issue. Please find my response below regarding them:

1. Runbook based on PowerShell version 7.1 don't work with Webhook: I tested a simple scenario and can reproduce it. I will report it to our team and will update this thread with any information I get.

2. For error related to Newtonsoft.Json : It might be possible that you are using older version of Modules in the runbook account. Can you please update the module available in Automation Account as mentioned in this link.

In case, this does not help, can you please add try{} catch{} blocks to the code to help us understand the error. A sample may look like below which should give us details on which command/module is throwing the exception.

 try {
      #Code that is/can throwing exception. 
  }
  catch {
      Write-Output $_.Exception.ToString() #Gets full details of the exception with stack
 }

The script works fine from your local machine as it has access to all the modules and other resources available on this machine, whereas the runbook runs in a sandboxed environment in Azure. One of the other options is to use Hybrid Runbook Worker, so that you have full control on the execution environment.

Please let me know if you have any questions.


Update 02/28/2022

Regarding point 1 above (PowerShell 7.1 runbook not getting triggered using Webhook) - our team is working on it and the fix should be released by end of next month. Meanwhile, please define a parameter for webhook as a workaround for it:
please define a parameter for webhook data as below as a workaround:

 param
  (
      [Parameter(Mandatory=$false)]
      [object] $WebhookData
  )

Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Emanuele-0211, Regarding the failure of runbook jobs for PowerShell 7.1 runbook, please define a parameter for webhook data as below, as a workaround:

 param
 (
     [Parameter(Mandatory=$false)]
     [object] $WebhookData
 )

The fix to this issue will be released by end of next month.

Also, did you get a chance to follow the suggestion above regarding error related to Newtonsoft.Json. Are you still facing this issue?


Please 'Accept as answer' if it helped so that it can help others in the community looking for help on similar topics.

0 Votes 0 ·

@Emanuele-0211, I wanted to check if you had a chance to review my answer above. Please let me know if you have any queries or concerns.

Please 'Accept as answer' if it helped so that it can help others in the community looking for help on similar topics.

0 Votes 0 ·
Emanuele-0211 avatar image
0 Votes"
Emanuele-0211 answered Emanuele-0211 edited

Hi @AnuragSingh-MSFT ,

thanks for the advice and sorry for delay. I solved by using powershell workflow and the problem was my fault. I created runbook not with workflow option on azure.

For 7.1 i red on documentation that is not supported yet but is not a problem. I can do same with workflow.

Thanks for your advice

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.