-1

I'm having trouble getting PowerShell to accept a single string param input on the command line OR accept a file as input containing a list of servers.

I've tried working this example but I can't get things to work.

I also tried working with param([switch] $FileList) but the IF statement doesn't reach the else block.

What I hope to put together is a script that accepts a single server name passed in on the command line or accepts input from a text file. I do appreciate any pointers!

-edit, using the below running the script with/without a param returns Run on single server and it is the same if (($FileList -eq $false)) Using Keith Hill's example, again no matter what I try passing the script the output is always same (the IF block never reaches the ELSE block)

-Edit2, the second code example works when passing a single server name to the script, the problem for me is trying to get the [Switch] parameter to accept a filename and pass it to the code block with the foreach loop. It errors with the following Get-EventLog : Invalid value '.\fake.txt' for parameter 'machineName'. at the ELSE line.

param([switch] $FileList)
if (($FileList -eq $true)) { "No file list input" }
Else { "Run on single server"}

2nd code example

param(
[switch]$FileList,
[string]$server)

if ($FileList -eq $true) {
$list = GC $FileList
foreach ($server in $list){Get-EventLog -ComputerName $server -LogName system | Where-Object {$_.EventID -eq '6005'} | Select TimeGenerated,Message | Select -first 1}#End foreach
}

Else{
Get-EventLog -ComputerName $server -LogName system | Where-Object {$_.EventID -eq '6005'} | Select TimeGenerated,Message | Select -first 1
}
3
  • which problem do you get using @Keith Hill example ? Commented Apr 1, 2015 at 3:31
  • Edited question to provide more detail. I wasn't able to focus on this until now, apologies for posting an incomplete question. Commented Apr 1, 2015 at 5:24
  • A [switch] parameter cannot take a value - it's more similar to a boolean. If it's specified in the arguments it becomes $true, otherwise its $false. If you want an array of servers you need to use [string[]]$servers as a parameter Commented Apr 1, 2015 at 8:56

1 Answer 1

1

If you're set on using the switch parameter this won't help but if you just want a single parameter that can be a file or server name it will.

param (
   [parameter(Mandatory = $true)] [string]$FileList
)

if (Test-Path $FileList) {
  "File found, do file related commands."
}
Else { "Single server actions." }
Sign up to request clarification or add additional context in comments.

1 Comment

Ok this looks great, a simple way to accomplish my goal. Looking at the [switch] option, that would only increase the scripts complexity since it would require the user to enter -FileList when the script is run. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.