Powershell parameters as options set

YaroC 311 Reputation points
2023-02-28T12:41:44.6+00:00

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 $?){...
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,037 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 31,571 Reputation points
    2023-02-28T13:50:40.0166667+00:00

    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"
    } 
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 44,696 Reputation points
    2023-02-28T20:17:43.41+00:00

    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{}
    
    0 comments No comments

  2. YaroC 311 Reputation points
    2023-03-01T14:55:12.9+00:00

    Thank you both. Nice to see how the pros do it :)

    0 comments No comments