Powershell example 3
简介:
Blocktest.ps1
sb = {
foreach (sb = {
foreach (process in input) {input) {process.
Blocktest.ps1
$sb = {
foreach ($process in $input) {
$process.ProcessName
}
}
get-process | &$sb
 |
Functiontest.ps1
$a = "Hello"
function foo ($b) {
$b.ToUpper()
$b.ToLower()
}
$x = foo $a
$x
Script1.ps1
trap {
write-host "YIKES!!!"
throw $_
}
script2
Script2.ps1
trap {
write-host "In Script2"
break
}
$a = get-content C:/nofile.txt -erroraction stop
TrapTest.ps1
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 stop
write-host "$computer : " $a.ServicePackMajorVersion
}
write-host "Service Pack Versions:"
CheckWMI("DON-PC")
CheckWMI("TESTBED")
TrickyDebugging.ps1
$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
DebugTest.ps1
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 = 25
F1 $name, $age
|