SlideShare a Scribd company logo
1 of 14
PowerShell – ScriptBlock
Howard 2013/11/29
Script Block


用 { …… } 定義 Script Block



將 Script Block 指派給某一變數




$variable = { …… }

透過變數執行 Script Block - &


&$variable
Script Block
Script Block - 返回值

Script Block - 返回值


回傳值後,後續的 script 仍會繼續執行



故 $result = 5
Script Block - 返回值
return


使用 return 以結束執行 script block
Script Block - 返回值
array

Script Block – 參數
$args


預設變數 $args,用來取得呼叫 script block 時的參數
Script Block – 參數
param(param1, param2……)


按順序指定參數


缺點:要依參數順序給值



指定參數名稱後給值



指定參數名稱,可以縮寫


名稱需要足夠的長度,才能
視別不同的參數
Script Block – 參數
param – 型別定義、預設值、強制參數

Script Block - Pipe


begin




在 pipe 執行之前會進入此 block

process





Pipe 的每個 element 都會進此 block
$_ 指的是當前物件

end


在 pipe 執行之前會進入此 block
Script Block - Pipe
Script Block - Pipe
Reference

More Related Content

What's hot

A Brief Introduction to Regular Expression with Python 2.7.3 Standard Library
A Brief Introduction to Regular Expression with Python 2.7.3 Standard LibraryA Brief Introduction to Regular Expression with Python 2.7.3 Standard Library
A Brief Introduction to Regular Expression with Python 2.7.3 Standard LibraryWen Liao
 
Avm2虚拟机浅析与as3性能优化(陈士凯)
Avm2虚拟机浅析与as3性能优化(陈士凯)Avm2虚拟机浅析与as3性能优化(陈士凯)
Avm2虚拟机浅析与as3性能优化(陈士凯)FLASH开发者交流会
 
Java7 fork join framework and closures
Java7 fork join framework and closuresJava7 fork join framework and closures
Java7 fork join framework and closureswang hongjiang
 
Coding guideline
Coding guidelineCoding guideline
Coding guideline斯理 衛
 
Effective linux.1.(commandline)
Effective linux.1.(commandline)Effective linux.1.(commandline)
Effective linux.1.(commandline)wang hongjiang
 
Hello Javascript
Hello JavascriptHello Javascript
Hello JavascriptBaidu, Inc.
 
Arduino應用系統設計 - Arduino程式快速入門
Arduino應用系統設計 - Arduino程式快速入門Arduino應用系統設計 - Arduino程式快速入門
Arduino應用系統設計 - Arduino程式快速入門吳錫修 (ShyiShiou Wu)
 
shell script introduction
shell script introductionshell script introduction
shell script introductionJie Jin
 
Maintainable PHP Source Code
Maintainable PHP Source CodeMaintainable PHP Source Code
Maintainable PHP Source CodeBo-Yi Wu
 
Cppcheck分析
Cppcheck分析Cppcheck分析
Cppcheck分析Wu Liang
 
compiler fuzzer : prog fuzz介紹
compiler fuzzer : prog fuzz介紹compiler fuzzer : prog fuzz介紹
compiler fuzzer : prog fuzz介紹fdgkhdkgh tommy
 

What's hot (13)

A Brief Introduction to Regular Expression with Python 2.7.3 Standard Library
A Brief Introduction to Regular Expression with Python 2.7.3 Standard LibraryA Brief Introduction to Regular Expression with Python 2.7.3 Standard Library
A Brief Introduction to Regular Expression with Python 2.7.3 Standard Library
 
nodeMCU IOT教學02 - Lua語言
nodeMCU IOT教學02 - Lua語言nodeMCU IOT教學02 - Lua語言
nodeMCU IOT教學02 - Lua語言
 
Avm2虚拟机浅析与as3性能优化(陈士凯)
Avm2虚拟机浅析与as3性能优化(陈士凯)Avm2虚拟机浅析与as3性能优化(陈士凯)
Avm2虚拟机浅析与as3性能优化(陈士凯)
 
Java7 fork join framework and closures
Java7 fork join framework and closuresJava7 fork join framework and closures
Java7 fork join framework and closures
 
Coding guideline
Coding guidelineCoding guideline
Coding guideline
 
Effective linux.1.(commandline)
Effective linux.1.(commandline)Effective linux.1.(commandline)
Effective linux.1.(commandline)
 
