3
votes

Trying to set up my VSTS windows build agent to accommodate for powershell steps within VSTS but having some problems. Initially it reported that the AzurePS system capability was not present, so I made sure to install the latest Azure modules via the powershell gallery. Now, the AzurePS capability is present but certain powershell commands do not seem to work.

This may not be related to VSTS at all but rather just a configuration issue with my specific powershell installation / configuration. The full error is as follows:

The term 'Select-AzureRMSubscription' 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.

This error can be seen both in the VSTS release logs of the particular definition I am trying to execute, as well as when attempting to run the Select-AzureRMSubscription command directly on the box itself.

6
With azure powershell step/task, you don't need to call Select-AzureRMSubscription manually. Does it meet your requirement?starian chen-MSFT

6 Answers

7
votes

According to This MS Docs for Get-AzSubscription it's located in the module Az.Accounts.

So I had to run:

Install-module Az -AllowClobber -Force
Import-module Az

to make the command avaliable to me.

1
votes

For me, updating the AzureRM module did the trick:

Update-Module -Name AzureRM

Depending on your system, you may get some messages while installing

Powershell requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\Users\<...>\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running Install-PackagePovider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now?

[Yes] {No] [Suspend]

Choose "Yes"

You are installling the modules form an untrusted repository. If you trust this repository, change its installationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?

[Yes] [Yes to All] [No] [No to All] [Suspend]

Choose "Yes to all" Up to three modules will be installed/updated and directly after that, the Select-AzureRmSubscription should work immediately.

Select-AzureRmSubscription b0cabaca-1234-1337-abcd-bebedada1337

# note: this subscription GUID is completely fictional.
# To get the correct one, query your subscriptions with
# Get-AzureRmSubscription
0
votes

You have to make sure that certain Azure Powershell modules are installed for certain cmdlets to work. You can check which modules are available by running Get-Module -ListAvailable. Select-AzureRMSubscription requires AzureRM module. You could just install Azure Powershell with its modules using webPI - I've never had problems using it and I've heard that installing PS modules using gallery could cause problems (see here).

0
votes

You can run azure PowerShell with Azure PowerShell step/task.

  1. Edit your build definition
  2. Click Add build step
  3. Select Deploy catalog > Add Azure PowerShell step/task
0
votes

the OS in question is server 2012

To install PowerShell on windows server 2012, we should add the .NET Framework 3.5 feature and add the windows PowerShell 2.0 Engine feature.

enter image description here

About how to add features, refer to the link.

Then we can download PowerShell installation package, and install it.

Then we can use windows server 2012 PowerShell to login azure.

0
votes

You need to use the Set-AzContext,

Select-AzSubscription -SubscriptionName 'SubName' | Set-AzContext