-
脚本块是一种特殊的命令模式。一个脚本块可以包含许多的 Powershell命令和语句。它通常使用大括号定义。最小最短的脚本块,可能就是一对大括号,中间什么也没有。可以使用之前的调用操作符“&”执行脚本块: PS E:> & {"当前时间:" + (get-date) } 当前时间:08/08/2012 22:30:24查看全部
-
-f Format operator Format a string expression. Place {0} {1} etc. into the string as placemarkers where you want the variables to appear, immediately follow the string with the -f operator and then lastly, the list of comma separated variables which will be used to populate the placemarkers. Get-ChildItem c:\ | ForEach-Object {'File {0} Created {1}' -f $_.fullname,$_.creationtime} Optional format string(s) can be included to add padding/alignment and display dates/times/percentages/hex etc correctly, see the -f format page for full details.查看全部
-
. Dot sourcing operator Run a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope. (without dot sourcing, the variables created within a script will all disappear when the script finishes.) . C:\sample1.ps1 . .\sample2.ps1 Note: The dot sourcing operator is followed by a space. Use the space to distinguish the dot from the dot (.) symbol that represents the current directory.查看全部
-
& Call operator Run a command, script, or script block. The call operator, also known as the "invocation operator," lets you run commands that are stored in variables and represented by strings. Because the call operator does not parse the command, it cannot interpret command parameters. C:\PS> $c = "get-executionpolicy" C:\PS> $c get-executionpolicy C:\PS> & $c AllSigned查看全部
-
:: Static member operator Call the static properties operator and methods of a .NET Framework class. To find the static properties and methods of an object, use the -Static parameter of Get-Member: [datetime] | gm -static [datetime]::now [datetime]::Utcnow查看全部
-
@( ) Array SubExpression operator. An array subexpression behaves just like a subexpression except that it guarantees that the output will be an array. This works even if there is no output at all (gives an empy array.) If the result is a scalar value then the result will be a single element array containg the scalar value. (If the output is already an array then the use of an array subexpression will have no effect, it won't wrap one array inside of another array.) PS C:\> @(Get-WMIObject win32_logicalDisk)查看全部
-
$( ) SubExpression operator. Use a subexpression to return specific properties of an object. Unlike simple brackets, a subexpression can contain multiple ; semicolon ; separated ; statements. The output of each statement contributes to the output of the subexpression. For a single result, it will return a scalar. For multiple results, it will return an array. Subexpressions allow you to evaluate and act on the results of an expression in a single line; with no need for an intermediate variable: if($(code that returns a value/object) -eq "somevalue") { do_something } PS C:\> "The result of 2 + 3 = $(2+3)" PS C:\> $($x * 64) PS C:\> $(Get-WMIObject win32_Directory)查看全部
-
那怎样突破优先级的限制执行指定的命令呢?方法都是大同小异,通过特定信息过滤到指定的CommandInfo对象,然后直接使用我们本篇的调用操作符。例如当存在如下的命令Ping命令 CommandType Name ----------- ---- Alias Ping Function Ping Application PING.EXE 我想调用第三个,可以使用: PS E:> $command=Get-Command -Name ping | where {$_.CommandType -eq "Application" } PS E:> & $command查看全部
-
通过命令名称唯一标识一条命令 但是可能存在别名,命令,函数的的名称一样,那Powershell会不会纠结到底执行哪个呢?当然不会,因为它们之间是有一个优先级的。从高到底,依次为: Alias(1) Function(2) Filter(2) Cmdlet(3) Application(4) ExternalScript(5) Script (-) 下面举个例子: PS E:> function Ping(){"我是Ping函数"} PS E:> Set-Alias -Name Ping -Value echo CommandType Name Definition ----------- ---- ---------- Alias Ping echo Function Ping param()... Application PING.EXE C:windowsSYSTEM32PING.EXE PS E:> ping "测试我的Ping,估计执行的别名echo" 测试我的Ping,估计执行的别名echo PS E:> del Alias:Ping PS E:> ping ; #别名已经被删除,估计执行的是函数PING 我是Ping函数 PS E:> del Function:Ping PS E:> ping baidu.com ; #函数已经被删除,估计执行的是函数ping 程序 正在 Ping baidu.com [220.181.111.85] 具有 32 字节的数据: 请求超时。 请求超时。查看全部
-
为什么会这样呢?追根溯源,Powershell中的调用符,首先会使用get-command去发现命令是否可用,而get-command的确只支持单独的一条命令,不支持命令串或者脚本串。 调用操作符执行CommandInfo对象 调用操作符初始化时会将指定的文本传递给get-command,然后有get-command去检索命令,事实上,调用操作符甚至可以直接执行一个CommandInfo对象,绕过自身的内部get-command,例如: PS E:> $command=Get-Command tasklist PS E:> $command CommandType Name ----------- ---- Application tasklist.exe PS E:> & $command 映像名称 PID 会话名 会话# 内存使用 ========================= ======== ================ =========== ============ System Idle Process 0 Services 0 24 K System 4 Services 0 560 K查看全部
-
调用操作符只能接受单个命令 但是调用操作符不能接受全部的Powershell脚本或命令,只能接受单个的一条命令,例如使用: $command = "Dir $env:windir" & $command 就会报错 无法将“Dir C:windows”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后重试。 所在位置 E:MyScript.ps1:6 字符: 2 + & <<<< $command + CategoryInfo : ObjectNotFound: (Dir C:windows:String) [], Comm andNotFoundException + FullyQualifiedErrorId : CommandNotFoundException查看全部
-
调用操作符“&”虽然简短,但是给我们执行Powershell命令提供了很大的方便。如果你之前将Powershell命令存储在了一个字符串中,或者一个变量中。此时,调用操作符就可以将字符串直接解释成命令并执行,如果在Powershell控制台中,你只须要输入即可。查看全部
-
如果指令中将erroraction设置为continue了,那么trap捕获不到这次异常。 $erroractionpreference="stop" $errorlog="E:\PSTest\el.log" trap{ write-host "++++++++++++asdfsadfsdf++++++++++++++++++++" } remove-item aaaaaa -ea Continue get-process|select -First 2 remove-item bbbbbb write-host "OK"查看全部
-
打出异常出现位置: trap{ $er=([System.Management.Automation.ErrorRecord]$_).InvocationInfo $now=get-date $now.ToString("yyyy MM dd hh-mm-ss") + "->"+ $er.ScriptLineNumber+" -- "+$er.offsetinline >> $errorlog continue }查看全部
-
split-path的作用:可以去掉路径中最后一个\后面的所有内容: PS C:\Users\lenovo> split-path $profile C:\Users\lenovo\Documents\WindowsPowerShell PS C:\Users\lenovo> $profile C:\Users\lenovo\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 PS C:\Users\lenovo> split-path (split-path $profile) C:\Users\lenovo\Documents PS C:\Users\lenovo>查看全部
举报
0/150
提交
取消