Jump to content
Drew

Passing a string array into PowerShell

Recommended Posts

Hi PS/Automate script guru's. I have an input parameter prompting a tech to enter one or more UPN's (email addresses), each separated by a comma. Ideally I'd like the PS Script to have a param defined that's of type string array.

To date, I've not been successful in getting PS to treat what I'm passing in as an array. Instead, it thinks it's just one item within the array. Does anyone have any experience with this?

Automate Script:

Function: Execute Script
Parameter: upns
Script Type: PowerShell
Script to Execute: (see PS Script below)
Script Parameters: -upns @upns@
Variable: ps_results

PS Script:

[CmdletBinding()]
param(

[Parameter(Mandatory=$true, Position=1)]
[string[]] $upns

)

Write-Host $upns.Count

foreach($upn in $upns) {
    Write-Host $upn
}


Cheers!

Share this post


Link to post
Share on other sites

For the time being, I put a bit of a hack in to get this working. For anyone interested, change the parameter to a simple string and then parse it at the beginning of your script. In my case, I wanted to verify valid UPN's were being provided. Here's the example:

try {
    [MailAddress[]]$Permissions = $AssignPermissions.split(",")
}
catch {
    Write-Host "Failure: Invalid UPN(s) detected within the AssignPermissions parameter." $AssignPermissions
    break
}

Not the most elegant approach, but it works.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...