0

I am writing a .Net Core application and in one of the APIs, I have to execute the PS script. Earlier I was using AzureRm commands in my PS script along with .Net Framework which use to work just fine, but after upgrading to .Net Core this script is failing. I updated my PS script to use Az commands but post that am getting an error while executing this script through the API.

Error:

The specified module 'Az' was not loaded because no valid module file was found in any module directory

DevOps Powershell Script to install Az module:

Install-Module -Name Az -RequiredVersion 2.8.0 -Force -AllowClobber 

Get-InstalledModule #Just print out the details to confirm whether `Az 2.8.0` has been installed successfully

// Even setting PSModulePath is not working

$key = (Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager').OpenSubKey('Environment', $true)
$path = $key.GetValue('PSModulePath','','DoNotExpandEnvironmentNames')
$path += ';D:\Program Files (x86)\ManagedDependencies\PowerShell\AzPSModules\1.0.0'
$key.SetValue('PSModulePath',$path,[Microsoft.Win32.RegistryValueKind]::ExpandString)

Is there anything else that I need to add to make sure that I can execute Az command in PS script from Azure App Service APIs?

Dishant
  • 1,545
  • 12
  • 29

1 Answers1

0

I have tried to install and set the ModulePath by using PowerShell. I am able to Set the ModulePath in the Environment variable.

The workaround follows

# installing the Required Module
Install-Module -Name Az -RequiredVersion 2.8.0 -Force -AllowClobber -SkipPublisherCheck
# here you can get the list of installed Module
Get-InstalledModule 
# Set the custom path of PSModulePath in Environment Variable
$key = (Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager').OpenSubKey('Environment', $true)
$path = $key.GetValue('PSModulePath','','DoNotExpandEnvironmentNames')
$path += ';D:\Program Files (x86)\ManagedDependencies\PowerShell\AzPSModules\1.0.0'
$key.SetValue('PSModulePath',$path,[Microsoft.Win32.RegistryValueKind]::ExpandString)

enter image description here

The specified module 'Az' was not loaded because no valid module file was found in any module directory

We cannot import all the Az-Modules from the Az folder because Sub-Modules are not installed in the system path. we can load these Sub-Modules one by one.

The sub-modules are automatically being searched in the system path. As they are actually installed in another path, the PowerShell cannot find them, which causes errors. Refer Github discussion for more information

Updated Answer

use the Get-ItemProperty to set the environment path in hosted Agent

# Here using this property in a pipeline
- pwsh: |
    $NewPathRegistory = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
    Write-Host $NewPathRegistry

Refer here

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15
  • Thanks for your answer. I believe that you have executed these commands on your local machine? If so, then the same I am also able to do so but the issue is that I am not able to set the path on the hosted agent. Is there any way to do this via build pipeline? – Dishant Mar 11 '22 at 14:18
  • Please check the updated Answer – Delliganesh Sevanesan Mar 16 '22 at 03:53