Search code examples
powershellazure-powershellaz

The 'Get-azVM' command was found in the module 'Az.Compute', but the module could not be loaded


I have used Az module in powershell script which is getting executed from powershell core but getting error while executing the script from application (.Net core SDK 2.1).

$VMExistanceCheck = Get-azVM -ResourceGroupName $VMResourceGroup -Name $NewComputerName -ErrorAction SilentlyContinue

I checked that Az.Compute module had been imported there.

I added Import-Module Az.compute -verbose -force inside the script also.

I got the following error while the script is getting invoked from application using runspace(System.Management.Automation, Version=6.1.0.0):

System.Management.Automation.RuntimeException: The 'Get-azVM' command was found in the module 'Az.Compute', but the module could not be loaded. For more information, run 'Import-Module Az.Compute'. ---> System.Management.Automation.CommandNotFoundException: The 'Get-azVM' command was found in the module 'Az.Compute', but the module could not be loaded. For more information, run 'Import-Module Az.Compute'.

'Get-Module -ListAvailable ' shows that Az.Compute module is avilable but while the command 'import-module Az.Compute' is executed using Powershell 6, nothing is importing.


Solution

  • I checked the log of PowerShellCore/Operational from Event viewer and found the following error:

    Error Message = Could not load file or assembly 'Microsoft.WindowsAzure.Storage, Version=9.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621) Fully Qualified Error ID = System.IO.FileLoadException,Microsoft.PowerShell.Commands.ImportModuleCommand

    I have installed Microsoft.WindowsAzure.Storage and finally the powershell script is getting invoked and the Get-AZVM command is successfully getting executed.