Solved

paramater function powershell

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

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

820 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