设为首页 收藏本站
查看: 976|回复: 0

Windows PowerShell Language Quick Reference

[复制链接]

尚未签到

发表于 2017-5-20 06:41:00 | 显示全部楼层 |阅读模式
Windows PowerShell Language Quick Reference

Native Support for Different Type Systems
Windows PowerShell adapts WMI, XML, ASDI, <city w:st="on"><place w:st="on">ADO</place></city>, and COM objects to provide a common syntax to access their properties and methods.  
Example
$g = Get-WmiObject Win32_Process
$g[0].Name  # instead of $g[0].Properties[“Name”]


Arithmetic Binary Operators
[table]
+
addition, concatenation
-
Subtraction
*
multiplication, string repetition
/
Division
%
Modulus


[/td][/tr][tr][td=1,1,288]Array Operations
[/td][/tr][tr][td=1,1,288]Does this array have a <metricconverter w:st="on" productid="3 in">3 in</metricconverter> it
1,2,3,5,3,2 –contains 3

Return all elements equal to 3:

1,2,3,5,3,2 –eq 3

Return all elements less than 3:
1,2,3,5,3,2 –lt 3

Test if 2 exists in collection:
if (1, 3, 5 –contains 2) …
Other operators:  -gt, -le, -ge, -ne

[/td][/tr][tr][td=1,1,288]Arrays
[/td][/tr][tr][td=1,1,288]
“a”,“b”,”c”
array of strings
1,2,3
array of integers
@()
empty array
@(2)
array of 1 element
1,(2,3),4
array within array
,”hi”
Array of one element
$a[5]
sixth element of array*
$a[2][3]
fourth element or the third

element of an array
$a[2..20]
Return elements 3 thru 21
·        Arrays are zero based.

[/td][/tr][tr][td=1,1,288]Assignment Operators
[/td][/tr][tr][td=1,1,288]=, +=, -=, *=, /=, %=


[/td][/tr][tr][td=1,1,288]Associative Arrays (Hashtables)
[/td][/tr][tr][td=1,1,288]
$hash = @{ }
Create empty hashtable
$h =@{foo=1;bar=2}
Create and initialize a hashtable
$hash.key1 = 1
Assign 1 to key “key1”
$hash.key1
Returns value of key1
$hash["key1"]
Returns value of key1

[/td][/tr][tr][td=1,1,288]Boolean Values and Operators
[/td][/tr][tr][td=1,1,288]
TRUE
FALSE
$TRUE
$FALSE
Any string of length > 0 except the word “false”
Empty string or the string “false”
Any number !=0
Any number = 0
Array of length > 1
Array of length 0
Array of length 1 whose element is TRUE
Array of length 1 whose element is FALSE
A reference to any object
Null

[/td][/tr][tr][td=1,1,288]Break (Scripting)
[/td][/tr][tr][td=1,1,288]The break commands exits a loop. It can take an optional LABEL to break to
Example:
while (1)
{        $a = something
          if ($a –eq 1) break;
}

[/td][/tr][tr][td=1,1,288]Command Expansion Operators
[/td][/tr][tr][td=1,1,288]
$( )
Returns null
$(1,2,3)
Returns an array containing1,2,3.
$(Get-Alias a*)
Returns evaluation of the expression
@(Get-Alias;Get-Process)
Executes the two commands and returns the results in an array

[/td][/tr][tr][td=1,1,288]Comments
[/td][/tr][tr][td=1,1,288]# This is a comment because # is the first char of a token
$a = “#This is not a comment…”
$a = “something” # …but this is.
Write-Host Hello#world

[/td][/tr][tr][td=1,1,288]Comparison Operators
[/td][/tr][tr][td=1,1,288]
-eq
Equal
-ne
Not equal
-gt –ge
Greater than, greater than or equal to
-lt –le
Less than, less than or equal to

“i” or “c” may be prepended to get case-insensitive or case-sensitive operations (for example, –ceq )
[/td][/tr][tr][td=1,1,288]Continue (Scripting)
[/td][/tr][tr][td=1,1,288]The continue statement continues the next iteration of a loop without breaking out of it. Example:
while (1)
{               $a = something
      if ($a –eq 1) (continue)
      # This line is not reached unless $a == 1
}
#  This line is never reached.
[/td][/tr][tr][td=1,1,288]Dot Sourcing
[/td][/tr][tr][td=1,1,288]Dot sourcing allows running functions, script blocks, and scripts in the current scope rather than a local one. Example:
. MyFunction

If MyFunction sets a variable, it is set in the current scope rather than the function’s local scope.
$a = {$x = Get-Process | Select –First 2}
. $a    #Evaluates the script block in the current scope

[/td][/tr][tr][td=1,1,288]Escape Sequences
[/td][/tr][tr][td=1,1,288]The Windows PowerShell escape character is the backwards apostrophe, or `.  To make a character literal, precede it with `.  To specify a ` use ``.
Special escape sequences
`0
(null)
`a
(alert)
`b
(backspace)
`f
(form feed)
`n
(new line)
`r
(carriage return)
`t
(tab)
`v
(vertical quote)


[/td][/tr][tr][td=1,1,288]Execution Order
[/td][/tr][tr][td=1,1,288]Windows PowerShell attempts to resolve commands in the following order: aliases, functions, cmdlets, scripts, executables, and normal files.

[/td][/tr][tr][td=1,1,288]For (Scripting)
[/td][/tr][tr][td=1,1,288][:label] for ([initializer]; [condition]; [iterator]) {}

Example:
for ($i = 0; $i –lt 5; $i++) {Write-Object $i}

[/td][/tr][tr][td=1,1,288]Foreach (Scripting)
[/td][/tr][tr][td=1,1,288][:label]
foreach (identifier in collection) {}
Expression | foreach {}
Expression | foreach {BEGIN{} PROCESS{} END{}}

Examples:
$i = 1,2,3
foreach ($z in $i) {Write-Object $z}
Get-Process |foreach {BEGIN{$x=1}
       PROCESS{$X++}
       END{“$X Processes”}}

[/td][/tr][tr][td=1,1,288]Functions (Scripting)
[/td][/tr][tr][td=1,1,288]function MyFunction {
      write-object $args[0]
}

function test ([string]$label=”default label”,[int]$start=0)
{ BEGIN {$x=$start} PROCESS {“$label: $_”’; $x++}
     END{“$x total”}
}
[/td][/tr][tr][td=1,1,288]Filters (Scripting)
[/td][/tr][tr][td]padding-right: 5.4pt; border-top: #ece9d8; padding-left: 5.4pt; padding-bottom: 0cm; width: 216pt; padding

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-379173-1-1.html 上篇帖子: Powershell学习笔记二 下篇帖子: powershell基本语法和命令
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表