[Solved]asking from user to enter multiple parameters

This topic contains 10 replies, has 4 voices, and was last updated by  Lev Leiderman 2 months, 2 weeks ago.

  • Author
    Posts
  • #95823

    Lev Leiderman
    Participant

    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 !!

  • #95826

    Don Jones
    Keymaster

    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.

    • #95867

      Lev Leiderman
      Participant

      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 🙂

    • #95877

      Fredrik Kacsmarck
      Participant

      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.

  • #95828

    Don Jones
    Keymaster

    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?

  • #95829

    Lev Leiderman
    Participant

    Hi Don
    thanks alot,
    i'll try that 🙂

    • #95873

      Aapeli Hietikko
      Participant

      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
      
    • #95874

      Aapeli Hietikko
      Participant

      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
      
    • #95880

      Aapeli Hietikko
      Participant

      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
      
    • #95891

      Lev Leiderman
      Participant

      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 🙂

  • #95909

    Lev Leiderman
    Participant

    ok,
    admins, please close my post 🙂

    thanks alot

You must be logged in to reply to this topic.