?
Solved

paramater function powershell

Posted on 2014-10-17
1
Medium Priority
?
107 Views
Last Modified: 2014-10-20
Hi Guys,

I have this on the top of my script:

param(
  [string]$users,
  [string]$copyuser

)

i can then use the command

.\Create_Profile_Script2.ps1 -users test -copyuser test2

problems is i want to enter more than one users in -users, how do i do this?

thank you in advance :)
0
Comment
Question by:Kay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 40386414
You define the input variable as array of strings and do a ForEach loop:
param(
	[string[]]$users,
	[string]$copyuser
)
ForEach ($User In $Users) {
	"Processing user '$User' ..." | Write-Host
}

Open in new window

Call it like this:
.\test.ps1 -Users "user1", "user2", "user3" -copyuser userC

Open in new window

Or from a file using Get-Content:
.\test.ps1 -users (gc .\test.txt) -copyuser userC

Open in new window

Or enhance it, supporting the pipeline as well:
Param(
	[Parameter(Mandatory=$True, ValueFromPipeline=$True)]
	[string[]]$users,
	[Parameter(Mandatory=$True)]
	[string]$copyuser
)
Begin {
	"'Begin' runs once per call." | Write-Host
}
Process {
	"'Process' runs once per pipeline element." | Write-Host
	ForEach ($User In $Users) {
		"Processing user '$User' ..." | Write-Host
	}
}
End {
	"'End' runs once when the pipeline is empty." | Write-Host
}

Open in new window

Now you can do this as well:
"user1", "user2", "user3" | .\test.ps1 -copyuser userC

Open in new window

Or from a file:
gc .\test.txt | .\test.ps1 -copyuser userC

Open in new window

0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question