Solved

paramater function powershell

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

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Set OWA language and time zone in Exchange for individuals, all users or per database.
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 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

8 Experts available now in Live!

Get 1:1 Help Now