PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,329 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How would I set parameters for a PowerShell script as options, assuming it's computer names, which are to be provided as either a list in a text file or a csv or just typed in as script arguments? Also, how to make sure that one of this options is actually used when running the script. How would the script know which is it? I know it's very bad but something to start with below.
Param(
[Parameter(Mandatory= $false,
HelpMessage = 'Computer name']
[String[]]$computername,
[Parameter(Mandatory = $false,
HelpMessage = 'Computer names from Csv file. Requires "computername" column']
[Object]$csv,
[Parameter(Mandatory = $false,
HelpMessage = 'Computer names from Txt file']
[Object]$txt
)
$hosts = Import-Csv $CSV.computername
# or
$hosts = Get-Content $txt
foreach($host in $?){...
Here's one way.
<#
.SYNOPSIS
This script does something with computers.
.DESCRIPTION
If you specify a CSV, be sure to have a column named computername.
This script accepts these parameters.
-computername A single computer name.
-csv The fully qualified path to a csv file that has a computername column.
-txt The fully qualified path to a txt file that contains names of computers.
.NOTES
.LINK
http://www.google.com
Author: MotoX80 on Microsoft Q&A Forums
#>
Param( #https://powershell.org/2013/05/a-helpful-message-about-helpmessage/
[String]$computername,
[String]$csv,
[String]$txt
)
if ($computername) {
$hosts = $computername
} elseif ($csv) {
if (Test-Path $csv) {
$hosts = (Import-Csv $CSV).computername
} else {
"CSV file not found."
return
}
} elseif ($txt) {
if (Test-Path $txt) {
$hosts = Get-Content $txt
} else {
"TXT file not found."
return
}
} else {
"Error. You must specify one parameter."
return
}
foreach($name in $hosts) {
"Processing $name"
}
In addition to the answer given by @MotoX80 , here's another way (using parameter sets) to handle selecting the appropriate parameter name:
[CmdletBinding()]
Param(
[Parameter(Mandatory= $false, ParameterSetName='Name',
HelpMessage = 'Computer name')]
[String[]]$computername,
[Parameter(Mandatory = $false, ParameterSetName='CSV',
HelpMessage = 'Computer names from Csv file. Requires "computername" column')]
[string]$csv,
[Parameter(Mandatory = $false,ParameterSetName='TXT',
HelpMessage = 'Computer names from Txt file')]
[String]$txt
)
begin {
switch ($PsCmdlet.ParameterSetName){
"Name" { $computers = $computername }
"CSV" { if (Test-Path $csv) {
$computers = (Import-Csv $CSV).computername
} else {
"CSV '$csv'file not found."
return
}
}
"TXT" { if (Test-Path $txt) {
$computers = Get-Content $txt
} else {
"TXT file not found."
return
}
}
}
}
process {
ForEach ($computer in $computers){
# do something amazing here!
$computer
}
}
end{}
Thank you both. Nice to see how the pros do it :)