Installing Azure Module on an AWS instance using userdata - azure

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.

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

Unable to install Az powershell module with PS7 on windows10

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.

Get-AzSubscription not working but Get-AzureRMSubscription is working

When I am trying to execute some Powershell commands like Get-AzSubscription, it is not working but I get output when I use Get-AzureRMSubscription. Same is the case for other commands as well like Get-AzVM, New-AzSnapshotConfig, New-AzSnapshot.
The Azure RM versions installed on my machine are:-
Get-Module -ListAvailable -Name AzureRM.Resources
ModuleType Version Name
Script 6.7.3 AzureRM.Resources
Script 5.5.2 AzureRM.Resources
Why is it so ? When I tried on Azure portal cloudshell, it worked just fine with Get-AzSubscription, Get-AzVM, New-AzSnapshotConfig, New-AzSnapshot, etc.
Got to understand. Either AzureRM or Az can be loaded at a time. Uninstalled AzureRM module and installed Az and all Az commands are working now.
Install-Module -Name Az -AllowClobber
Now:-
Get-Module -ListAvailable -Name Az.Resources
ModuleType Version Name
---------- ------- ----
Script 2.0.1 Az.Resources

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.

The term 'az' is not recognized as the name of a cmdlet, function, script file, or operable program

Created Powershell Azure function and trying to use "az" commands under that function app. As per docs, function runtime should resolve "az" and other module dependencies. but it doesn't work for me.
ERROR: The term 'az' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Microsoft.Azure.WebJobs.Script.Rpc.RpcException : Result: ERROR: The term 'az' is not recognized as the name of a cmdlet, function, script file, or operable program.
I want to run some "az" command under function app without manually uploading modules. Is it powershell Preview version issue or something I need to correct?
requirement.psd1
#{
Az = '2.*'
}
For those who get these error in local while trying to access AZ, try this command below in an admin PowerShell instance.
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi
You can get more details about installation # https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest&tabs=azure-powershell
Make sure you restart the powershell instance
"Az" in the context of PowerShell probably means the Az module, with cmdlets like Add-AzAccount etc.
"az" is the cross-platform CLI, which is not a PowerShell module.
I got this error because I had not installed CLI. I was sent this link which downloaded the required install:
https://aka.ms/installazurecliwindows
Two ways you can solve this issue.
Install below installation file for Azure CLI in windows.
https://aka.ms/installazurecliwindows
(OR)
Install using PowerShell the below command.
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi
More details: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest&tabs=azure-cli
I can see PowerShell 7.x and later is the recommended version.
You can check Powershell version using below command
$PSVersionTable.PSVersion
https://learn.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-6.2.0
NOTE
PowerShell 7.x and later is the recommended version of PowerShell for
use with the Azure Az PowerShell module on all platforms.
Run this to fix:
Install-Module AzureAD -Force
Install-module AzureADPreview -Force
Install-Module -Name MSOnline -Force
Import-Module Az -Force
Install-Module Az -Force
Download this then this problem will not occur.
https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli

Resources