按计划在 Azure 中启用/禁用可用性测试



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

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

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

这是唯一的办法吗?

编辑:我发布了我在下面得到的错误。

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 运行手册来执行此操作。

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

2.In 运行手册,将脚本添加到登录,完整的脚本应如下所示。(在运行 Runbook 之前,请确保已在自动化帐户中导入AzureRM.ResourcesAzureRM.Profilepowershell 模块 ->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 自动化中计划运行手册,将计划添加到运行手册。

最新更新