Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - Break is not working ?

Reply
 
Old 08-26-2008   #1 (permalink)
IT Staff
Guest


 
 

Break is not working ?

$title = "Defrag Drive"
$message = "Select Drive letter to perform defragamention."

$cdrive = New-Object System.Management.Automation.Host.ChoiceDescription
"&C drive", "Analyse C:\ drive."
$ddrive = New-Object System.Management.Automation.Host.ChoiceDescription
"&D drive", "Analyse D:\ drive."
$quit = New-Object System.Management.Automation.Host.ChoiceDescription
"&Quit", "Quit this session."

$options = [System.Management.Automation.Host.ChoiceDescription[]]($cdrive,
$ddrive,$quit)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)

switch ($result)
{
0 {$cmdline = "cmd /c c:\windows\system32\defrag.exe c:\ -a >
c:\%computername%_defrag.txt"}
1 {$cmdline = "cmd /c c:\windows\system32\defrag.exe d:\ -a >
d:\%computername%_defrag.txt"}
2 {break}
}

$result
=====================
I select Q, and it suppose to quit session, but it did not ? Why ?





My System SpecsSystem Spec
Old 08-26-2008   #2 (permalink)
IT Staff
Guest


 
 

Re: Break is not working ?

Quit meaning i wld like to quit the program.

I wld like to make the choice via the switch statement, if select q, which
is equal to 2, after break, the program did not quit.

I find it very strange. It did not work at my side. Even i put break in
every switch statement, it still does not break.


My System SpecsSystem Spec
Old 08-26-2008   #3 (permalink)
Shay Levy [MVP]
Guest


 
 

Re: Break is not working ?

Hello IT,

It is working for me. What do you mean by 'quit session'?

Also, consider adding 'break; to each of the other switch items (unless you
want all items to be processed):

switch ($result){
0 {$cmdline = "cmd /c c:\windows\system32\defrag.exe c:\ -a > c:\%computername%_defrag.txt";
break}
1 {$cmdline = "cmd /c c:\windows\system32\defrag.exe d:\ -a > d:\%computername%_defrag.txt";
break}
2 {break}
}



---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic

IS> $title = "Defrag Drive"
IS> $message = "Select Drive letter to perform defragamention."
IS> $cdrive = New-Object
IS> System.Management.Automation.Host.ChoiceDescription
IS> "&C drive", "Analyse C:\ drive."
IS> $ddrive = New-Object
IS> System.Management.Automation.Host.ChoiceDescription
IS> "&D drive", "Analyse D:\ drive."
IS> $quit = New-Object
IS> System.Management.Automation.Host.ChoiceDescription
IS> "&Quit", "Quit this session."
IS> $options =
IS> [System.Management.Automation.Host.ChoiceDescription[]]($cdrive,
IS> $ddrive,$quit)
IS> $result = $host.ui.PromptForChoice($title, $message, $options, 0)
IS> switch ($result)
IS> {
IS> 0 {$cmdline = "cmd /c c:\windows\system32\defrag.exe c:\ -a
IS> >
IS> c:\%computername%_defrag.txt"}
IS> 1 {$cmdline = "cmd /c c:\windows\system32\defrag.exe d:\ -a
IS> >
IS> d:\%computername%_defrag.txt"}
IS> 2 {break}
IS> }
IS> $result
IS> =====================
IS> I select Q, and it suppose to quit session, but it did not ? Why ?


My System SpecsSystem Spec
Old 08-26-2008   #4 (permalink)
IT Staff
Guest


 
 

Re: Break is not working ?

u are right man.



"Shay Levy [MVP]" <no@xxxxxx> wrote in message
news:89228ed2406d88cad58f1e6a0c7d@xxxxxx
Quote:

> Hello IT,
>
> If you mean quit = close powershell then replace 'break' with 'exit'
>
> ...
> 2 {exit}
>
>
> ---
> Shay Levy
> Windows PowerShell MVP
> http://blogs.microsoft.co.il/blogs/ScriptFanatic
>
> IS> Quit meaning i wld like to quit the program.
> IS> IS> I wld like to make the choice via the switch statement, if select
> q,
> IS> which is equal to 2, after break, the program did not quit.
> IS> IS> I find it very strange. It did not work at my side. Even i put
> break
> IS> in every switch statement, it still does not break.
> IS>
>
>

My System SpecsSystem Spec
Old 08-26-2008   #5 (permalink)
Shay Levy [MVP]
Guest


 
 

Re: Break is not working ?

Hello IT,

If you mean quit = close powershell then replace 'break' with 'exit'

....
2 {exit}


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic

IS> Quit meaning i wld like to quit the program.
IS>
IS> I wld like to make the choice via the switch statement, if select q,
IS> which is equal to 2, after break, the program did not quit.
IS>
IS> I find it very strange. It did not work at my side. Even i put break
IS> in every switch statement, it still does not break.
IS>



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: How Many Laws did I break? Vista General
How to break an old raid Drivers
Did I break it ? General Discussion
Break away Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46