0
votes

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

7
may I know what exactly you are trying to achive using command- Get-AzureRMVM I googled this command but couldn't get anythingAatif Akhter
Just trying to get my ARM VMs.Farouk

7 Answers

1
votes

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.

0
votes

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

0
votes

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.

0
votes

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.

0
votes

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
}
0
votes

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.

0
votes

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.