Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.21k stars 3.81k forks source link

Get-AzAutomationScheduledRunbook missing parameters when array #11504

Open jeffwmiles opened 4 years ago

jeffwmiles commented 4 years ago

Description

When using Get-AzAutomationScheduledRunbook, for a JobSchedule that contains an array in a parameter, the output of the Parameter property is missing for that specific parameter.

Steps to reproduce


# Define an object that will be used for parameters on the JobSchedule
# This represents an Azure NSG rule that will be created across each subnet defined in the 'nsggroup' property
$object = [pscustomobject]@{  name = "automationjobruntimedata"; rulename = "any_automationjobruntimedata_443"; ports = @(443,80); endpointfqdn = "eus2-jobruntimedata-prod-su1.azure-automation.net"; nsggroup = @("db-nsg","prod-extweb-nsg","prod-nsg","test-nsg","mgmt-nsg") }

# Set up Automation Account variables
$resourceGroupName = "automation-rg"
$automationAccountName = "automationAccount"
$subscriptionid = "REDACTED"
$runbookname = "nsg-interface-update"

$scheduleparams = @{
    nsggroup = $object.nsggroup
    rulename = $object.rulename
    endpointfqdn = $object.endpointfqdn
    ports = $object.ports
    subscriptionid = $subscriptionid
}
$runbookparams = @{
    Name = $runbookname
    ScheduleName = "6Hours-$($object.name)"
    AutomationAccountName = $automationAccountName
    ResourceGroupName = $resourceGroupName
    Parameters = $scheduleparams
}
$sched = Get-AzAutomationSchedule -Name "6Hours-$($object.name)" -AutomationAccountName $automationAccountName -ResourceGroupName $resourceGroupName -ErrorAction Ignore 

$StartTime = (Get-Date).AddMinutes(10) # Start Time should do the first run in 10 minutes.

New-AzAutomationSchedule -Name "6Hours-$($object.name)" -StartTime $StartTime -HourInterval 6 -AutomationAccountName $automationAccountName -ResourceGroupName $resourceGroupName

Register-AzAutomationScheduledRunbook @runbookparams

# Get the job that we just created
$scheduledjob = Get-AzAutomationScheduledRunbook -Name $runbookname -ScheduleName "6Hours-$($object.name)" -AutomationAccountName $automationAccountName -ResourceGroupName $resourceGroupName
# Find the Parameters for the job
$setparams = (Get-AzAutomationScheduledRunbook -jobscheduleid $scheduledjob.jobscheduleid -AutomationAccountName $automationAccountName -ResourceGroupName $resourceGroupName).Parameters

Result is that in the Portal, the parameter is created as an array properly: portal

However, when looking at $setparams, the values for ports and nsggroup are empty: setparams

See Debug output below, where the actual REST API call returns JSON containing the parameters, but with additional /r/n line breaks added.

It appears that the conversion of the REST JSON body into a PowerShell Hashtable in Powershell 7 isn't functioning, where it does in PowerShell 5.1 or 6.

Environment data

This only appears to occur on PowerShell 7; not 6 or 5.1

Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT

Module versions

ModuleType Version    PreRelease Name
---------- -------    ---------- ----          
Script     3.7.0                 Az
Script     1.3.6                 Az.Automation

Debug output

DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
GET

Absolute Uri:
https://management.azure.com/subscriptions/4ceb974a-REDACTED/resourceGroups/rgname/providers/Mi
crosoft.Automation/automationAccounts/AutomationAccount/jobSchedules/23e424e0-fe66-450f-b7ff-589aa8fb82be?api-version=2015-1
0-31

Headers:
x-ms-client-request-id        : cdc63e1e-8cfd-40aa-95e4-132067eaa080
Accept-Language               : en-US

Body:

DEBUG: ============================ HTTP RESPONSE ============================

Status Code:
OK

