Solved

paramater function powershell

Posted on 2014-10-17
1
97 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
1 Comment
 
LVL 83

Accepted Solution

by:
oBdA earned 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
A brief introduction to what I consider to be the best editor for PowerShell.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now