#眉標=PowerShell #副標=PowerShell = Shell + Script + .NET(7) #大標=函式與劇本 #作者=文/蔡學鏞 #引言= ==<反灰>=========== PS > function hello >> { >> $ofs="," >> "Hello there $args and how are you?" >> } >> PS > hello Bob Carol Ted Alice Hello there Bob,Carol,Ted,Alice and how are you? ================ ==<反灰>=========== PS > function Add-Two { $args[0] + $args[1] } PS > add-two 2 3 5 ================ ==<反灰>=========== PS > function Add-Two { >> $x,$y=$args >> $x+$y >> } >> PS > add-two 1 2 3 ================ ==<反灰>=========== function subtract ($from, $count) { $from - $count } ================ ==<反灰>=========== PS > subtract 5 3 2 ================ ==<反灰>=========== PS > subtract -from 5 -count 2 3 PS > subtract -from 4 -count 7 3 ================ ==<反灰>=========== subtract(1,2) ================ ==<反灰>=========== PS > function nadd ([int] $x, [int] $y) {$x + $y} ================ ==<反灰>=========== PS > nadd 1 2 3 PS > nadd "1" "2" 3 ================ ==<反灰>=========== PS > function a ($x, $y) { >> "x is $x" >> "y is $y" >> "args is $args" >> } >> PS > a 1 x is 1 y is args is PS > a 1 2 3 4 5 x is 1 y is 2 args is 3 4 5 ================ ==<反灰>=========== PS > function add ($x=1, $y=2) { $x + $y } ================ ==<反灰>=========== PS > add 3 PS > add 5 7 ================ ==<反灰>=========== PS > function dow ([datetime] $d = $(get-date)) >> { >> $d.dayofweek >> } >> ================ ==<反灰>=========== PS > dow Tuesday PS > dow "mar 11, 2008" Tuesday ================ ==<反灰>=========== PS > function numbers { 2+2; 9/3; [math]::sqrt(27) } PS > $result = numbers PS > $result.length 3 PS > $result[0] 4 PS > $result[1] 3 PS > $result[2] 5.19615242270663 ================ ==<反灰>=========== PS > function numbers >> { >> $i=1 >> while ($i -le 10) >> { >> $i >> $i++ >> } >> } >> PS > $result = numbers ================ ==<反灰>=========== return 2 ================ ==<反灰>=========== PS > dir function:/mkdir CommandType Name Definition ----------- ---- ---------- Function mkdir param([string[]]$pat... ================ ==<反灰>=========== PS > dir function:/mk* CommandType Name Definition ----------- ---- ---------- Function mkdir param([string[]]$pat... ================ ==<反灰>=========== PS > (dir function:/).count 78 ================ ==<反灰>=========== PS > function clippy { "I see you're writing a function." } ================ ==<反灰>=========== PS > (dir function:/).count 79 ================ ==<反灰>=========== PS > dir function:clippy CommandType Name Definition ----------- ---- ---------- Function clippy "I see you're writin... ================ ==<反灰>=========== PS > rm function:/clippy ================ ==<反灰>=========== "Hello world" ================ ==<反灰>=========== PS > ./hello.ps1 Hello world ================ ==<反灰>=========== PS > Set-ExecutionPolicy remotesigned PS > ./hello.ps1 Hello world ================ ==<反灰>=========== "Hello $args" ================ ==<反灰>=========== PS > ./hello Jerry Hello Jerry ================ ==<反灰>=========== param($name="world") "Hello $name!" ================ ==<反灰>=========== PS > ./hello Hello world! PS > ./hello Jerry Hello Jerry! PS > ================ ==<反灰>=========== function callExit { "calling exit from callExit"; exit} CallExit "Done my-script" ================ ==<反灰>=========== PS > ./my-script.ps1 calling exit from CallExit ================ ==<反灰>=========== C:\>powershell "'Hi there'; exit 17" Hi there ================ ==<反灰>=========== C:\>echo %ERRORLEVEL% 17 ================