function Get-AccessTokenFromContext
{
try {
$accesstoken = (New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient([Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile)).AcquireAccessToken((Get-AzureRmContext).Subscription.TenantId).AccessToken
$buildheaders = @{
'Authorization' = "Bearer $accesstoken"
'Content-Type' = "application/json"
}
return $buildheaders
}
catch
{
Write-Output "No context found! Please run 'Login-AzureRMAccount' to login to Azure"
break
}
}
функция Get-LogAnalyticsAlertRule
{
пары (
$ cur_sub = 'YoursubID',
$ resourceGroupName = 'Имя RG'
)
$ headers = Get-AccessTokenFromContext
$ cur_sub = (Get-AzureRmContext) .Subscription.Id
$ ruleidURI = "https://management.azure.com/subscriptions/$cur_sub/providers/microsoft.insights/scheduledQueryRules" +"? api-version = 2018-04-16 "
$ sqrs = (Invoke-RestMethod -Method GET $ ruleidURI -Headers $ headers) .value
# $ sqrs | Select-Object @ {Name = "DisplayName"; Выражение = {$ .properties.displayname}}, @ {Name = "IsEnabled"; Выражение = {$ .properties.enabled}}, @ {Name = "LastModified"; Expression = {$ .properties.lastUpdatedTime}}, {@ Name = "Рабочее пространство"; Expression = {[регулярное выражение] :: Match ($ .properties.source.dataSourceId "( ? <= / workspaces /)(.<em>) "). value}}, @ {Name =" Группа ресурсов "; Expression = {[regex] :: Match ($ _. properties.source.dataSourceId," ( ? <= / resourceGroups /)(.</em>) (? = / provider) "). value}} | Format-Table
$ sqrs | Select-Object name, @ {Name = "DisplayName"; Выражение = {$ .properties.displayname}}, @ {Name = "IsEnabled"; Выражение = {$ .properties.enabled}}, @ {Name = "Рабочее пространство";. Выражение = {[регулярное выражение] :: Match ($ .properties.source.dataSourceId "? (<= / рабочие области /)(.*)") значение}}, {@ Имя = "Группа ресурсов"; Выражение = {[regex] :: Match ($ </em> .properties.source.dataSourceId, "(? <= / ResourceGroups /)(.*)(?=/ поставщики)"). значение}} | Format-Table -AutoSize -Wrap </p>
$sqrs_prop = $sqrs.properties
$rule_name_list = $sqrs_prop.DisplayName
foreach($rule_name in $rule_name_list){
Write-Host "disabling $rule_name"
if($rule_name -ne $null){
$ruleUri = "https://management.azure.com/subscriptions/$cur_sub/resourcegroups/$resourceGroupName/providers/microsoft.insights/scheduledQueryRules/$rule_name"+"?api-version=2018-04-16"
$bodyEnable = "
{
'properties': {
'enabled': 'false'
}
}
"
Write-Verbose "ResourceURI being invoked: $ruleUri"
try {
$disablerule = Invoke-RestMethod -Method PATCH -Uri $ruleUri -Headers $headers -Body $bodyEnable
$disablerule | Select-Object @{Name="displayName";Expression={$_.properties.displayName}}, @{Name="IsEnabled";Expression={$_.properties.enabled}},@{Name="lastUpdate";Expression={$_.properties.lastUpdatedTime}}, @{Name="provisioningState";Expression={$_.properties.provisioningState}} | Format-Table -AutoSize -Wrap
Write-Verbose "Output of Invoke-RestMethod: $disablerule"
}
catch
{
Write-Error "$_"
}
}
}
}
Get-LogAnalyticsAlertRule