powershell例子

例子如下:

$ErrorActionPreference="Stop"

function getlist{
    ls D:\tmp2|select name,extension,fullname|export-csv -encoding oem list.csv
}

function do_continue{
    $xx=0
    while( $xx -lt 10){
        $xx++
        if($xx -eq 5){
            continue
        }
        write-host $xx   
    }
}

function do_break{
    $xx=0
    while($true){
        write-host $xx
        $xx++
        if($xx -gt 5){
            break
        }
    }
}

function do_for{
    for($ii=0;$ii -lt 10;$ii++){
        write-host $ii
    }
}

function do_foreach{
    foreach ($i in 1,2,3){
        write-host $i
    }
}

function do_logic{
    write-host (1+1) (2*3) (2/2)
    write-host (1 -lt 2);
    write-host (1 -gt 2);
    write-host (1,2,3 -contains 2);
    write-host (1,2,3 -notcontains 2);
    write-host ('one two' -match 'one')
    write-host ('one two' -notmatch 'one')
}

function do_function{

    Begin{
        write-host "begin"
    }
    
    Process{
        write-host "$_"
    }
    
    End{
        write-host "end"
    }
}

function do_input{
    1,2,3 |do_function
}

function do_hashtable{
    $hashx=@{msg1="message 1";meg2="message 2";msg3="message 3"}
    write-host $hashx["msg1"]
    $hashx
}

function do_switch{
    $ii=4
    switch($ii){
        1 {"one";break}
        2 {"two";break}
        3 {"three";break}
        Default{"default"}
    }
}

do_switch

#read-host "please enter to exit"

 

posted @ 2014-04-10 23:45  zhizhou  阅读(312)  评论(0编辑  收藏  举报