Headers:
Cache-Control                 : no-cache
Pragma                        : no-cache
x-ms-request-id               : b440e97a-9f13-4dc9-
Strict-Transport-Security     : max-age=31536000; includeSubDomains
Server                        : Microsoft-IIS/10.0
X-AspNet-Version              : 4.0.30319
X-Powered-By                  : ASP.NET
x-ms-ratelimit-remaining-subscription-reads: 11999
x-ms-correlation-request-id   : e569aa11-fe6a-4d5e-
x-ms-routing-request-id       : NORTHCENTRALUS:20200406T164340Z:e569aa11-fe6a-4d5e-bdd7-75bd0d13baa9
X-Content-Type-Options        : nosniff
Date                          : Mon, 06 Apr 2020 16:43:39 GMT

Body:
{
  "id": "/subscriptions/4ceb974a-REDACTED/resourceGroups/rgname/providers/Microsoft.Automation/
automationAccounts/AutomationAccount/jobSchedules/23e424e0-fe66-450f-b7ff-589aa8fb82be",
  "type": "Microsoft.Automation/AutomationAccounts/JobSchedules",
  "properties": {
    "jobScheduleId": "23e424e0-fe66-450f-b7ff-589aa8fb82be",
    "runbook": {
      "name": "nsg-interface-update"
    },
    "schedule": {
      "name": "6Hours-automationjobruntimedata"
    },
    "runOn": null,
    "parameters": {
      "SubscriptionId": "\"047f8a8b-REDACTED\"",
      "RuleName": "\"any_automationjobruntimedata_443\"",
      "endpointfqdn": "\"eus2-jobruntimedata-prod-su1.azure-automation.net\"",
      "nsggroup": "[\r\n  \"db-nsg\",\r\n  \"prod-extweb-nsg\",\r\n  \"prod-nsg\",\r\n  \"te
st-nsg\",\r\n  \"mgmt-nsg\"\r\n]",
      "ports": "[\r\n  443,\r\n  80\r\n]"
    }
  }
}

DEBUG: AzureQoSEvent: CommandName - Get-AzAutomationScheduledRunbook; IsSuccess - True; Duration - 00:00:01.0278770; Exc
eption - ;
DEBUG: Finish sending metric.
DEBUG: 10:44:20 AM - GetAzureAutomationScheduledRunbook end processing.
DEBUG: 10:44:20 AM - GetAzureAutomationScheduledRunbook end processing.

Error output

No errors encountered

jeffwmiles commented 4 years ago

This is the Debug output from PowerShell 5.1

PowerShell Version

Name                           Value
----                           -----
PSVersion                      5.1.18362.628
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.628
CLRVersion                     4.0.30319.42000

Azure Module

ModuleType Version    Name
---------- -------    ---- 
Script     1.7.4      Az.Accounts
Script     1.3.6      Az.Automation

Debug Output

DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
GET

Absolute Uri:
https://management.azure.com/subscriptions/4ceb974a-REDACTED/resourceGroups/rgname/providers/M
icrosoft.Automation/automationAccounts/automationAccount/jobSchedules/23e424e0-fe66-450f-b7ff-589aa8fb82be?api-version=2015
-10-31

Headers:
x-ms-client-request-id        : 3878d38e-19d1-4f34-a14c-
accept-language               : en-US

Body:

DEBUG: ============================ HTTP RESPONSE ============================

Status Code:
OK

Headers:
Pragma                        : no-cache
x-ms-request-id               : 9e4c3729-569f-493f-
Strict-Transport-Security     : max-age=31536000; includeSubDomains
x-ms-ratelimit-remaining-subscription-reads: 11995
x-ms-correlation-request-id   : f3f455fe-dfcf-48c3-
x-ms-routing-request-id       : NORTHCENTRALUS:20200406T170738Z:f3f455fe-dfcf-48c3-96de-6f9cd27043ab
X-Content-Type-Options        : nosniff
Cache-Control                 : no-cache
Date                          : Mon, 06 Apr 2020 17:07:37 GMT
Server                        : Microsoft-IIS/10.0
X-AspNet-Version              : 4.0.30319
X-Powered-By                  : ASP.NET

