Unable to install Az powershell module with PS7 on windows10 - azure

I am administrator on my Windows10, and trying to install Azure PS module as per the documentation in here.
PS C:\Program Files\PowerShell\7> Install-Module -Name Az -Scope CurrentUser -Force -Allowclobber
However, I get the error message as below..
Install-Package: C:\program files\powershell\7\Modules\PowerShellGet\PSModule.psm1:9711
Line 9711 talledPackages = PackageManagement\Install-Package #PSBoundParameters
Administrator rights are required to install or update. Log on to the computer with an account that has Administrator rights, and then try again, or install by adding "-Scope CurrentUser" to your command. You can also try running the Windows PowerShell session with elevated rights (Run as Administrator).
It does not make sense why it throws this error even though I am already in admin privilege mode. Few other SO answers asked to set to use TLS 1.2, I have done that as well.
Like so [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12.
What else could be the issue ?

Trying to install az module over existing modules will give this error and it appears to be the cause for this issue.
The way to solve this issue is by deleting already existing not in use previous az modules which you can find in the following path -
C:\Program Files\WindowsPowerShell\Modules.
Then run the following command -
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force -AllowClobber
And this will solve your problem.
Also look at az module falsely throws Admin rights required error.
You can also check this discussion and find the similar answer in this comment.

Related

How to install Az module on Windows Server through DevOps?

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?
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)
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

Azure Devops - for ARM template deployment

While Running a job for arm template deployment using a Power shell script it's throwing error like the term New-AzumResourceGroupDeployment is not recognized as a name of cmdlet function script file.can anyone help me understand on this.
Odds are the AzureRM Powershell module isn't installed on the machine. Run the following command in an elevated powershell session: More Detail
Install-Module -Name AzureRM -AllowClobber
I would make a recommendation note to user AzureRm as it has been replaced with the Az module. For Az run: More Detail If the machine has Az already installed on it then skip to the alias section as this would also throw the error you are describing if Az is installed and the script is reference Rm
Install-Module -Name Az -AllowClobber
If you are running powershell that reference the AzureRm module create an alias for it after installing Az by running
Enable-AzureRmAlias -Scope CurrentUser
After the installing the Az Module
Firstly, there is Azure Resource Group Deployment task that could be used to deploy ARM template.
Also, there is Azure PowerShell task that would include many modules, so you can use this task too.
Secondly, the command is New-AzureRmResourcegroupdeployment or New-AzResourceGroupDeployment.
On the other hand, by default, for powershell task, it won't include Azure Modules, you need to install or import it. Check DreadedFrost's reply.
Related article: https://learn.microsoft.com/en-us/powershell/azure/azurerm/install-azurerm-ps?view=azurermps-6.13.0

Unable to install AzureAd module using Install-Module -Name AzureAD in ADO pipeline

I am trying to execute Get-AzureADServicePrincipal in a azure powershell task in ADO.
I need to get service principal for me to then connect to get ServicePrincipalId and ServicePrincipalkey.
As part of this I am trying the below, as an inline script
Install-Module -Name AzureAD
Get-AzureADServicePrincipal
But this keeps failing as,
WARNING: User declined to install module (AzureAD)
[error]The term 'Get-AzureADServicePrincipal' is not recognized as the name of a cmdlet
Please could some one help? Thanks
Install-Module is prompting for user input. Specify the -Force flag.
If you are using host agent. Better to install a module only for the current user
This example downloads and installs the newest version of a module, only for the current user. Also add -Force
Install-Module -Name PowerShellGet -Scope CurrentUser -Force
Then you need to call the Connect-AzureAD cmdlet before calling any other cmdlets.
Finally Get-AzureADServicePrincipal.

Installing Azure Module on an AWS instance using userdata

I'm trying to automatically install the Azure module for PowerShell (Az) on an AWS EC2 instance through userdata so Azure commands can be executed when an instance is started. However, I'm getting this error:
PackageManagement\Install-Package : Cannot convert value
"2.0.0-preview" to type "System.Version". Error: "Input string was
not in a correct format."
Other people who have reported this problem have solved it by upgrading PowerShellGet and removing the old version. I'm doing that with this block of code:
Install-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201 -Force -Confirm:$false
Install-Module -Name PowerShellGet -Repository PSGallery -Force -Confirm:$false
Remove-Item -Path "C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1" -Force -Recurse
However when I run the command to install the moudule after that:
Install-Module -Name Az -AllowClobber -Force -Confirm:$false -Scope AllUsers
I still get the same error.
How can I fix this without having to close and reopen the shell? This all needs to run as part of the same script.
You used Remove-Item, however that command just deleted the module file. The imported module still existed in your PS session. So, you may try to use Remove-Module remove it and use Import-Module to import the right one. But, this is not the suggested way, you may try the following two approaches:
You can just use Update-Module -Name PowerShellGet -Force to directly update your existing module.
Another way is to uninstall your old module, and then install the new version.

Issues with installing Azure specific PowerShell modules for writing to Azure Tables

We are currently rolling out Windows 10 (1903) devices via Intune, and we have requirements to install Azure PowerShell modules under the System account for the devices to communicate and write data to Azure tables. There is a script running under the system account that will write data to Azure every so often.
None of these modules are loading, even when we manually copy them across to the correct location and try the 'offline' method, and we have noticed the following errors on the clients within Event Viewer:
Cannot convert value "2.0.0-preview" to type "System.Version". Error: "Input string was not in a correct format."
The modules that I am trying to install are:
Install-Module -Name Az -AllowClobber -Force | Out-Null
Install-Module -Name AzureRmStorageTable -RequiredVersion 2.0.1 -Force | Out-Null
Any advice/help would be greatly appreciated.
Referring to Github issue: Can't install PowerShell Az - Cannot convert value "2.0.0-preview" Windows 10
If your PowerShellGet module version is less than 1.6.0, you might encounter this issue. You may update the PowerShellGet module nad have another try.

Resources