为另一个OMS工作区运行脚本时出错 [英] Error Running Script for another OMS workspace

查看:69
本文介绍了为另一个OMS工作区运行脚本时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家

我有一个脚本可以导出在OMS(日志分析)中创建的所有警报规则的列表.运行正常,我可以在名为OMSAlert.csv的临时文件夹中看到结果.但是,当我在另一个Azure订阅中为工作区运行相同的脚本时, 错误.它有点奇怪,我也不知道为什么会发生.

I have a script to export list of all alert rule created in OMS (Log Analytics). It is running fine and i can see result in my temp folder with name OMSAlert.csv. But when i am running same script in my another Azure subscription for a workspace i am getting error. Its bit strange and i am not ware why its is happening. 

代码如下:-

param (
    #The subscription id where the OMS is located
    [Parameter(Mandatory)]
    [string]$SubscriptionId,  

    #The name of the OMS workspace.
    [Parameter(Mandatory)]
    [string]$WorkspaceID,

    #Notification Group shortname
    [string]$FilePath="c:\temp"
)

$ErrorActionPreference = "Stop"

#Variables

$OutPath=$FilePath + "\OMSalerts.csv"

#Getting all Workspaces

$allWorkspaces = armclient get "/subscriptions/$SubscriptionId/providers/Microsoft.OperationalInsights/workspaces?api-version=2015-03-20" | ConvertFrom-Json

ForEach($Workspace in $allWorkspaces.value)
{

    if($Workspace.name -eq $WorkspaceID)
    {
    
    $url = $Workspace.id

    }

}

Write-Host "Building table for results"  
  
#Build a table to format the results  
$table = New-Object system.Data.DataTable "Results"  
$col1 = New-Object system.Data.DataColumn Name,([string])  
$table.columns.add($col1)  
$col2 = New-Object system.Data.DataColumn Description,([string])  
$table.columns.add($col2)  
$col3 = New-Object system.Data.DataColumn Threshold,([string])  
$table.columns.add($col3)  
$col4 = New-Object system.Data.DataColumn Severity,([string])  
$table.columns.add($col4)
$col5 = New-Object system.Data.DataColumn Query,([string])  
$table.columns.add($col5)
$col6 = New-Object system.Data.DataColumn Interval,([string])  
$table.columns.add($col6)  
$col7 = New-Object system.Data.DataColumn QueryTimeSpan,([string])  
$table.columns.add($col7)  
$col8 = New-Object system.Data.DataColumn QueryEnabled,([boolean])  
$table.columns.add($col8)    

#Get all searches

Write-Host "Getting all searches for Workspace"  

$Searches = armclient get "$url/savedsearches?api-version=2015-03-20" | ConvertFrom-Json

if($Searches.value -ne $null)
{

Write-Output "Searches found: $($Searches.value.count)"

}

else
{

Write-Output "No searches found, exiting script"

Break

}

Write-Host "Getting schedules for Searches"  

$Searches.value.id | %{
    
    $search = armclient get "$_/?api-version=2015-03-20" | ConvertFrom-Json
    
    $schedules = armclient get "$_/schedules?api-version=2015-03-20" | ConvertFrom-Json

    $schedules | %{

    $schedule = $_.value.properties

    $actions = armclient get "$($_.value.id)/actions?api-version=2015-03-20" | convertfrom-json

    $actions | %{

        if ($_.value.properties.Type -eq "Alert"){

        Write-Host "Alert found: $($_.value.properties.Name)"  
        
        $Name=$_.value.properties.Name
        $Description=$_.value.properties.Description
        $Threshold=$_.value.properties.Threshold
        $Severity=$_.value.properties.Severity
        $Query=$search.properties.Query
        $Interval=$schedule.Interval
        $QueryTimeSpan=$schedule.QueryTimeSpan
        $QueryEnabled=$schedule.Enabled
           
        $row = $table.NewRow()  
        $row.Name = $Name
        $row.Description = $Description  
        $row.Threshold = $Threshold.Value 
        $row.Severity = $Severity
        $row.Query = $Query
        $row.Interval = $Interval
        $row.QueryTimeSpan = $QueryTimeSpan
        $row.QueryEnabled = $QueryEnabled  
        $table.Rows.Add($row)
        }
    }
        
    }
    }

Write-Host "Exporting results"  

$table | Export-Csv $OutPath -NoTypeInformation


我得到的错误如下:-

convertfrom-json:无效的JSON原语:.
位于C:\ Users \ kumar_g \ Downloads \ retrievealerts-oms.ps1:130 char:80
+ ...等"$($ _.value.id)/actions?api-version = 2015-03-20"; | convertfrom-json
+                                                          ~~~~~~~~~~~~~~~
    + CategoryInfo          :未指定:(:) [ConvertFrom-Json],ArgumentException
    + FullyQualifiedErrorId:System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

convertfrom-json : Invalid JSON primitive: .
At C:\Users\kumar_g\Downloads\retrievealerts-oms.ps1:130 char:80
+ ... et "$($_.value.id)/actions?api-version=2015-03-20" | convertfrom-json
+                                                          ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

注意:-相同的脚本,没有任何更改,可以很好地运行于另一个订阅.我也已将api版本从2015-03-20替换为2016-04-01,但仍然没有运气.

Note:- Same script without any changes running fine for another subscription. I have also replaced api version from 2015-03-20 to 2016-04-01 but still no luck.

干杯,古拉夫(Gourav),请记住,如果有帮助,请将其标记为答案.

Cheers, Gourav Please remember to mark the replies as answers if it helped.

推荐答案

失败的json是什么样的?  

What does the failing json look like? 


这篇关于为另一个OMS工作区运行脚本时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