Azure Cloud Shell - The term 'Stop-AzureVM' is not recognized - azure

I am attempting to use the Azure Cloud Shell (browser based) to manage a classic VM. When I run this command:
Stop-AzureVM -ResourceGroupName <resourceGroup> -Name <vmName>
I get this error:
Stop-AzureVM : The term 'Stop-AzureVM' 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.
At line:1 char:1
+ Stop-AzureVM -ResourceGroupName <resourceGroup>-Name <vmName>
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Stop-AzureVM:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Am I doing anything wrong, or is this just not supported in Cloud Shell?

By default, AzureRM Module is used. For PowerShell on Azure Cloudshell, you need to install classic ASM module. Just simply run
Install-Module Azure
Try to use Get-Module -Name Azure* to see if the ASM module is installed.
Now you can play with your Azure classic resource.
If you have more than one subscription in ASM model, you need to set the subscription first, using
Select-AzureSubscription -Default -SubscriptionName "your_subscription_name"

but that gives me Unable to load DLL 'IEFRAME.dll'
Azure cloud shell does not support use another account to login it.
Cloud Shell also securely authenticates automatically for instant
access to your resources through the Azure CLI 2.0 or Azure PowerShell
cmdlets.
As a workaround, maybe you can use cloud shell bash to stop that VM(classic).
Azure CLI 1.0 support ASM and ARM, just change mode to ASM, you can use cloud shell to manage your classic VM, like this:
azure config mode asm
azure vm list
azure vm --help //get more information about CLI 1.0
Hope this helps.

Related

'The term 'Set-AzureRmDataFactoryV2' is not recognized as the name of a cmdlet' error in Pipeline Azure Devops

Azure Powershell AZ module still does not work in azure devops pipeline?
I get this error when I try powershell version 4+ and the agent is 2017 (also tried windows 2019)
#Install-Module -Name Az -AllowClobber -Scope CurrentUser (is this needed? I tried with and #without and it fails)
Set-AzureRmDataFactoryV2 -ResourceGroupName "myRG" -Name "LLmenADF1" -Location "North Europe"
If you are using Powershell Az module, then the cmdlet is:
Set-AzDataFactoryV2
Your cmdlet makes reference to the old library, AzureRM. To get the same cmdlet when migrating from one library to another, just replace AzureRM with Az.
Hope this helped!
#Martin Esteban Zurita has the correct recommended solution to migrate to Az commands
For those that can't/won't for any reason, just select Version 3 of the Azure Powershell task to work with AzureRM commands.

Azure Powershell: Az and AzureRM modules cannot be imported in the same session

