Enable/Disable 按计划在 Azure 中进行可用性测试

Enable/Disable availability tests in Azure on Schedule

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

我设法在

中为可用性测试编写了 Enable/Disable 命令

Azure CLI:

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

Powershell:

#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 脚本,但这似乎是一场噩梦,因为我遇到了很多身份验证问题(不知道为什么)。

这是唯一的方法吗?

编辑: 我 post 错误我 was/am 低于。

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

此致。

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

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

2.In runbook,添加脚本登录,你的完整脚本应该如下所示。 (在 运行 runbook 之前,请确保您已在自动化帐户中导入 AzureRM.ResourcesAzureRM.Profile powershell 模块 -> 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.After 运行 脚本成功,按照此 link Scheduling a runbook in Azure Automation 将计划添加到您的 runbook。