0

I am running a script that will parse a relatively big csv. Depending on which machine it runs on however it will be searching for different things.

I am fairly new to powershell so I'm not sure what the most efficient way to do this would be. Keeping everything in one file would be best, as I would like to be able to have all machines call the same script.

I was thinking possibly a case statement matching $env:computername to the names of the machines I will be running it on?

2 Answers 2

2

IF the logic is always parse a .csv file and the different things are the matching patterns I'ld use a switch statement based on the hostname for setting all thevariables that I'll use later in the logic of the script. If also the logic needs changes based on hostname I'll set them in the switch statement some script blocks to invoke later in the script's logic.

Example:

$a = hostname switch ($a) { "WS001" { $sb = {dir} ; $txt = "Computer name: " + $a } "WS003" { $sb = { get-process } ; $txt = "Computer name: " + $a } default { $sb = $null ; $txt = "This Computer can't run this script"} } if ($sb) { &$sb } $txt 
Sign up to request clarification or add additional context in comments.

Comments

0

One small addition to the answer by @Christian.

At the end of the script block for each host name, you might want to consider adding a break statement. That way you avoid testing $a against all possible hosts - once you find a host and do the host specific stuff, the Break statement jumps you out of the Switch's script block.

Thus it might look like this:

$a = hostname switch ($a) { "WS001" { $sb = {dir} ; $txt = "Computer name: " + $a ; break } "WS003" { $sb = { get-process } ; $txt = "Computer name: " + $a; break } default { $sb = $null ; $txt = "This Computer can't run this script"} } ... 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.