3
votes

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.

2
You can provide more details about what error do you get? If all the Azure PowerShell cannot execute? And do you log in before you execute the Azure PowerShell?Charles Xu
Can you post complete error exception message >HariHaran
Do you set the execution policy for your local machine?Charles Xu
yes. I have already set it to unrestrictedSRoy
Is it only Az.compute module can't be used?Charles Xu

2 Answers

3
votes
Install-Module -Name Az -AllowClobber -Scope CurrentUser

Run that command. Restart Powershell. Run import-module az.compute again

1
votes

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.