Search code examples
azureazure-automationpowershell-7.0

Azure Automation Hybrid-Worker Get-AutomationPSCredential for PowerShell 7


We are moving a few Azure Automation Hybrid-worker scripts to PowerShell 7.1. In doing so one of the commands that work in PowerShell 5.1 is: [PSCredential] $AutomationCredential = Get-AutomationPSCredential -Name 'abcdef'. When we try the same command in PowerShell 7.1 we get an error The 'Get-AutomationPSCredential' command was found in the module 'Orchestrator.AssetManagement.Cmdlets', but the module could not be loaded. For more information, run 'Import-Module Orchestrator.AssetManagement.Cmdlets'

We have added the Import-Module to the code but we get Could not load file or assembly 'JobRuntimeData.Client, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

We do find Orchestrator.AssetManagement.Cmdlets the on the hybrid-worker, in the sandbox area. We know that this module is loaded when the hybrid-worker is installed (https://learn.microsoft.com/en-us/azure/automation/shared-resources/modules#internal-cmdlets).


Solution

  • We are assuming the Orchestrator.AssetManagement.Cmdlets is bugged at this point, but have not found anything to say that it is. To get around it we were going to use a runbook variable to store the password, but it needs the cmdlet to actually decode a secret/hidden value.

    In the end, we used a key vault to store the password. The following is what was used as a workaround.

    $User = "AutomationUser"
    $KeyVaultName = "KeyVault"
    try {
        $Password = (ConvertTo-SecureString (Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name $User -AsPlainText) -AsPlainText -Force)
        [PSCredential] $AutomationCredential = New-Object System.Management.Automation.PSCredential($User, $Password)
    }
    catch {
        $ErrorMessage = "Unable to retrieve credentials for user: [${$User}].  $_"
        throw $ErrorMessage
        BREAK
    }