Batch under windows (lower) - PowerShell

 Analyzing conditions 1.PowerShell 

Conditional expression

Comparison operators: -eq, -nc, -gt, -gc, -lt, -le, -contains, -notcontain

Logic operations: -and, -or, -xor: XOR, -not or! :inverse

The WHERE  Object condition filter

Analyzing can be filtered in the conduit duct results conditions, Where-Object be set individually filtered and the Results from retention.

IF ELSEIF ELSE

The conditions must be determined in parentheses, the code must be performed immediately following braces.

Switch

If the statement in a number of road branch, using the IF-ELSEIF-ELSE unfriendly, you can use the Switch

                           

When using the default switch -eq comparison operator equal, the comparison can also customize your condition, the condition into braces, must ensure conditions for the return value of Boolean expression "$ True" or "$ False".

If in case there are multiple conditions match, each matching condition will be handled, processed only once, you can use the break keyword if you see the matching conditions.

                

option there is a switch case, once this option is specified, a comparison operator switches from -eq to -ceq, i.e., case-sensitive comparison string.

After the switch statement specifies -wildcard option to use wildcards.

Keyword specifies option to switch -regex can use regular expressions.

                                     


2.PowerShell cycle

ForEach-Object

In ForEach-Object, the $ _ represents the current object, of course, also allows $ _, the method of the object support calls.

Foreach

Foreach-object cmdlet command is used in a pipeline, the pipeline processing results one by one, through the collection of the foreach keywords.

Do-While

do-while () first performs determination again, to ensure that the loop executes at least once.

For(初始条件;执行循环前的判断;执行循环后的操作)

Switch

switch本是多路分支的关键字,但是在powershell中由于switch支持集合,所以也可以使用它进行循环处理。


 3.PowerShell函数 

定义函数

函数的结果由三部分组成:函数名、参数、函数体

处理函数的参数

任意函数:内部变量$args接受函数调用时接受的参数,$args是一个数组类型。

命名参数:函数的每一个参数可以分配一个名称,在调用时通过名称指定对应的参数。

预定义参数:函数在定义参数时可以指定默认值,如果调用时没有专门指定参数的值,就会保持默认值。

                

指定函数的返回值

              

函数 过滤器 管道

在最简单的情况下,函数不是真正支持管道。只能对前一个命令执行后的结果处理。只能对前一个命令执行后的结果处理。前一个命令执行的结果通过被自动保存在$input变量值,$inpute是一个数组,它可以包含许多元素,一个元素,甚至一个元素都没有,这取决于具体的环境。

管道的低效率顺序模式在处理大容量数据时很容易出现问题,其结果是巨大的内存占用和进程等待。如果函数支持高效率的流模式,在处理管道结果时仅占很小的内存。

如果一个函数内部使用了管道,就可以定义三个基础的任务区:第一步,完成函数的初始化,完成函数执行的预备步骤;第二步处理递归调用所得的结果;最后进行收尾工作。这三个任务区分别可以使用begin,process,end语句块。


4.PowerShell脚本

编写和运行脚本

一个powershell脚本仅仅是一个包含powershell代码的文本文件。如果这个文本文件执行,powershell解释器会逐行解释并执行它的语句。powershell脚本非常像以前的cmd控制台上的批处理文件,可以通过文本编辑工具创建powershell脚本。

如果脚本不是很长,可以直接在控制台中要执行的语句重定向给一个脚本文件,缺点是代码必须放在闭合的引号中。书写方式一旦在脚本内部也有引号时,可以将脚本文件通过@‘ ‘@闭合起来,任何文本都可以存放在里面,即使是一些特殊字符,空号,白空格。

PowerShell般初始化情况下都会禁止脚本执行。脚本能否执行取决于 Power Shell的执行策略,只有管理员才有权限更改这个策略。非管理员会报错。
更改脚本执行策略,可以通过

脚本执行策略

  • Restricted    这是默认的设置。在任何条件下,根本没有 Power Shell脚本运行
  • AsIgned   只有数字签名的脚本(包括配置文件脚本)将会运行,此外,会提示你允许运行那些使用指定的证书签名的脚本;
  • Remotesigned    经本地编写的脚本将会运行。从网络下载的脚本运行,除非他们签名了并且你批准了的签名的证书;
  • UnRestricted      所有的脚本都将运行,但是,对于下载的脚本会警告你,而且,它们运行前必须批准
  • ByPass     任何脚本都将运行,而且不管其来源。这是一个潜在的非常危险的设置,只有在非常特定的情况下才使用;其中,其他的安全性系统已经准备好了,可以防上流氓脚本未经你的许可而运行
  • 未定义      如果没有认为的设置过主机的 Power Shel脚本执行策略。默认是 Restricted,没有脚本会运行

给脚本传递参数

增强脚本可读性

发布了103 篇原创文章 · 获赞 26 · 访问量 6132

Guess you like

Origin blog.csdn.net/qq_41210745/article/details/103786374