Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

How to NOT Have Redundancy

$
0
0

If I want to create an advanced function where a $ComputerName parameter can have either the name of a computer obviously, or where it can have another value like “ComputerList” that would indicate using a text file that would open up so the user can input multiple computer names, how would I not create redundant code for each value?

For example,

Function TestFunction {
[CmdletBinding()]
param (
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string[]]$ComputerName
)

If ($ComputerName -eq “ComputerList)
{
$Computers = Get-content “$env:Temp\computerlist.txt”

Foreach ($Computer in $Computers)
{
#Do stuff here
}

Else
{
#do stuff here

#Will be nearly identical to the code in the “if” block above except without a get-content
}

Is there a way to just have only one block process the two different values so as not to reate redundancy and not give me carpal tunnel from all the typing?

Thanks and as always, I appreciate your help


Viewing all articles
Browse latest Browse all 13067

Trending Articles