Body:
{
  "id":
"/subscriptions/4ceb974a-REDACTED/resourceGroups/rgname/providers/Microsoft.Automation/automat
ionAccounts/automationAccount/jobSchedules/23e424e0-fe66-450f-b7ff-589aa8fb82be",
  "type": "Microsoft.Automation/AutomationAccounts/JobSchedules",
  "properties": {
    "jobScheduleId": "23e424e0-fe66-450f-b7ff-589aa8fb82be",
    "runbook": {
      "name": "hfx-nsg-interface-update"
    },
    "schedule": {
      "name": "6Hours-automationjobruntimedata"
    },
    "runOn": null,
    "parameters": {
      "SubscriptionId": "\"047f8a8b-REDACTED\"",
      "RuleName": "\"any_automationjobruntimedata_443\"",
      "endpointfqdn": "\"eus2-jobruntimedata-prod-su1.azure-automation.net\"",
      "nsggroup": "[\r\n  \"db-nsg\",\r\n  \"prod-extweb-nsg\",\r\n  \"prod-nsg\",\r\n
\"test-nsg\",\r\n  \"mgmt-nsg\"\r\n]",
      "ports": "[\r\n  443,\r\n  80\r\n]"
    }
  }
}

DEBUG: AzureQoSEvent: CommandName - Get-AzAutomationScheduledRunbook; IsSuccess - True; Duration - 00:00:01.0605458;
Exception - ;
DEBUG: Finish sending metric.
DEBUG: 11:08:18 AM - GetAzureAutomationScheduledRunbook end processing.
DEBUG: 11:08:18 AM - GetAzureAutomationScheduledRunbook end processing.

This time the $setparams output contains the parameters: setparams2

jeffwmiles commented 4 years ago

I've been trying to dig a little deeper on this with my limited knowledge of debugging PowerShell. If I hit the API directly with the URI given in the debug output, I get the exact same response between PowerShell 5.1 and 7.

#https://gallery.technet.microsoft.com/scriptcenter/Easily-obtain-AccessToken-3ba6e593
. .\Get-AzCachedAccessToken.ps1
$BearerToken = ('Bearer {0}' -f (Get-AzCachedAccessToken))
$RequestHeader = @{
        "Content-Type"  = "application/json";
        "Authorization" = "$BearerToken"
    }

$URI = "https://management.azure.com/subscriptions/4ceb974a-REDACTED/resourceGroups/rgname/providers/Microsoft.Automation/automationAccounts/automationaccount/jobSchedules/1c5af1da-1c7b-4583-9879-a0b5676fd55d?api-version=2015-10-31"
$Response = Invoke-RestMethod -Uri $URI -Method GET -header $RequestHeader

restresponse

I searched through the PS7 release notes for references to JSON handling, and came across this item: #3424. Perhaps this is related - I tried to round-trip the conversion of my $response, and got different results.

PowerShell 7

ps7_roundtrip

PowerShell 5.1

ps5_roundtrip

I can't understand the link in the Az.Automation module code between the Get-AzAutomationAccountScheduledRunbook and JSON handling to return the hashtable yet, so I'm stuck going deeper at this point.

dingmeng-xue commented 4 years ago

@jeffwmiles, thanks for reporting. We will look into it

wyunchi-ms commented 4 years ago

Hi @jeffwmiles , we have confirmed that this is caused by the issue of converting the strings with escape chars into json object. We will fix it in the future.

jeffwmiles commented 3 years ago

This issue doesn't appear to be resolved by the fix on #12283 - when I repeat the test (with Az.Automation module 1.4.0 on PowerShell Core 7.1 now), I do see the debug output is modified: powershellerror_1

However the output is still malformed (although not actually null): powershellerror_2

Similar to the related issue #12702 , the type is returned as a Json.Linq.Jvalue: powershellerror_3

And converting this back to JSON returns and empty array: powershellerror_4