Hello Javascript
Hello JavascriptHello Javascript
Hello Javascript
 
Arduino應用系統設計 - Arduino程式快速入門
Arduino應用系統設計 - Arduino程式快速入門Arduino應用系統設計 - Arduino程式快速入門
Arduino應用系統設計 - Arduino程式快速入門
 
shell script introduction
shell script introductionshell script introduction
shell script introduction
 
Maintainable PHP Source Code
Maintainable PHP Source CodeMaintainable PHP Source Code
Maintainable PHP Source Code
 
Cppcheck分析
Cppcheck分析Cppcheck分析
Cppcheck分析
 
functional-scala
functional-scalafunctional-scala
functional-scala
 
compiler fuzzer : prog fuzz介紹
compiler fuzzer : prog fuzz介紹compiler fuzzer : prog fuzz介紹
compiler fuzzer : prog fuzz介紹
 

Viewers also liked

Ken - caman.js,SoundManager2.js,CSS3 Animations
Ken - caman.js,SoundManager2.js,CSS3 AnimationsKen - caman.js,SoundManager2.js,CSS3 Animations
Ken - caman.js,SoundManager2.js,CSS3 AnimationsLearningTech
 
Design pattern builder 20131115
Design pattern   builder 20131115Design pattern   builder 20131115
Design pattern builder 20131115LearningTech
 
20121228 jQueryui - dialog - By Drake
20121228 jQueryui - dialog - By Drake20121228 jQueryui - dialog - By Drake
20121228 jQueryui - dialog - By DrakeLearningTech
 
20151120 ian cocos2d js
20151120 ian cocos2d js20151120 ian cocos2d js
20151120 ian cocos2d jsLearningTech
 
Expression 的介紹及應用
Expression 的介紹及應用Expression 的介紹及應用
Expression 的介紹及應用LearningTech
 

Viewers also liked (7)

Ken - caman.js,SoundManager2.js,CSS3 Animations
Ken - caman.js,SoundManager2.js,CSS3 AnimationsKen - caman.js,SoundManager2.js,CSS3 Animations
Ken - caman.js,SoundManager2.js,CSS3 Animations
 
Design pattern builder 20131115
Design pattern   builder 20131115Design pattern   builder 20131115
Design pattern builder 20131115
 
Bcp
BcpBcp
Bcp
 
20121228 jQueryui - dialog - By Drake
20121228 jQueryui - dialog - By Drake20121228 jQueryui - dialog - By Drake
20121228 jQueryui - dialog - By Drake
 
20151120 ian cocos2d js
20151120 ian cocos2d js20151120 ian cocos2d js
20151120 ian cocos2d js
 
Expression 的介紹及應用
Expression 的介紹及應用Expression 的介紹及應用
Expression 的介紹及應用
 
Peggy - XPath
Peggy - XPathPeggy - XPath
Peggy - XPath
 

More from LearningTech

More from LearningTech (20)

vim
vimvim
vim
 
PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Expression tree
Expression treeExpression tree
Expression tree
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
Reflection & activator
Reflection & activatorReflection & activator
Reflection & activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
Node child process
Node child processNode child process
Node child process
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
git command
git commandgit command
git command
 

Power shell – scriptblock

  • 2. Script Block  用 { …… } 定義 Script Block  將 Script Block 指派給某一變數   $variable = { …… } 透過變數執行 Script Block - &  &$variable
  • 4. Script Block - 返回值 
  • 5. Script Block - 返回值  回傳值後,後續的 script 仍會繼續執行  故 $result = 5
  • 6. Script Block - 返回值 return  使用 return 以結束執行 script block
  • 7. Script Block - 返回值 array 
  • 8. Script Block – 參數 $args  預設變數 $args,用來取得呼叫 script block 時的參數
  • 9. Script Block – 參數 param(param1, param2……)  按順序指定參數  缺點:要依參數順序給值  指定參數名稱後給值  指定參數名稱,可以縮寫  名稱需要足夠的長度,才能 視別不同的參數
  • 10. Script Block – 參數 param – 型別定義、預設值、強制參數 
  • 11. Script Block - Pipe  begin   在 pipe 執行之前會進入此 block process    Pipe 的每個 element 都會進此 block $_ 指的是當前物件 end  在 pipe 執行之前會進入此 block