I have the below script that needs to remove files from an Azure Storage Container that are older than 7 days and it has suddenly stopped working:
$StorageAccountName = "#"
$StorageAccountKey = "#"
$ContainerName = "#"
$CleanupOlderThanDays = [DateTime]::UtcNow.AddDays(-7)
$FolderName = "Daily"
$Ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Get-AzStorageBlob -Container "$ContainerName" -Context $Ctx -Prefix $FolderName/ | Where-Object { $_.LastModified.UtcDateTime -lt $CleanupOlderThanDays } |Remove-AzStorageBlob
It fails now with the below error:
PS C:\backupcopy> .\remove_files_daily.ps1
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.
Get-AzStorageBlob : The 'Get-AzStorageBlob' command was found in the module 'Az.Storage', but the module could not be
loaded. For more information, run 'Import-Module Az.Storage'.
At C:\backupcopy\remove_files_daily.ps1:8 char:1
+ Get-AzStorageBlob -Container "$ContainerName" -Context $Ctx -Prefix $ ...
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-AzStorageBlob:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
I tried running Uninstall-AzureRm but I get the following error:
PS C:\backupcopy> Uninstall-AzureRm
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
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.
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.
Uninstall-AzureRm : The 'Uninstall-AzureRm' command was found in the module 'Az.Accounts', but the module could not be
loaded. For more information, run 'Import-Module Az.Accounts'.
At line:1 char:1
+ Uninstall-AzureRm
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Uninstall-AzureRm:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
I then tried running Import-Module Az.Accounts and got this:
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
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.
Get-ChildItem : 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.
At C:\Program Files\WindowsPowerShell\Modules\Az.Accounts\1.6.2\Az.Accounts.psm1:39 char:5
+ Get-ChildItem "$PSScriptRoot\StartupScripts" -ErrorAction Stop | ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (AzureRM.Profile...igration-guide.:String) [Get-ChildItem], RuntimeExc
eption
+ FullyQualifiedErrorId : AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same se
ssion or used in the same script or runbook. If you are running PowerShell in an environment you control you can u
se the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Aut
omation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found he
re: https://aka.ms/azps-migration-guide.,Microsoft.PowerShell.Commands.GetChildItemCommand
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.
Import-Module : 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.
At line:1 char:1
+ Import-Module Az.Accounts
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (AzureRM.Profile...igration-guide.:String) [Import-Module], RuntimeExc
eption
+ FullyQualifiedErrorId : AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same se
ssion or used in the same script or runbook. If you are running PowerShell in an environment you control you can u
se the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Aut
omation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found he
re: https://aka.ms/azps-migration-guide.,Microsoft.PowerShell.Commands.ImportModuleCommand
Does anyone know what I need to do to get this working? I'm running
Major Minor Build Revision
----- ----- ----- --------
5 1 14409 1005
You could not mix the Az and AzureRm commands together, change the first line in your script as below, then try it in a new powershell session.
$Ctx = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
If you still get the same error, run a new powershell session as admin, use the command Uninstall-Module as below, then try the script again.
Uninstall-Module -Name AzureRm -AllVersions -Force
I had this same issue when trying to deploy a Web App to Azure using Octopus deploy with the Run a PowerShell script using an Azure subscription with Azure modules loaded template.
I had Az modules for Azure Powershell installed on the Octopus server:
Install-Module -Name Az -Repository PSGallery -Force
I also uninstalled all AzureRm modules on the Octopus server:
Uninstall-AzureRm
However, when I try deploying I get this error:
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.
Invoke-Expression : The 'New-AzStorageAccount' command was found in the module
'Az.Storage', but the module could not be loaded. For more information, run
'Import-Module Az.Storage'.
At C:\Octopus\Work\20210326200138-344663-101\Octopus.AzureContext.ps1:143
char:5
+ Invoke-Expression ". $OctopusAzureTargetScript $OctopusAzureTarge ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (New-AzStorageAccount:String) [I
nvoke-Expression], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule,Microsoft.PowerSh
ell.Commands.InvokeExpressionCommand
Fatal
The remote script failed with exit code 1
The action Create Azure Storage Account for Test, Staging and Production on the Octopus Server failed
Here's I fixed it:
The issue was Run a PowerShell script using an Azure subscription with Azure modules loaded template has AzureRm modules loaded on it, which conflicts with the Az modules that I installed on the Octopus server.
Since I did not want to modify my Powershell scripts to use the AzureRm modules, all I had to do was to Run a PowerShell script using an Azure subscription with Azure modules loaded template and use the normal Run a Powershell script template which allowed me to communicate with the Az modules installed on the Octopus server without interfering with the AzureRm modules from Octopus.
This meant that I had to modify my script now had to handle the authentication to the Azure account before it could run any command, which was what the Run a PowerShell script using an Azure subscription with Azure modules loaded template was initially supposed to help me achieve.
That's all.
I hope this helps

Azure runbook automation - Invoke-AzureRmVMRunCommand error

