Solved

paramater function powershell

Posted on 2014-10-17
1
102 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 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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

729 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