Farouk
Farouk

Reputation: 17

How to stop all VMs with Azure Automation using Resource Manager module?

I have created some Azure VMs using the new Resource Manager and i'd like to stop them everyday.

To do so, i've published a runbook to stop aboth classic and ARM VMs, and i created a scheduler which runs the runbook every night :

workflow Stop-AzureVMs 
{ 
    $cred = Get-AutomationPSCredential -Name 'Cred'
    Add-AzureAccount -Credential $cred
    Select-AzureSubscription -Current 'SubscriptionName'

    Get-AzureVM | Stop-AzureVM –Force
    Get-AzureRmVM | Stop-AzureRmVM -Force
}

I have imported the AzureResourceManager module to my Azure Automation account :

Azure Automation Modules

But i am getting this error :

Exception
At line:34 char:2
 + Get-AzureRMVM | Stop-AzureRMVM -Force
 + ~~~~~~~~~~~~~ Cannot find the 'Get-AzureRMVM' command. If this command is defined as a workflow, ensure it is defined before the workflow that calls it. If it is a command intended to run directly within Windows PowerShell (or is not available on this system), place it in an InlineScript: 'InlineScript { Get-AzureRMVM }'

How is that possible ?

Edit : Below is the solution

    $cred = Get-AutomationPSCredential -Name 'Cred'

    Add-AzureRmAccount -Credential $cred
    Select-AzureRmSubscription -Name 'SubscriptionName' -SubscipritionId 'SubscriptionId' 

    Get-AzureRmVM | Stop-AzureRmVM -Force

All workflows i found didn't mention the use of Add-AzureRmAccount and Select-AzureRmSubcription instead of the standard Add-AzureAccount and Select-AzureSubscription. I thought that the authentication process to our Azure account was the same.

Update : It is now possible to combine both ASM and ARM cmdlets within the same runbooks, see this post for more informations about ARM supported by default on Azure Automation

Upvotes: 0

Views: 5125

Answers (7)

Rahul Soni
Rahul Soni

Reputation: 4968

This might be late to the party, but I would recommend you check out this link:

https://www.attosol.com/start-or-stop-all-vms-of-a-resource-group-in-azure/

Basically, you can create a script and write some aliases with switches to make your job super easy.

Upvotes: 0

Sadjad Abdoli
Sadjad Abdoli

Reputation: 351

For new Azure RM VMs use access extensions the following command:

Set-AzureRmVMAccessExtension -ResourceGroupName "ResourceGroupName" -VMName "VMName" -Username "Admin User Name" -Password "Admin Password" -Name "Extension Name"

Please note the -Name parameter is the arbitrary extension name.

Upvotes: 0

Nigel Lunn
Nigel Lunn

Reputation: 1

The Following code will work for both old style and new Style VM's but be aware this will shut down all machines with no warning.

{
    # TODO: update to the name of the credential asset in your Automation account
    $AutomationCredentialAssetName = "AzureAutomationRG"

    # Get the credential asset with access to my Azure subscription
    $Cred = Get-AutomationPSCredential -Name $AutomationCredentialAssetName

    # Authenticate to Azure Service Management and Azure Resource Manager
    Add-AzureAccount -Credential $Cred 
    Add-AzureRmAccount -Credential $Cred 

    "`n-Old Style VMS-`n"

    # Get and output Azure classic VMs
    $VMs = Get-AzureVM
    $VMs.Name

    Get-AzureVM | Stop-AzureVM -Force

    "`n-New Style Resource Group VMs-`n"

    # Get and output Azure v2 VMs
    $VMsv2 = Get-AzureRmVM
    $VMsv2.Name

    Get-AzureRmVM | Stop-AzureRmVM -Force
}

Upvotes: 0

Farouk
Farouk

Reputation: 17

Below is the solution

$cred = Get-AutomationPSCredential -Name 'Cred'

Add-AzureRmAccount -Credential $cred
Select-AzureRmSubscription -Name 'SubscriptionName' -SubscriptionId 'SubscriptionId' 

Get-AzureRmVM | Stop-AzureRmVM -Force

It is not yet possible to combine ARM and ASM cmdlets in same runbook apparently ... So you have to use only ARM cmdlet or ASM cmdlet.

Also, all workflows i found didn't mention the use of Add-AzureRmAccount and Select-AzureRmSubcription instead of the standard Add-AzureAccount and Select-AzureSubscription.

I thought that the authentication process to our Azure account was the same.

Upvotes: 0

Joe
Joe

Reputation: 2540

Looks like you imported the old version of the ARM cmdlets (before Azure PS 1.0) into Azure Automation. This was before the *-AzureRm* renaming. So tt should be Stop-AzureVM not Stop-AzureRmVM.

However, that makes it ambiguous as to whether you are trying to call Azure Service Management or Azure Resource Manager cmdlets -- which is exactly why the cmdlet names were renamed in Azure PS 1.0. I recommend you follow the guidance here.

Upvotes: 1

bmoore-msft
bmoore-msft

Reputation: 8717

The Get-AzureRMVM cmdlet is in the AzureRM.Compute module... The AzureRM* cmdlets are still in preview, I don't think they are available in Azure Automation yet.

The two modules in your screenshot above likely correspond to the 0.9.x version of the cmdlets and there were indeed two different modules (Azure=ASM and AzureResourceManager=ARM) behind Switch-AzureMode. Switch-AzureMode just unloads one and loads the other.

If Automation is still using the 0.9.x version of the cmdlets then you should be able to just use Get-AzureVM for ARM VMs using the AzureResourceManager module.

Upvotes: 0

Aatif Akhter
Aatif Akhter

Reputation: 2206

As per my understanding ASM mode is default. If you are going for ARM command firstly switch mode is required using Switch-AzureMode

One more confusion is what is the purpose of Get-AzureRMVM command. I googled but coulndn't find anything -

enter image description here

Upvotes: 0

Related Questions