简体   繁体   English

如何使用资源管理器模块停止具有Azure自动化的所有VM?

[英]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. 我使用新的资源管理器创建了一些Azure VM,我想每天停止它们。

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 : 为此,我已经发布了一个停止经典和ARM VM的运行手册,并且创建了一个调度程序,该调度程序每天晚上运行该运行手册:

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 : 我已将AzureResourceManager模块导入到我的Azure自动化帐户中:

Azure自动化模块

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. 我发现的所有工作流程都没有提到使用Add-AzureRmAccount和Select-AzureRmSubcription来代替标准的Add-AzureAccount和Select-AzureSubscription。 I thought that the authentication process to our Azure account was the same. 我认为对我们的Azure帐户的身份验证过程是相同的。

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 更新:现在可以将ASM和ARM cmdlet合并在同一本运行手册中,请参阅此文章,以获取有关Azure自动化默认支持的ARM的更多信息。

As per my understanding ASM mode is default. 根据我的理解,ASM模式是默认的。 If you are going for ARM command firstly switch mode is required using Switch-AzureMode 如果您要使用ARM命令,则首先需要使用Switch-AzureMode切换模式

One more confusion is what is the purpose of Get-AzureRMVM command. 另一个困惑是Get-AzureRMVM命令的目的是什么。 I googled but coulndn't find anything - 我用Google搜索,但找不到任何内容-

在此处输入图片说明

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. Get-AzureRMVM cmdlet在AzureRM.Compute模块中... AzureRM * cmdlet仍在预览中,我认为它们在Azure自动化中尚不可用。

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. 上方屏幕快照中的两个模块可能对应于cmdlet的0.9.x版本,而Switch-AzureMode后面确实有两个不同的模块(Azure = ASM和AzureResourceManager = ARM)。 Switch-AzureMode just unloads one and loads the other. Switch-AzureMode只是卸载一个而另一个。

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. 如果Automation仍在使用cmdlet的0.9.x版本,则应该可以使用AzureResourceManager模块将Get-AzureVM用于ARM VM。

Looks like you imported the old version of the ARM cmdlets (before Azure PS 1.0 ) into Azure Automation. 看起来您将旧版本的ARM cmdlet(在Azure PS 1.0之前)导入了Azure Automation。 This was before the *-AzureRm* renaming. 这是在*-AzureRm*重命名之前。 So tt should be Stop-AzureVM not Stop-AzureRmVM . 因此,tt应该是Stop-AzureVM而不是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. 但是,这使得您是否要调用Azure服务管理或Azure资源管理器cmdlet模棱两可-这正是在cmdlet名称在Azure PS 1.0中重命名的原因。 I recommend you follow the guidance here . 我建议您按照此处的指导进行操作。

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. 显然,不可能将ARM和ASM cmdlet合并在同一个Runbook中。因此,您只需要使用ARM cmdlet或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. 另外,我发现的所有工作流程都没有提到使用Add-AzureRmAccount和Select-AzureRmSubcription来代替标准的Add-AzureAccount和Select-AzureSubscription。

I thought that the authentication process to our Azure account was the same. 我认为对我们的Azure帐户的身份验证过程是相同的。

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. 以下代码适用于旧样式和新样式VM,但是请注意,这将关闭所有计算机,而不会发出警告。

{
    # 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
}

For new Azure RM VMs use access extensions the following command: 对于新的Azure RM VM,使用访问扩展以下命令:

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. 请注意, -Name参数是任意扩展名。

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/ 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. 基本上,您可以创建一个脚本并使用开关编写一些别名,以使您的工作变得非常容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用Azure自动化停止VM - Unable to stop VMs using Azure Automation Azure自动化-如何在不使用资源管理器帐户的情况下进行身份验证 - Azure Automation - how to authenticate without using an account with resource manager 使用Azure资源管理器库的ClassicCompute VM列表 - List of ClassicCompute VMs using Azure Resource Manager library Azure:如何更改Resource Manager托管VM / IP的空闲超时 - Azure: How to change idle timeout for Resource Manager managed VMs/IPs Azure 使用自动化运行手册访问私有 VM - Azure private VMs access using Automation runbooks 如何通过门户将端点映射添加到Azure IaaS VM(新资源管理器)? - How to add endpoint mapping to Azure IaaS VMs (new resource manager) via Portal? 如何为资源管理器VM使用Azure提供的DNS? - How can I use Azure-provided DNS for Resource Manager VMs? 无法通过 Azure 自动化帐户使用 azure 资源管理器模板部署 azure 存储 - cannot deploy azure storage using azure resource manager template with Azure Automation Account 如何在多个 azure vms 上停止服务? - How to stop a service on multiple azure vms? 使用Azure Automation中的服务主体向Azure Resource Manager进行身份验证 - Authenticating to Azure Resource Manager with a Service Principal in Azure Automation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM