Powershell example 3

来源:互联网 发布:linux dns 缓存时间 编辑:程序博客网 时间:2024/05/18 10:56
Blocktest.ps1
Image from book
$sb = {  foreach ($process in $input) {    $process.ProcessName  }}get-process | &$sb
Image from bookFunctiontest.ps1
Image from book
$a = "Hello"function foo ($b) {  $b.ToUpper()  $b.ToLower()}$x = foo $a$x
Image from book
Script1.ps1
Image from book
trap { write-host "YIKES!!!" throw $_}script2
Image from book 
Script2.ps1
Image from book
trap {  write-host "In Script2"  break}$a = get-content C:/nofile.txt -erroraction stop
Image from book
TrapTest.ps1
Image from book
function CheckWMI ($computer) {  trap {    write-host "An error occured: "    write-host "ID: " $_.ErrorID    write-host "Message: "$_.Exception.Message    throw "Couldn't check $computer"}$a = get-wmiobject Win32_OperatingSystem ` -property ServicePackMajorVersion ` -computer $computer -ea stopwrite-host "$computer : " $a.ServicePackMajorVersion}write-host "Service Pack Versions:"CheckWMI("DON-PC")CheckWMI("TESTBED")
TrickyDebugging.ps1 
Image from book
$foo = "this is the original text"function f1($str){  "Calling f1..."  $str.toUpper()}function f2($value){  "Calling f2... what is value?"  $value | get-member  ""  "Before f1 value is: " + $value  "Before f1 foo is: " + $script:foo  $script:foo = f1 $value  "after f1 value is: " + $value  "after f1 foo is: " + $script:foo}"""BEFORE PASS 1, WHAT IS FOO?"$foo | get-member"""PASS 1"f2 $foo"""AFTER PASS 1, WHAT IS FOO?"$foo | get-member"""""PASS 2"f2 $foo"""global value"$foo
Image from book

DebugTest.ps1

Image from book
function F1 {  param ($n, $a)  if (F2($a)) {    "$n is old enough to vote"  } else {    "$n is too young to vote"  }}function F2 {  param ($var)  if ($var -gt 17) {    $true  } else {    $false  }}[string]$name = "Joe"[int]$age = 25F1 $name, $age
Image from book

 

 

原创粉丝点击