首页 > 解决方案 > 按计划在 Azure 中启用/禁用可用性测试

问题描述

我想知道是否有一种简单的方法可以在 Azure 中运行计划的自动化命令。

我设法为可用性测试编写了启用/禁用命令

Azure CLI:

az resource update --set properties.enabled=true --name 'someName' --resource-type 'Microsoft.Insights/webtests' --resource-group 'soemResourceGroup'

电源外壳:

#Get All webTests
$resourceGroupnames = "someGroupName1", "someGroupName2";
$enableTests = "True";

ForEach ($resourceGroupname in $resourceGroupnames) { 
    $resourceGroupname
    $allAvailabilityTestsIds = Get-AzureRmResource -ResourceGroupName $resourceGroupname `
    | Where-Object -Property ResourceType -EQ "microsoft.insights/webtests" `
    | Select-Object -ExpandProperty ResourceId;

    ForEach ($availabilityTestId in $allAvailabilityTestsIds) { 
        $availabilityTest = Get-AzureRmResource -ResourceId $availabilityTestId;
        $availabilityTest.Properties.Enabled = $enableTests;
        $availabilityTest | Set-AzureRmResource -Force;
    }
}

问题是我不确定在 Comamnd 线之外并按时运行它们。我已经读到我可以使用自动化帐户来使用 powershell 脚本,但这似乎是一场噩梦,因为我在身份验证方面遇到了很多问题(不知道为什么)。

这是唯一的方法吗?

编辑: 我发布了我正在/正在得到的错误。

Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument 
that is not null or empty, and then try the command again.
At line:37 char:29
+         $availabilityTest | Set-AzureRmResource -Force;
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource], 
ParameterBindingValidationException
    + FullyQualifiedErrorId : 
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet

Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument 
that is not null or empty, and then try the command again.
At line:37 char:29
+         $availabilityTest | Set-AzureRmResource -Force;
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource], 
ParameterBindingValidationException
    + FullyQualifiedErrorId : 
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet

Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument 
that is not null or empty, and then try the command again.
At line:37 char:29
+         $availabilityTest | Set-AzureRmResource -Force;
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource], 
ParameterBindingValidationException
    + FullyQualifiedErrorId : 
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet

Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument 
that is not null or empty, and then try the command again.
At line:37 char:29
+         $availabilityTest | Set-AzureRmResource -Force;
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource], 
ParameterBindingValidationException
    + FullyQualifiedErrorId : 
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet

Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument 
that is not null or empty, and then try the command again.
At line:37 char:29
+         $availabilityTest | Set-AzureRmResource -Force;
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource], 
ParameterBindingValidationException
    + FullyQualifiedErrorId : 
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet

Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument 
that is not null or empty, and then try the command again.
At line:37 char:29
+         $availabilityTest | Set-AzureRmResource -Force;
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource], 
ParameterBindingValidationException
    + FullyQualifiedErrorId : 
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet

Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument 
that is not null or empty, and then try the command again.
At line:37 char:29
+         $availabilityTest | Set-AzureRmResource -Force;
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource], 
ParameterBindingValidationException
    + FullyQualifiedErrorId : 
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet

问候。

标签: azureazure-runbook

解决方案


您可以按照以下步骤在自动化中使用 azure Runbook 来执行此操作。

1.导航到您的自动化帐户 -> Runbooks-> Create a runbook-> 创建Powershell运行手册。

2.在运行手册中,添加登录脚本,您的完整脚本应如下所示。(在运行 Runbook 之前,请确保您已在自动化帐户 -> 中导入了AzureRM.ResourcespowershellAzureRM.Profile模块Modules,如果没有,请在Modules->Browse Gallery中搜索模块并导入它们。)

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

#Get All webTests
$resourceGroupnames = "someGroupName1", "someGroupName2";
$enableTests = "True";

ForEach ($resourceGroupname in $resourceGroupnames) { 
    $resourceGroupname
    $allAvailabilityTestsIds = Get-AzureRmResource -ResourceGroupName $resourceGroupname `
    | Where-Object -Property ResourceType -EQ "microsoft.insights/webtests" `
    | Select-Object -ExpandProperty ResourceId;

    ForEach ($availabilityTestId in $allAvailabilityTestsIds) { 
        $availabilityTest = Get-AzureRmResource -ResourceId $availabilityTestId;
        $availabilityTest.Properties.Enabled = $enableTests;
        $availabilityTest | Set-AzureRmResource -Force;
    }
}

3.成功运行脚本后,点击此链接在 Azure 自动化中调度 Runbook以将计划添加到您的 Runbook。


推荐阅读