I have a ping-script, which has the following input parameters
param(
#Arralist zum Aufnehmen der Hosts.
#[parameter(Mandatory = $true, Position = 0)][System.Collections.ArrayList]$Hosts = @(),
[parameter(Mandatory = $true, Position = 0)][System.String]$Hosts,
#Anzahl Wiederholung pro Host, Standard 10 Jahre
[parameter(Mandatory = $false,Position = 1)][double]$Repetition = 315360000,
#solange wird nichts unternommen, standard 0
[parameter(Mandatory = $false,Position = 1)][double]$Pause = 5,
#Speicherort des Logfile, standard C:\Temp
[parameter(Mandatory = $false,Position = 2)][string]$LogPath = 'C:\Temp\Ping Tool'
)
if I invoke the script by using the script name and the needed parameters, like script.ps1 -hosts www.google.ch, www.youtube.com -repetition 2 the script will ping the two hosts 2 times and then stops. That is good so. But the problem occurs, if I have only one host(exam. www.google.ch) to ping. It says that The argument transformation for the parameter "Hosts" can't be processed. The value "www.google.ch" of type "System.String" can't be converted to the type "System.Collections.ArrayList".
What can I do, so that the script works even if I ping only one host? The problem here is that I defined an Arraylist in the parameters that is why it is not letting me enter only one host to ping.
Remove the cast of ArrayList
or System.String
for the $Hosts
parameter, and use foreach
in your code to iterate each item, it will handle it alone...
See example:
function Test-Input {
param(
$hosts
)
Write-Host The Input is: [ $hosts.GetType().FullName ]
foreach ($item in $hosts) {
Write-Host Item: [ $item ]
}
}
See the Results:
PS > Test-Input -hosts www.google.ch
The Input is: [ System.String ]
Item: [ www.google.ch ]
PS > Test-Input -hosts www.google.ch,www.google.com
The Input is: [ System.Object[] ]
Item: [ www.google.ch ]
Item: [ www.google.com ]