0

I have a powershell runbook in Azure Automation with the following code:

param
(        
        [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] 
        [String] $ADMINUSERS,
)


#parse User data
function Get-GroupUser 
{
    [CmdletBinding()]
    Param
    (
        [Parameter()]
        [string]$inputUsers
    )
        $userlist = $Local:inputUsers.split(',')
        if ($Local:inputUsers -notlike "*@*") {
            $userlist.replace(' ','')
        } 
        else {
            $returnArray = @()    
            foreach ($user in $($inputUsers | convertFrom-Json)) {
                $returnArray += $user.split('@')[0]
            }
            $returnArray
}

Get-GroupUser -inputUsers $ADMINUSERS

My first input for $ADMINUSERS look like this:

["johndoe@abc.biz","janedoe@abc.biz"]

When the runbook run, it fails immediately with the following error:

Cannot process argument transformation on parameter 'ADMINUSERS'. Cannot convert value to type System.String. (Cannot convert value to type System.String. (Cannot convert value to type System.String.))

My second input for $ADMINUSERS look like this:

johndoe,janedoe

And this completes successfully.

Any ideas how I can fix the first input error on my runbook in Azure Automation?

NOTE: When I run both input locally from a server, it completes successfully.

kilo2020
  • 11
  • 1
  • 4
  • You declared the parameters as string and the function splits that on the comma to get an array. This means you nee to send a **string** and right now you are sending arrays.. `"johndoe@abc.biz","janedoe@abc.biz"` --> `"johndoe@abc.biz,janedoe@abc.biz"` and `johndoe,janedoe` --> `"johndoe,janedoe"` – Theo Jul 30 '20 at 19:51
  • What is the best way of converting this specific array into a string? – kilo2020 Jul 30 '20 at 20:06
  • I just showed you in my comment didn't I ? That's hardcoded of course. If you have a string array and want to make a comma separated string out of that use the `-join` operator like `$string = "johndoe@abc.biz","janedoe@abc.biz" -join ','` – Theo Jul 30 '20 at 20:07
  • One thing I should mention is that the input is derived from the storage account queue, where it looks like this: "AdminUser": " [\"johndoe@abc.biz\",\"janedoe@abc.biz"\]". In the runbook input section, it shows this: ADMINUSERS ["johndoe@abc.biz","janedoe@abc.biz"] – kilo2020 Jul 30 '20 at 20:50

1 Answers1

0

The param expects either a string or an array, but not both forms. If you want to accept an array of strings, the param syntax would be:

[String[]] $ADMINUSERS

Note the extra bracket pair after String, indicating an array.

Given the array, you can convert into a comma-separated string using join:

Get-GroupUser -inputUsers ($ADMINUSERS -join ",")

I'm not whether you could accept both of the input forms you mention, and should choose one or the other.

Noah Stahl
  • 6,905
  • 5
  • 25
  • 36
  • It runs fine locally when I test it out but when I add the code to the Azure Automation powershell runbook and run, it fails with the same error message. – kilo2020 Jul 30 '20 at 20:41
  • Ah, the problem is the runbook input doesn't match the param type `String`. See updated answer, I suggest using `String[]` instead or passing the second comma-separated raw value without array brackets. – Noah Stahl Jul 30 '20 at 21:46
  • I set the string[] in the param block but now I got a different but similar error: ```Get-GroupUser : Cannot process argument transformation on parameter 'inputUsers'. Cannot convert value to type System.String. At line:42 char:27 + Get-GroupUser -inputUsers $ADMINUSERS + ~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Get-GroupUser], ParameterBindingArgumentTransformationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-GroupUser``` – kilo2020 Jul 31 '20 at 01:44
  • That makes it seem like ADMINUSERS is now accepted in array form and then needs to be passed as a string to Get-GroupUser, which is where you'd use the `join` – Noah Stahl Jul 31 '20 at 01:51
  • You can use Write-Output and Write-Verbose to log things during testing, which helps see what things are at certain points: https://learn.microsoft.com/en-us/azure/automation/automation-runbook-output-and-messages – Noah Stahl Jul 31 '20 at 01:51
  • Definitely a huge improvement, but I noticing this error on the Azure automation runbook even thought it completes successfully: Invalid JSON primitive: johndoe. – kilo2020 Jul 31 '20 at 15:49