I have an Azure runbook that runs on schedule. Its in powershell and this runbook starts a VM and executes a script on the VM started. How I achieve this is
1) Store the script to be run on the VM in a storage account
2) Run powershell runbook
3) Powershell runbook uses wget command to copy the script from step 1
4) Invoke-AzureRmVMRunCommand in the Azure automation powershell commands as shown below
wget "https://utilitystorageaccnt.blob.core.windows.net/utilitycontainer/token" -outfile ((Get-Location).path + "\Reporting Copy.ps1") -UseBasicParsing
Invoke-AzureRmVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VmName -CommandId 'RunPowerShellScript' -ScriptPath ((Get-Location).path + '\Reporting Copy.ps1') -ErrorVariable result
Please not that the above two commands are in the powershell runbook script and not the actual script that is run on the VM.
Facing two issues
1) When this script Reporting Copy.ps1 runs standalone on the VM, then it works properly and it has no issues. When it is run using the runbook, I get these errors in the log file on the target vm.
"New-AzStorageContext : The term 'New-AzStorageContext' 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."
2) Even after this error occurs, it doesnt terminate and runs in loops. This script does a copy operation and it keeps looping until all the copy is complete. I can handle code to terminate but I would like to know how to force terminate a runbook. I tried to stop the VM for even a hour and it resumes the copy operation. The runbook status in Azure shows as completed. There are two python processes that show in explorer and terminating them doesn't work either.
Any help or hint is appreciated.
Thanks.
Look like you did not imported Az PowerShell module into our Automation Account.
Please, follow this tutorial : Az module support in Azure Automation
Try to use only Az module and not AzureRM
The issue was because I had not installed the AZ module for all users like this.
Install-Module -Name Az -AllowClobber -Scope AllUsers
Instead I had used
Install-Module -Name Az -AllowClobber -Scope CurrentUser
and since the automation runs on a different user, the issue occurred. Thanks for your help.

Can I create an azure app using azure devops

