azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Cannot obtain Values from Get-AzureRmOperationalInsightsSearchResults

Open tombromby opened this issue 9 years ago • 5 comments

Cmdlet(s)

Get-AzureRmOperationalInsightsSearchResults
Get-AzureRmOperationalInsightsSavedSearch

PowerShell Version

Name                           Value
----                           -----
PSVersion                      5.1.14393.1358
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.1358
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Module Version

ModuleType Version    Name
---------- -------    ----
Script     3.1.0      AzureRM.OperationalInsights

OS Version

BuildVersion 10.0.14393.1358

Description

Cannot return JSON from .Value

I run the following command to pull data from my OMS workspace: ((Get-AzureRmOperationalInsightsSavedSearchResults -ResourceGroupName $ResourceGroupName -WorkspaceName $WorkSpaceName -SavedSearchId "starc|starq").Value | ConvertFrom-Json)

However the ConvertFrom-JSON step throws an error:

The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

.Value does have data in it, as per this sample, but I cannot access the data in Value:
Id       : subscriptions/xxx/resourceGroups/rg/providers/Microsoft.OperationalInsights/workspaces/myomsworkspace/search/878c655e-e4f5
           -4937-a342-5e04ab986f18|10.1.0.12|2017-07-06T04-52-07Z
Metadata : Microsoft.Azure.Commands.OperationalInsights.Models.PSSearchMetadata
Error    :
Value    : {"Type": "Alert" "AggregatedValue": 86.0, "Type": "Usage" "AggregatedValue": 14.0}

Debug Output

convertfrom-json : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.
At line:1 char:17
+ $result.Value | convertfrom-json
+                 ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: ({
  "Type": "A...Value": 86.0
}:JObject) [ConvertFrom-Json], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

convertfrom-json : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.
At line:1 char:17
+ $result.Value | convertfrom-json
+                 ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: ({
  "Type": "U...Value": 14.0
}:JObject) [ConvertFrom-Json], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

### Script/Steps for Reproduction
$ResourceGroupName = 'oms-rg'
$WorkSpaceName = 'oms-workspace'

$result = Get-AzureRmOperationalInsightsSavedSearchResults `
-ResourceGroupName $ResourceGroupName `
-WorkspaceName $WorkSpaceName `
-SavedSearchId "foo"
$result.value | ConvertFrom-Json

tombromby avatar Jul 06 '17 05:07 tombromby

I've also replicated this issue in Azure Automation

tombromby avatar Jul 06 '17 22:07 tombromby

My workaround is to pass the values to a hashtable but this is not how the modules are documented - https://blogs.technet.microsoft.com/privatecloud/2016/04/05/using-the-oms-search-api-with-native-powershell-cmdlets/ and the solution is not elegant:

$dynamicQuery = "* | measure count() by Type"
$now = Get-Date
$StartDateAndTime = $now.AddHours(-24).ToString("yyyy-MM-ddTHH:mm:ss")

$EndDateAndTime = $now.ToString("yyyy-MM-ddTHH:mm:ss")

$result = Get-AzureRmOperationalInsightsSearchResults `
-ResourceGroupName $ResourceGroupName `
-WorkspaceName $WorkSpaceName `
-Query $dynamicQuery `
-Start $StartDateAndTime `
-End $EndDateAndTime 

 $queryResults = new-object System.Collections.Hashtable

Foreach ($item in $result.value)
{
 $obj1 = $item["Type"].ToString()
   $obj2= $item["AggregatedValue"].ToString()
  [void]$queryResults.Add($obj1,$obj2)
}

Write-Output $queryResults

tombromby avatar Jul 07 '17 06:07 tombromby

@xizhamsft Hey Xiaofang, would you mind taking a look at this issue?

cormacpayne avatar Jul 07 '17 18:07 cormacpayne

I have a similar issue. Looks like output is being automatically converted in a Newtonsoft.JSON Jobject. This issue is blocking me from putting a script into production with latest cmdlets. I rolled back to AzureRM module version 4.0.2.

montereyharris avatar Jul 12 '17 21:07 montereyharris

fixed and wait for next release.

haitch avatar Aug 01 '17 20:08 haitch