Azure Release Pipeline Powershell Task Fails - powershell

I have an Azure Release Pipeline with an Azure Powershell Task inline script that fails. I am running a self hosted build agent. The Powershell command works fine when I execute it locally from Windows Powershell. The task is attempting to start a DataFactory SSIS Integration Runtime:
Start-AzDataFactoryV2IntegrationRuntime -ResourceGroupName "MyResourceGroup"
-DataFactoryName "my-data-factory"
-Name "myIntegrationRuntime1"
-Force
Firstly I get a warning:
WARNING: AzureRM.Profile already loaded. Az and AzureRM modules cannot
be imported in the same session or used in the same script or runbook.
If you are running PowerShell in an environment you control you can
use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from
your machine. If you are running in Azure Automation, take care that
none of your runbooks import both Az and AzureRM modules. More
information can be found here: https://aka.ms/azps-migration-guide.
And then the error:
The 'Start-AzDataFactoryV2IntegrationRuntime' command was found in the
module 'Az.DataFactory', but the module could not be loaded. For more
information, run 'Import-Module Az.DataFactory'.
When I look at the installed modules, I see both AzureRM and Az are installed, and although I run the command Import-Module Az.DataFactory I do not see it in the list of installed modules.

WARNING: AzureRM.Profile already loaded. Az and AzureRM modules cannot
be imported in the same session or used in the same script or runbook.
From last year, Az published as a new cross-platform PowerShell module that is completely independent of AzureRM. Since Az and AzureRM use the same dependencies with different versions, it's impossible to run Az and AzureRM side by side in the same PowerShell session. That's why you receive the first error message.
The first solution is remove all AzureRM modules if you don't have script that use AzureRM.
But, if you continue want to use AzureRM for part of scripts while also writing another scripts with Az. You can execute with Azure Powershell task V4.0.

Related

How to use AzureRm module with PowerShell 7?

Context
I've just installed PowerShell 7. I am trying to run my working tested Azure related scripts... So I installed and imported AzureAd and AzureRM modules.
When trying to log in either Connect-AzureAD or Connect-AzureRmAccountboth gave me the following error (keep reading)
Could not load type 'System.Security.Cryptography.SHA256Cng'
OK, this is because the Azure Modules are looking for that API, which is not available in .NET Core, so I used the Import-Module with the -UseWindowsPowerShell parameter, which solved the issue but only for the AzureAD module
Question
For the command Import-Module AzureRm -UseWindowsPowerShell I got the following error message:
Import-Module: Failed to generate proxies for remote module 'AzureRM'. Running the Get-Command command in a remote session returned no results.
So I still can not use Connect-AzureRmAccount Any ideas?
The AzureRm is incompatible with PowerShell 7, and it has been deprecated and will not be updated.
Your option is to use the Az module, just uninstall the AzureRm module and install the Az module.
Install-Module -Name Az -Force
Fore more details, see Introducing the new Azure PowerShell Az module.
Then use the Connect-AzAccount to login, if you don't want to change your existing script which uses AzureRm command, just use Enable-AzureRmAlias before all the commands.

How to use SharePointPnpPowerShellOnline PowerShell module in Azure DevOps?

