RSS

PS Switch

26 Mar

标准格式为

switch (collection)
{
<a> { cmd }
<b> { cmd }
default { cmd }
}

主要开关

-casesensitive  因switch默认是大小写不敏感的

-regex 使用正则

-wildcard 使用通配符

———————

脚本1

   1: $eventlog = Get-EventLog "application" -Newest 1
   2:  
   3: switch ($eventlog.Entrytype)
   4: {
   5: "error" { write-host "error:"  -ForegroundColor red
   6: $eventlog.Message }
   7: "warning" { write-host "warning:"  -ForegroundColor yellow
   8: $eventlog.Message }
   9: "information" { write-host "information:"  -ForegroundColor Green
  10: $eventlog.Message }
  11: }

脚本2,与脚本1相同

$eventlog = Get-EventLog "application" -Newest 1
 
switch ($eventlog)
{
{$_.Entrytype -eq "error"} { write-host "error:"  -ForegroundColor red
$eventlog.Message }
{$_.Entrytype -eq "warning"} { write-host "warning:"  -ForegroundColor yellow
$eventlog.Message }
{$_.Entrytype -eq  "information"} { write-host "information:"  -ForegroundColor Green
$eventlog.Message }
}

脚本3,使用通配符

cls
$file = dir c:\*.txt
switch -wildcard  ($file)
{
*con* { $_.name + " : this file exsit" }
*est* { $_.name + " ahah"}
default {"hahah"}
}

结果

 

脚本4,使用正则

cls
$file = dir c:\*.txt
switch -regex ($file)
{
tracert[1-3].txt { $_.name + ":this file exsit"}
tracert[4-6].txt { $_.name + ":this file is not exsit"}
}

结果

Advertisements
 
1 Comment

Posted by on March 26, 2009 in Powershell

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: