# Error Running Script for another OMS workspace

**URL:** <https://forums.powershell.org/t/error-running-script-for-another-oms-workspace/11245>\
**Category:** PowerShell Help\
**Created:** [September 1, 2018, 4:56am UTC](https://forums.powershell.org/t/error-running-script-for-another-oms-workspace/11245 "2018-09-01T04:56:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gouravin](https://avatars.discourse-cdn.com/v4/letter/g/97f17d/32.png) [@gouravin](https://forums.powershell.org/u/gouravin)\
**Post date:** [September 1, 2018, 4:56am UTC](https://forums.powershell.org/t/error-running-script-for-another-oms-workspace/11245/1 "2018-09-01T04:56:25Z")

</div>

Hi Experts,  
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.  
The code is as follows:-

```
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
```

&nbsp;  
The error which i am getting is as below:-

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

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.  
&nbsp;

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [October 13, 2018, 10:11am UTC](https://forums.powershell.org/t/error-running-script-for-another-oms-workspace/11245/2 "2018-10-13T10:11:35Z")

</div>

@Gourav - Did you get the solution for this ? if so please share it with us which will be helpful for others else please update this thread with latest update.

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [October 13, 2018, 6:55pm UTC](https://forums.powershell.org/t/error-running-script-for-another-oms-workspace/11245/3 "2018-10-13T18:55:05Z")

</div>

Since you are getting this…

> Invalid JSON primitive: .

… it directly translates in most cases, to the problem is caused by a format mismatch. Something like, JSON that does not contain opening and closing square brackets [] around arrays.

Now, all that being said, an Azure sub is a Azure sub, and there really should not be any difference in the way the data set / object is being returned to you from on to the other, but apparently there is between you subs.

I’ve not encounter this with my sub, but I only have one, nor have I seen this at any customer site I supported.

So, my suggestion is to step back a bit and look at the raw data coming back before making any convert\* attempts and note any differences there, and try to resolve before further attempts.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/error-running-script-for-another-oms-workspace/11245/4 "2024-05-16T20:34:12Z")

</div>


