Author Posts

March 13, 2018 at 7:19 pm

Hello guys,
after a three weeks of reading and watching videos about Power Shell it's time for me to start and make my first project

i'm going to ask from a user to make a selection of want he want to do, and then do something

i'm started from the basic thing which is pingin few computers at ones from users input

$usersinput = read-host "how much pcs do you want to ping?"

$pingonepc= 1

$pingtwopcs= 2

$pingthreepcs = 3

if ($usersinput -eq 1) {
   
 $pingingonepc= Read-Host "what is the name of the pc that you want to ping?"

    Test-Connection $pingingonepc -Count 1
    }

if ($usersinput -eq 2) {

    $pingingsecondpc = Read-Host "please input two computer names"

    Test-Connection #don't know how to get input here :\

the problem is that i don't understand how to get multiple inputs for the second "if" statemant and use it

i've tried something like that to:

$usersinput = read-host "how much pcs do you want to ping?"

$pingonepc= 1

$pingtwopcs= 2

$pingthreepcs = 3

if ($usersinput -eq 1) {

    $pingingonepc= Read-Host "what is the name of the pc that you want to ping?"

    Test-Connection $pingingonepc -Count 1
    
} elseif ($usersinput -eq 2) {

       $pingingtwopcs = Read-Host "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

} 
            
           

}


PS: please don't give me the solution, only direct me if it possible
thanks a lot !!

March 13, 2018 at 7:25 pm

If Read-Host is given an input like "one, two" then that will come in as a single string with a comma in the middle. Versus this:

function Test-Me {
 [CmdletBinding()]
 Param(
  [Parameter(Mandatory=$True)]
  [string[]]$Param
 )
 ForEach ($thing in $param) {
  Write-Verbose "I'm on thing $thing"
 }
}
Test-Me -Verbose

Try running that. PowerShell's convention on a prompt like that is to hit Enter on a blank prompt to indicate you're done entering values. $Param winds up being a proper array you can enumerate.

March 13, 2018 at 7:28 pm

You wouldn't normally ask how many PCs to ping. You'd just collect all the names, figure out how many names there are, and ping them. Why should the user have to count, when counting is something computers do so well?

March 13, 2018 at 7:38 pm

Hi Don
thanks alot,
i'll try that 🙂

March 14, 2018 at 6:34 am

Hi Don,

i've tried your code and the thing is that you've done a function and i'm trying to figure out how to make my "else if" condition to work properly 🙂

actually it half working, when i input one string, it's working properly, but when i input 2 strings it's not working at all, so i've though that i use "param" but then again i've stuck in the same place, so i've changed the code to use "foreach" statement:

} 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

}

the question is, if i understand things correctly, for this thing to work i need to set two variables but i don't know what they will be since it's decided by the user.. and so i've decided to use foreach, but once again, i'm probebly do something wrong..

Thanks alot for your help 🙂

March 14, 2018 at 8:49 am

how about something like this

function yarr-pong {

$menu = @"
*****************************
*                           *
* 1) Ping Computer1 8.8.8.8 *
* 2) Ping Computer1 9.9.9.9 *
* 3) Ping Computer1 5.5.5.5 *
* e) Exit                   *
*****************************
"@
cls
Write-output $menu
$option = read-host "yarr option"

switch ($option) {
    1 {ping 8.8.8.8}
    2 {ping 9.9.9.9}
    3 {ping 5.5.5.5}
    e {break}
    default {write-output "wrong option" ; yarr-pong}
    }

}

yarr-pong

March 14, 2018 at 10:06 am

I missed the point a bit, so here is another version

function yarr-pong {

$menu = @"
*************************************
*                                   *
* 1) Ping 8.8.8.8                   *
* 2) Ping 8.8.8.8, 9.9.9.9          *
* 3) Ping 8.8.8.8, 9.9.9.9, 4.4.4.4 *
*                                   *
* e) Exit                           *
*                                   *
*************************************
"@
cls
Write-output $menu
$option = read-host "yarr option"

switch ($option) {
    1 {$computername = '8.8.8.8'}
    2 {$computername = '8.8.8.8','9.9.9.9'}
    3 {$computername = '8.8.8.8','9.9.9.9','4.4.4.4'}
    e {break}
    default {write-output "wrong option" ; yarr-pong}
    }
foreach ($c in $computername) {

    [PsCustomObject]@{
        'ComputerName'  = $c
        'PingSucceeded' = Test-Connection $c -count 1 -Quiet
        
        }
    }

}

yarr-pong

March 14, 2018 at 10:11 am

As Don says, the variable will be a single string.
So $pingingtwopcs will contain the string "pc1,pc2" (replacing pc1 and pc2 with the values that was entered).

Since you don't want the solution itself, look at the string method called Split.
With Split you can seperate the value pc1 from pc2 by using the the comma as the split operator.
You would use the method on $pingingtwopcs and then the foreach loop will work.

March 14, 2018 at 10:31 am

Apparently I have too much spare time on my lunch breaks as I got stuck with the thing xD

function yarr-pong {
$output = $null
$exit   = $true
$menu   = @"
*************************************
*                                   *
* 1) Ping 8.8.8.8                   *
* 2) Ping 8.8.8.8, 9.9.9.9          *
* 3) Ping 8.8.8.8, 9.9.9.9, 4.4.4.4 *
*                                   *
* e) Exit                           *
*                                   *
*************************************
"@

do {
    cls
    
    $menu
    $output | Format-Table
    $option = read-host "yarr option"
    
    switch ($option) {
        1 {$computername = '8.8.8.8'}
        2 {$computername = '8.8.8.8','9.9.9.9'}
        3 {$computername = '8.8.8.8','9.9.9.9','4.4.4.4'}
        e {$exit = $false}
        default {$computername = "wrong option"}
        }
    $output = foreach ($c in $computername) {
    
        [PsCustomObject]@{

            'ComputerName'  = $c
            'PingSucceeded' = Test-Connection $c -count 1 -Quiet
            
            }
        }

    } while ($exit)

clv output,menu,exit

}
yarr-pong

March 14, 2018 at 11:20 am

Hi,
Aapeli

because i'm new to all this coding thing, my mind is not working as yours or other people who wrote code before (i always made my way with batch files 🙂 )

so i'm only learning to understand this stuff and do my training through my first script (which is going to be with
some menus and choises (i'm tring to put as much things as i can in one script 🙂 )

} 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)").split(',') | ForEach-Object {$_.trim()}
       
           foreach ($computer in $pingingtwopcs) {

           Test-Connection -ComputerName $computer -Count 1 |Format-Table -AutoSize

} 
            
           
}

and it works 🙂

March 14, 2018 at 1:08 pm

ok,
admins, please close my post 🙂

thanks alot