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
}
[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[]]$serversas a parameter