0

I am stuck at this: The user needs to input two computer names and i don't know how to transfer them into one input (maybe i'm asking it wrong) but here is the code

elseif ($usersinput -eq 2) 
{
    $pingingtwopcs = Read-Host -Prompt "what are the names of the pc that >you want to ping? (please enter pc names in the next order with comma : >pc1,pc2)"
    foreach ($pcs in $pingingtwopcs)
    {
        Test-Connection -computername $pcs -Count 1
    }
}

Please don't provide the solution, if it is possible please direct me so I'll figure it out by my self.

Ocaso Protal
  • 19,362
  • 8
  • 76
  • 83
WebsGhost
  • 107
  • 1
  • 14
  • `Read-Host` will return a string, not two variables. So you have to [split](https://stackoverflow.com/questions/30617758/splitting-a-string-into-separate-variables) that string – Ocaso Protal Mar 14 '18 at 07:05

2 Answers2

1

Do not use Read-Host, this is bad design and does not allow for automation. Instead let the user propvide the ComputerName(s) either as a string an array.

function Write-ComputerName([System.Array]$ComputerName) {
    foreach($oneComputerName in $ComputerName){
        Write-Output $oneComputerName
    }
}

Then a user can either pass in one or multiple ones:

Write-ComputerName 'Bob'
Write-ComputerName @('Bob','Alice')

If you still need to do custom logic based on the number of ComputerNames, then you can use $ComputerName.Count in your function.

bergmeister
  • 949
  • 2
  • 10
  • 16
  • but then it'll become a function and will behave like a cmdlet, isn't it? – WebsGhost Mar 14 '18 at 08:12
  • Yes. But this was one of the main points of PowerShell to have a common, well defined user interface without re-inventing basic parameter parsing/retrieving logic. – bergmeister Mar 14 '18 at 12:27
  • my friend i'm in power shell only 3 weeks (this is the end of the third) let me do my mistakes and do stuff as i understand :) after i will get used to it, i promise you that things will look much better :) Thanks for your help :) – WebsGhost Mar 14 '18 at 13:07
0

You can do something like this -

$pingingtwopcs = (Read-Host -Prompt "what are the names of the pc that >you want to ping? (please enter pc names in the next order with comma : >pc1,pc2)").split(',') | ForEach-Object {$_.trim()}

The above line will accept two values, separated by a comma, in a single go. As Read-Host accepts the input in a String format, you will have to use the Split() method to separate them and store it in $pingingtwopcs. The Trim() method will remove any extra space which has been entered during input.

Vivek Kumar Singh
  • 3,223
  • 1
  • 14
  • 27
  • so it should look like that : $pingingtwopcs = (Read-Host -Prompt "what are the names of the pc that you want to ping? (please enter pc names in the next order with comma : >pc1,pc2)").split(',') | ForEach-Object {$_.trim()} foreach ($computer in $pingingtwopcs) { Test-Connection -ComputerName $computer -Count 1 soryy that it's written that way from some reason the ">" sign not working :\ It's working ~!!@!@#!#@!!!! so what did the trick? this thing ".split(',')"? – WebsGhost Mar 14 '18 at 08:21
  • The `>` sign is not working? What are you expecting it to do? Anyways it is enclosed within `double quotes` and will only be displayed on screen and nothing else. You have to enter two PC names separated by a `comma` when the prompt asks you to do so! – Vivek Kumar Singh Mar 14 '18 at 08:28