[Solved]"switch" statment - somthing missing

Welcome Forums General PowerShell Q&A [Solved]"switch" statment - somthing missing

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
8 months ago.

  • Author
    Posts
  • #96150

    Participant
    Points: 0
    Rank: Member

    Hi guys,
    So, I'm trying to train my Powershell skils, I'm new to scripting and I've tried to do a simple script of managing VM's (things like, show all the VM's stop, suspend etc)

    I've already tried few variations on the code, and i'm probably missing something, the thing is, that i dont understand what

    here is the code (it is very simple code)

    do
    {
        $input = Read-Host " what do you want to do? 
                            `n1)  list all the Vms
                            `n2) do something to the Vms?
                            `n
                            `n * what is your choice?"
        switch ($input)
        {
            '1' { 'you choose to list all the avalibale VMs on the pc'
                    $mypcs = "localhost"
                    get-vm -computername $mypcs
                }
            '2' {
                cls
                'You chose option #2'
                }
            '3' {
                cls
                'You chose option #3'
                }
            'q' {
                return
                }
        }
        pause
    }
    until ($input -eq 'q')

    I've tried to replace the

    $mypcs = "localhost"
                    get-vm -computername $mypcs

    with

    get-vm

    wihtout any variable, but still i'm getting errors

    Missing closing '}' in statement block or type definition. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingEndCurlyBrace
    
    

    PS: please don't solve the problem, if you could, please direct me to the solution.
    Thanks A lot, i've tried to search for a solution on google, but maybe i define the problem incorrectly so i dont get any correct answer.
    thanks alot.

  • #96156

    Participant
    Points: 134
    Helping Hand
    Rank: Participant

    I have tried your code and I didn't get any error.

    • #96158

      Participant
      Points: 0
      Rank: Member

      Hi,
      do you get the list of the Vms when you choose "1"?

  • #96159

    Participant
    Points: 134
    Helping Hand
    Rank: Participant

    No, only when I choose "q"

    • #96162

      Participant
      Points: 0
      Rank: Member

      that is the problem, it's supposed to list the vm's when i choose "1", and exit on "q"

  • #96164

    Participant
    Points: 1
    Rank: Member

    Problem is not in the switch statement.
    I replaced get-vm with get-process and it works like a charm.

    Whats the error message you get when you try:
    $mypcs = "localhost"
    get-vm -computername $mypcs

    • #96171

      Participant
      Points: 0
      Rank: Member

      here is a strange thing, i got all the vm's on my pc

      PS C:\WINDOWS\system32> $mypcs = "localhost"
      get-vm -computername $mypcs
      
      Name             State  CPUUsage(%) MemoryAssigned(M) Uptime           Status             Version
      ----             -----  ----------- ----------------- ------           ------             -------
      MDT-2013         Paused 0           1990              11:19:21.5000000 Operating normally 8.2    
      Windows-10-1709  Off    0           0                 00:00:00         Operating normally 8.2    
      Windows-7-32-bit Off    0           0                 00:00:00         Operating normally 8.2    
      

      by the way i've changed the cmdlet with test-connection and it did the trick..
      so i'm confused as hell

      here is the edited code with "test-connection" instead of "get-vm"

      do
      {
           $input = Read-Host " what do you want to do? 
                              `n1)  list all the Vms
                              `n2) do something to the Vms?
                              `n
                              `n * what is your choice?"
      
      
                    
           switch ($input)
           {
                 '1' {  'you choose to show all the avalibale on the pc'
                      
                      if ($input -eq '1') {
               
                       get-vm 
      
                      } 
                      
                 } '2' {
                      cls
                      'You chose option #2'
                 } '3' {
                      cls
                      'You chose option #3'
                 } 'q' {
                      return
                 }
           }
           pause
      }
      until ($input -eq 'q')
      
      

      here is the output

      * what is your choice?: 1
      you choose to show all the avalibale on the pc
      
      Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
      ------        -----------     -----------      -----------                              -----    -------- 
      LEVL-01       localhost       127.0.0.1        ::1                                      32       0        
      Press Enter to continue...:  what do you want to do? 

      just figure out that if i press "q" with the "get-vm" cmdlet it lists the vms :\

      any one?

      never mind i got it working

The topic ‘[Solved]"switch" statment - somthing missing’ is closed to new replies.