/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" Dir, Dir and more Dir in MSH "," Simple Show-Image function for Monad "," MSH out-zip Function "," get a Users LastLogontime and Domaincontroller fro... "," Some fun With Monads Add-Member, MP3 files and COM... "," MSH snap-in to Translate IADsLargeInteger to Int64... "," Commandline editing with MSH. "," MSH Scroll-host function "," Series about Monad on Computerworld "," Watching FSRM Quota and filescreen events from Mon... "," ")

Tuesday, January 31, 2006

 


Switch -regEx



The Switch statement has an handy option -Regex to use a Regular expresion in it.

the About_Switch help seems missing in Beta 3 but, there is some info in the About_break Help about it.

when you type :

Help about_Break

Part of the Help contains this Example :

In this example, $var is created and initialized to a string value of
"word2". The switch statement uses regex (a regular expression .NET
class) to match the variable value first with the term "word2". Because
the variable value and the first test in the switch statement match,
the first code block in the switch statement runs. When MSH reaches the
first break statement, the switch statement exits. If the four break
statements were removed from the example, then all four conditions
would be met. Thus, this example uses the break statement to display
results when the most specific condition is met.


 $var = "word2"
 switch -regex ($var)
 {
     "word2"
     {
         write-host "Exact" $_
         break
     }


     "word.*"
     {
         write-host "Match on the prefix" $_
         break
     }


     "w.*"
     {
         write-host "Match on at least the first letter" $_
         break
     }

     default
     {
         write-host "No match" $_
         break
     }
 }


This was wat I was looking for in the Dir example in Last post, Hence I came up with the work-around with scriptblocks and -Match.

I found this by using the following command :

MSH>ls $MSHHOME\*.txt | match-string "switch"
about_break.help.txt:5:    A statement for immediately exiting foreachforwhiledo, or switch
about_break.help.txt:10:    or do loop or in a switch statement, ends the code block. In the case
about_break.help.txt:12:    In the case of the switch statement, the break statement causes a code
about_break.help.txt:13:    block inside of a switch statement to exit and thus the entire switch
about_break.help.txt:69:    A switch statement is not a looping construct, but the break statement
about_break.help.txt:71:    met. For example, the following switch statement uses break statements
about_break.help.txt:75:        switch -regex ($var)
about_break.help.txt:103:    "word2". The switch statement uses regex (a regular expression .NET
about_break.help.txt:105:    the variable value and the first test in the switch statement match,
about_break.help.txt:106:    the first code block in the switch statement runs. When MSH reaches the
about_break.help.txt:107:    first break statement, the switch statement exits. If the four break
about_break.help.txt:119:    For information about the switch statement, enter the following
about_break.help.txt:122:        help about_switch
about_if.help.txt:35:    elseif statements within it, consider using a switch statement instead.
about_if.help.txt:87:    For information about the switch statement, enter the following command
about_if.help.txt:90:        help about_switch
about_Reserved_words.help.txt:20:    foreach     while        if           switch


I did find it this way before in Beta 2 but could not find it yesterday.

enjoy,

gr /\/\o\/\/
Tags :


Comments: Post a Comment

Links to this post:

Create a Link



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?