azure - 我在 Azure 自动化的 PowerShell 中缺少什么,导致 Start-AzureVM 无法工作?

标签 azure powershell-cmdlet azure-automation

我有以下 PS 脚本,可以在本地正常运行:

Get-AzureVM | Where-Object { $_.Name -eq "my-server-selector" }  | select name | ForEach-Object {
    Write-Output $_.Name 
    Start-AzureVM $_.Name $_.Name
}     

在我的本地 PS 控制台的上下文中,我添加了我的订阅信息,并且代码执行没有问题;所有虚拟机都会打印到输出中,并且服务器会启动。

当我将其移动到云端时,我需要做一些其他事情,即将订阅纳入范围。为此,我在门户中创建凭证 Assets ,通过所述凭证将帐户添加到我的脚本中,然后在脚本中选择正确的订阅。我还将其包装在工作流程中(我打算稍后对某些方面进行参数化)。

最终代码如下:

workflow StartServer
{   
    $credential = GetAutomationPSCredential -Name "credential-asset-name" 

    Add-AzureAccount -Credential $credential

    Select-AzureSubscription -SubscriptionName "subscription-name"


    Write-Output "Starting the server."

    Get-AzureVM | Where-Object { $_.Name -Contains "my-server-selector" }  | select name | ForEach-Object {
        Write-Output $_.Name 
        Start-AzureVM $_.Name $_.Name
    }            

    Write-Output "Execution Complete."

}

如果我删除 Start-AzureVM 命令,工作流将按预期运行。我打印出所有匹配虚拟机的列表。如果我尝试重新输入该命令,则会收到以下错误:

Parameter set cannot be resolved using the specified named parameters.

所以,我认为我知道的事情:

  • 当我获取正确的虚拟机列表时,凭据正在工作
  • 订阅已正确设置,因为它已转储到输出
  • 脚本的内部部分无需任何更改即可在本地 powershell 控制台上运行

任何人都可以提供关于在 Azure 自动化工作流程中需要采取哪些不同措施才能使其正常工作的想法吗?

最佳答案

解决方案是在参数命名中更加明确,无论是在 Where-Object 的过滤器中还是在对 Start-AzureVM 的调用中。我不知道为什么这会有所不同;正如我所说,写入服务器名称的调用在没有显式参数名称的情况下工作,但你瞧,这里它可以使用它设置。

内部 block 最终代码如下:

Get-AzureVM | Where-Object -FilterScript { $_.Name -Contains "my-server-selector" }  | select name | ForEach-Object {
    Write-Output $_.Name 
    Start-AzureVM -ServiceName $_.Name -Name $_.Name
}   

感谢 Twitter 上的 @DexterPOSH 对 -FilterScript 的指导。

关于azure - 我在 Azure 自动化的 PowerShell 中缺少什么,导致 Start-AzureVM 无法工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27161954/

相关文章:

visual-studio-2010 - Azure - 无需 Azure 模拟器即可调试 Web 应用程序

azure - 将存储从 azure 非托管磁盘空间移动到托管

bash - k8s CronJob 在 pod 列表上循环

c# - 在具有不同分区模式的Azure DocumentDB中创建集合

c# - 是否可以使用 C# 中的 Format-Table?

c# - 如何使 PSCmdlet bool 参数像标志一样工作?

c# - 从 C# 调用 Powershell 函数

azure - 自动化帐户重新运行,作业时间表已存在

用于按计划自动缩放 Function App 的 Azure 自动化和逻辑应用

powershell - Azure 自动化上的 Get-AzureStorageKey 错误