Author Posts

March 16, 2018 at 10:10 am

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.

March 16, 2018 at 10:58 am

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

March 16, 2018 at 11:01 am

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

March 16, 2018 at 11:13 am

No, only when I choose "q"

March 16, 2018 at 11:21 am

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

March 16, 2018 at 11:40 am

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

March 16, 2018 at 11:48 am

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