Every so often, we find an Azure Automation Runbook fails with the following error:
Get-AutomationPSCredential : The term 'Get-AutomationPSCredential' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At line:1 char:9
+ $cred = Get-AutomationPSCredential -Name SqlJobRunner
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-AutomationPSCredential:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Different runbooks of ours fail with this error, seemingly at random.
Get-AutomationPSCredential
is an Azure-provided module. It's not our code, it's Microsoft's.
And sometimes, it appears to just "go away".
So what's happening here? Is the Azure Automation infrastructure reloading or updating modules at an inopportune time for our job? I was under the impression that once a runbook is scheduled, the dependent modules are "bound" to the runbook, so they'll be there when needed.
Why does this happen and what's the best defense against failure?
I received a call from Microsoft Azure support with the root cause.
Azure Automation in my region (East US) is running on PowerShell 5.0. They said there is a problem in 5.0 in that modules are loaded asynchronously. Cross-module dependencies are therefore not guaranteed to be available.
They told me the defect will be fixed in August when they release PowerShell 5.1 to the East US region.
They offered two mitigations:
Another mitigation would be to import the required module where I need it. A complication of doing so is that the function in question, Get-AutomationPSCredential, comes from a different assembly in local development than when executing in the Automation context. In local development using the Azure Automation Authoring Toolkit, the command resides in assembly
AzureAutomationAuthoringToolkit
. In Azure, it's inOrchestrator.AssetManagement.Cmdlets
.I'm going to try simply this:
Import-Module Orchestrator.AssetManagement.Cmdlets -ErrorAction SilentlyContinue
When executing in the ISE environment, the function will always be there, and this Import-Module will fail silently.
According to your description, we should check if the module has import. If not, we should import it.
We can use portal to check it:
Automation account-> modules->Orchestrator.AssetManagement.Cmdlets:
If we does not import it, we should use portal to import this module: