Powershell example 3

简介: Blocktest.ps1 sb = {  foreach (sb = {
 foreach (
process in input) {input) {process.
Blocktest.ps1
Image from book
$sb = {
  foreach ($process in $input) {
    $process.ProcessName
  }
}

get-process | &$sb
Image from book
Functiontest.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 stop
write-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 = 25

F1 $name, $age
Image from book

 

 

目录
打赏
0
0
0
0
20
分享
相关文章
Powershell example 1
Restart-Computers.ps1 # Define input parameters param ( [string] filename=(throw "Filename is required!") ) ...
661 0
Powershell example 2
ProcessCPU.ps1 #ProcessCPU.ps1 process=getprocesslow=0 #counter for low cpu processes $med=0 #counter for medi...
832 0
Powershell example 4
BulkCopy.ps1 #BulkCopy.ps1 Set-Location "C:/Logs" files=Get-ChildItem |where {_.
760 0
Powershell example 5
SetPermswithCACLS.ps1 #SetPermsWithCACLS.ps1 # CACLS rights are usually # F = FullControl # C = Change # R = Reado...
902 0
Powershell example 6
GetSecurityRSS.ps1 #GetSecurityRSS.ps1 #Query Microsoft's Basic Security Feed for latest bulletins #Critical bullet...
887 0
|
6月前
|
Powershell 重新排列去重 Windows环境变量
【9月更文挑战第13天】本文介绍如何使用PowerShell对Windows环境变量进行重新排列和去重。首先通过`$env:`访问环境变量,接着使用`-split`命令分割路径,再利用`Select-Object -Unique`去除重复项。之后可根据需要对路径进行排序,最后将处理后的路径组合并更新环境变量。注意修改环境变量前应备份重要数据并了解潜在影响。
198 10
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
210 0
|
10月前
|
windows可以安装Ubuntu,ubuntu上也可以安装Powershell
powerhsell除了可以在windows上使用外,还可以在Ubuntu上部署开发环境。下面介绍Ubuntu上安装powershell的方法。
271 0
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的
150 0

相关课程

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等