Reputation: 1
I have a Hybrid worker VM configured on my Azure Automation Account runbook and I am trying to invoke some Pester scripts(ps1 file with invoke-pester command kept in the Hybrid worker VM) from there as an elevated session(with start-process -runas
verb which invokes the ps1 in the VM). But when I do so the pester scripts to give this exception.
CommandNotFoundException: The 'New-AzStorageContext' command was found
in the module 'Az.Storage', but the module could not be loaded. For more
information, run 'Import-Module Az.Storage'.
I am importing the AZ module before the Invoke pester command(Az is latest 1.6.0, Powershell and .net are all latest versions). Still, it does not do anything. I tried to edit the Allusers/AllHosts profile (added Import-Module AZ) in the Hybrid worker VM and ran the runbook. It rectifies the issue for one run, and again I get the issue (it removes the Import-Module AZ added by me and resets itself in the windowspowershell V1.0 directory)), the reason is weirdly the profile.ps1
resets itself.
Expected: Autoimports AZ modules Actual: Does not autoimport AZ modules
Upvotes: 0
Views: 1032
Reputation: 4695
I had a similar issue. The Az module was installed on the VM but the Hybrid Worker returned error that The term 'Connect-AzAccount' is not recognized as the name of a cmdlet, function, script file, or operable program.
Then I discovered the problem. I installed the Az module like this:
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
See the -Scope parameter. The Az module was installed only for me. You have to change it to AllUsers and your hybrid worker will work.
Install-Module -Name Az -Scope AllUsers -Repository PSGallery -Force
Upvotes: 0
Reputation: 1
Same thing happened recently with one VSTS Windows Agent-hybrid Worker, some of the guys installed a new version module az.keyvaults and it was failing with the same error, I found the offending modules in this path:
C:\Windows\System32\config\systemprofile\Documents\WindowsPowerShell, after I deleted the az.keyvaults module started working again.
Upvotes: 0