I am failing to use the SharePointPnpPowerShellOnline PS Module as part of my release pipeline.
We are using Azure DevOps with a hosted build agent on a Azure VM. I want to accomplish uploading build artifacts into Microsoft Teams/Sharepoint.
I installed SharePointPnpPowerShellOnline on the server (under the account that the DevOps build agent operates with), yet the build agent fails to execute the script on the first line that uses said module (a call to Connect-PnPOnline -Url https://... -Credentials ...). Error message: "Connect-PnPOnline : The term 'Connect-PnPOnline' is not recognized as the name of a cmdlet, function, script file". So it behaves as if the module is not installed at all.
yet when I log onto the server (with the correct account), the module is found and I can execute Connect-PnPOnline succesfully.
the used Azure DevOps pipeline task is "PowerShell".
Shouldn't the build agent be able to find the installed module, as I installed it for its user?
Edit: I did some more "debugging", namely:
logging the Username the script is running under by [Environment]::UserName, to make sure I installed the module for the right user. result: matches the expected user.
Listing the installed modules by Get-InstalledModule in my script. Once it is executed in the Release pipeline by the agent it yields an empty list. yet executing the same script on the VM (via RemoteDesktop) yields the installed modules
My guess is I'm doing a stupid beginner mistake because I haven't that much experience with PowerShell...
Answering my own question: In the end I just installed the Module again as part of the script by prepending the following two lines. Of course I'd be happy if somebody could explain why it didn't work the other way...
Install-PackageProvider Nuget -ForceBootstrap -Force
Install-Module -Name SharePointPnPPowerShellOnline -Force -Verbose -Scope CurrentUser

How do I import a PowerShell module for use by a pipeline task

I'm creating a Build Pipeline in Azure DevOps. I have a PowerShell task that invokes a script inside a file - it's not "inline" PowerShell. That script needs the Az.Accounts module so I added Import-Module Az.Accounts. When I run the pipeline, I get the following:
Import-Module : The specified module 'Az.Accounts' was not loaded because no valid module file was found in any module
directory.
At D:\a\1\s\XXX\XXX\XXX.ps1:14 char:1
+ Import-Module Az.Accounts
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (Az.Accounts:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
How do I add the missing module so my script can run?
By default, the hosted windows agent just include the AzureRM module, which is older.With the Az module, Azure PowerShell is now compatible with PowerShell 5.1 on Windows and PowerShell Core 6.x and later on all supported platforms - including Windows, macOS, and Linux.It is the biggest and most important change.
You can add an inline powershell task for installing the Az.Accounts module.
Please note that if you don't add the -force parameter in the script, you will get this information in process.
User declined to install module (Az.Accounts).
The hosted agents don't have the Az modules installed -- they have the older AzureRM modules.
You should be able to use Install-Module to install them. Or you can rewrite your scripts to use AzureRM.
When using Azure Devops with the hosted windows-2019 image, I recommend you use the "AzurePowerShell" task instead of the plain "PowerShell" task, with a version #4 or higher.
documentation of this task can be found here.. Scroll down to see the options related to versioning.
Syntax and arguments are almost identical to the PowerShell task, except that you no longer need to login in Azure (you provide a service connection in the parameters).
You don't need to do crazy stuff like installing the Az module or uninstalling AzureRM. If you use the AzurePowerShell task, it's available for you.
note: don't use Az commands and AzureMR (deprecated) commands in the same script.

Azure DevOps Az Task still loads RM modules and fails to load Az modules

We are trying to extract Azure Advisor recommendations using PowerShell.
Unfortunately, Advisor doesnt seem to have an RM module.
Hence relying on DevOps Azure Task version 4.* which supposedly is for Az modules.
However, it appears that RM modules still gets loaded into the session per error below:
##[warning]Both Az and AzureRM modules were detected on this machine. Az and AzureRM modules cannot be imported in the same session or used in the same script or runbook. If you are running PowerShell in an environment you control you can use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Automation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found here: https://aka.ms/azps-migration-guide
Hence I am resorting to using Uninstall-AzureRM command.
##[error]Could not find a part of the path 'C:\Users\VssAdministrator\Documents\WindowsPowerShell\Modules'.
This is more or less what I am trying.
uninstall-azurerm
install-Module Az -force -scope currentuser
install-Module Az.Advisor -force -scope currentuser
import-module Az.Advisor
Get-AzAdvisor recommendations
Any help is appreciated either using RM or Az modules.

azure powershell prompting login after logging in

I'm trying to use a template to deploy an ASE. I can't use the UI since they made it clear that you can't deploy to a pre-existing subnet in the portal, but you can by using a PS template.
I run the script and it prompts New-AzureRmResourceGroupDeployment : Run Login-AzureRmAccount to login.
But I've logged in.
After researching I've tried
1. Uninstalling AzureRM and re-installing
2. Running update-module and updating
Problem with your the error message is:
Login-AzureRMAccount is available with different modules.
For eg if you are using azureRM module cmdlets you should probably login with:
AzureRM.profile\Login-AzureRmAccount
Have you select the right subscription?
We can use this PowerShell command to select subscription Select-AzureRmSubscription -SubscriptionName.
Also, we should check the version of your Azure PowerShell with this command Get-Module -ListAvailable -Name Azure -Refresh, the latest version is 3.9.0. Please try other commands to make sure the Azure PowerShell works fine.