running a powershell script in azure automation failure

Raj D 616 Reputation points
2022-08-07T22:31:49.363+00:00

Greetings...

I'm working on making a rest api call which pulls back data from the year 2000 until the current year when I run the powershell script from my machine everything works as expected. If I were to run the azure automation runbook I get out of memory exception is there anything I should be doing to make this work. I'm looping on this rest endpoint for each year starting from the year 2000.

PoSh:

try {  
         $clientSecret = ''  
         $clientId = ''  
         $tenantId = ''  
         # Construct URI  
         $uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"  
         # Construct Body  
         $body = @{  
             client_id = $clientId  
             client_secret = $clientSecret  
             scope = 'https://graph.microsoft.com/.default'  
             grant_type = 'client_credentials'  
         }  
         $Year = "2000"  
         $Uri = "https://apiserver.com/v1/data/year/$Year"  
         # Get OAuth 2.0 Token  
         $tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType 'application/x-www-form-urlencoded' -Body $body -UseBasicParsing  
         # Access Token  
         $token = ($tokenRequest.Content | ConvertFrom-Json).access_token  
         $api = Invoke-RestMethod -Method Get -Uri $Uri -ContentType 'application/json' -Headers @{Authorization = "Bearer $token"} -ErrorAction Stop  
     }  
 catch {  
         "Error"   
         Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__  
         Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription  
         Write-Host "ErrorMessage:" $_.ErrorDetails.Message  
     }  

Error:

228894-aap-error.jpg

Thank you!!!

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
Windows for business | Windows Server | User experience | PowerShell
{count} votes

Accepted answer
  1. Maxim Sergeev 6,586 Reputation points Microsoft Employee
    2022-08-08T16:07:31.947+00:00

    In this case you need changing the following settings in your Hybrid Worker Severs locally

    $PSHome
    Install-PowerShellRemoting.ps1
    net stop winrm
    net start winrm

    Get-Item -Path WSMan:localhost\Shell\MaxMemoryPerShellMB
    Set-Item -Path WSMan:localhost\Shell\MaxMemoryPerShellMB -Value "SET WHAT YOU NEED"

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

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