I am trying to automate the process of an Azure Active Directory (AAD) app registration using Azure DevOps release pipeline but it fails to do so. (Please note that the same command (powershell commands as well as azure commands) works perfectly fine if I am running the same commands from my laptop) and for that I created an azure powershell task in the release pipeline and used the following line of code in the "inline script section":
I tried creating the AAD app registration using the following 2 methods:
1. "Azure Powershell script task"
2. Azure commands
Following is inline script that I used in case of azure powershell task:
Import-Module AzureRM
Import-Module AzureAD
# Register an AAD app
$appURI = "https://knaabdapp123.azurewebsites.net"
$appHomePageUrl = "https://knaabdapp123.knandan.in"
$appReplyURLs = #($appURI, $appHomePageURL, "https://localhost:12345")
New-AzureADApplication -DisplayName knaabdapp123 -IdentifierUris $appURI -Homepage $appHomePageUrl -ReplyUrls $appReplyURLs
I get the following error when I do so:
2019-08-09T11:27:31.1039145Z ##[section]Starting: Azure PowerShell script: Register an AAD app and generate credential for the same
2019-08-09T11:27:31.1162119Z ==============================================================================
2019-08-09T11:27:31.1162226Z Task : Azure PowerShell
2019-08-09T11:27:31.1162310Z Description : Run a PowerShell script within an Azure environment
2019-08-09T11:27:31.1162378Z Version : 2.153.1
2019-08-09T11:27:31.1162446Z Author : Microsoft Corporation
2019-08-09T11:27:31.1162520Z Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/deploy/azure-powershell
2019-08-09T11:27:31.1162620Z ==============================================================================
2019-08-09T11:27:37.0179906Z ##[command]Import-Module -Name C:\Modules\AzureRm_5.1.1\AzureRM\5.1.1\AzureRM.psd1 -Global
2019-08-09T11:28:10.7554409Z ##[command]Clear-AzureRmContext -Scope Process
2019-08-09T11:28:11.2755157Z ##[command]Disable-AzureRmContextAutosave -ErrorAction Stop
2019-08-09T11:28:15.0230853Z ##[command]Add-AzureRMAccount -ServicePrincipal -Tenant *** -Credential System.Management.Automation.PSCredential -Environment AzureCloud #processScope
2019-08-09T11:28:16.5226685Z ##[command] Select-AzureRMSubscription -SubscriptionId a*******-ae1c-****-****-********** -TenantId ***
2019-08-09T11:28:16.8648715Z ##[command]& 'C:\Users\VssAdministrator\AppData\Local\Temp\2a55****-67c6-****-8f80-**********.ps1'
2019-08-09T11:28:17.0308219Z ##[error]The specified module 'AzureAD' was not loaded because no valid module file was found in any module directory.
2019-08-09T11:28:19.0607544Z ##[command]Remove-AzureRmAccount -Scope Process -ErrorAction Stop
2019-08-09T11:28:19.4371114Z ##[command]Clear-AzureRmContext -Scope Process -ErrorAction Stop
2019-08-09T11:28:19.8885329Z ##[error]The term 'New-AzureADApplication' 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.
I also used the Azure CLI task and used the following script, but that fails too:
az ad app create --display-name MyApplication123 --homepage "https://myapplication1232.nl" --reply-urls "https://localhost:12345" --identifier-uris "https://myapplication2.azurewebsites.net"
I get the following error in this case:
>az ad app create --display-name MyApplication123 --homepage "https://myapplication1232.nl" --reply-urls "https://localhost:12345" --identifier-uris "https://myapplication2.azurewebsites.net"
2019-08-09T11:47:46.5676945Z ERROR: Insufficient privileges to complete the operation.
2019-08-09T11:47:46.6721317Z ##[error]Script failed with error: Error: d:\a\_temp\azureclitaskscript1565351201021.bat failed with return code: 1
So, I have 2 questions:
Is it possible to create an AAD app registration using "Azure powershell" task script or "Azure CLI" task in Azure DevOps?
If yes, then what may I be doing wrong?
Is it possible to create an AAD app registration using "Azure
powershell" task script or "Azure CLI" task in Azure DevOps?
For this question, the answer is Yes, of course you can.
The cause of the error you received in Azure Powershell task is as default, the AzureAD powershell cmdlets will not be installed in agent.So, if you try to using this module directly, you will receive the message like "##[error]The specified module 'AzureAD' was not loaded because no valid module file was found in any module directory."
If yes, then what may I be doing wrong?
To solve this error message, please try with replacing your script Import-Module AzureAD as the follow script to use a correct way to get the AzureAD module.
$AzureADModulePath = $PSScriptRoot + "\AzureAD\2.0.1.16\AzureAD.psd1"
Import-Module $azureAdModulePath
This is the detailed info about AzureAD module in Powershell Gallery. And also, here has a blog you can refer.
Updated:
You must install AzureAD cmdlets module from the PowerShell gallery with the below script first:
Install-Module -Name AzureAD -RequiredVersion 2.0.1.16
Note: If get error like Install-Module : Administrator rights are required to install modules while you install with this script, please add -scope CurrentUser to the install script. It will running the script as administrator role.
And then, import the module from the installed path afterwards.

The term 'Set-AzureRmVMPlan' is not recognized as the name of a cmdlet

I am trying to create a VM on Azure using powershell script in Azure Automation. This VM is to be created from marketplace. So I need to use cmdlet 'Set-AzureRmVMPlan' in my script.
$vm = New-AzureRmVMConfig -VMName $inVMName -VMSize $vmSize
Set-AzureRmVMPlan -VM $vm -Publisher "kali-linux" -Product
"kali-linux" -Name "kali"
This script works fine when I run it from Powershell ISE however when I put this command in Azure Automation Runbook then Azure doesn't recognize this command. I get below error,
Set-AzureRmVMPlan : The term 'Set-AzureRmVMPlan' 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.
At restore-vhd-backups:8 char:8
CategoryInfo : ObjectNotFound: (Set-AzureRmVMPlan:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
So I further investigate and found that this command is not available in cmdlet list at page "Edit PowerShell Runbook" (see below image)
I also got to know that this cmdlet was added in Azure Powershell last year from below link
How to include "Plan information" when creating ARM VM from a captured image using Powershell?
Can someone help me on this?
So what is happening here, Azure Automation uses the old version of the Module, you have to go to Azure Automation Blade > Assets > Modules > Browse Gallery and reimport AzureRM.Profile and AzureRM.Compute.

Resources