PowerShell 3.0 应该有更清晰的语法,它是什么样的?
我看到提到了改进的 PowerShell 3.0 语法,但还没有示例,它会是什么样子?
I've seen mentions of improved PowerShell 3.0 syntax but not an example yet, how will it look like?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
许多常见的
*-Object
cmdlet 利用多个参数集来完成简化的语法。在 V3 中看一下:注意:查看新的运算符
-NotIn
和-In
例如:因此简化的语法对于“常见”情况很好,但请注意因为你很容易掉进尖锐的岩石和熔岩中,例如:
这不会返回任何内容,更糟糕的是,没有错误,所以你认为结果集“正确”为空,而实际上这种语法并不像你那样工作可能会期待。也就是说,您无法访问属性的属性。在上面,PowerShell 查找名为
LastWriteTime.Year
的属性,该属性不存在。另请注意,作为简化语法的一部分,您现在可以使用
$PSItem
代替$_
,以防您或您为其编写脚本的人对以下内容产生某种过敏反应: <代码>$_。 :-)虽然这不一定与简化的语法相关,但我发现它简化了我的生活,而且我喜欢它:
A number of the common
*-Object
cmdlets utilize multiple parameter sets to accomplish the simplified syntax. Take a look at this in V3:NOTE: Check out the new operators
-NotIn
and-In
e.g.:So the simplified syntax is nice for the "common" case but watch out as you can fall off into the sharp rocks and lava pretty easily e.g.:
This returns nothing and even worse, there is no error so you think the result set is "correctly" empty when in fact this syntax just doesn't work as you might expect. That is, you can't access a property of a property. In the above, PowerShell looks for a property called
LastWriteTime.Year
which doesn't exist.Also note that as part of the simplified syntax you can now use
$PSItem
in place of$_
in case you or those you write scripts for have some sort of allergic reaction to$_
. :-)And while this isn't necessarily tied to the simplified syntax I find that it simplifies my life and I love it:
这是一个示例:
dir |其中 length -lt 10
在 3.0 之前,它会是
dir |其中 {$_.length -lt 10}
编辑:另一个例子,这次使用 foreach-object
dir | foreach-对象长度
Here's an example:
dir | where length -lt 10
Before 3.0, it would have been
dir | where {$_.length -lt 10}
edit: another example, this time with foreach-object
dir | foreach-object length
Powershell 确实已经拥有相当干净的语法,因此没有太多需要改进的地方。
我喜欢的一项新功能是
作为对象的哈希表
,您可以通过传递 hastable 及其属性来创建对象:因此,创建自定义对象的更新、更简洁的方法是:
重定向已加强了。除了正常(管道)和错误之外,您现在还拥有详细、调试和警告流,因此您可以执行诸如
5>&1
之类的重定向,您可以使用
$PSDefaultParameterValues
用于设置 cmdlet 的默认参数值的首选项变量。有新的
[ordered]
加速器来创建有序的 hastable(字典):从 SO 中的另一个答案,我意识到
-in
是 Powershell v3.0 中的新功能:所以你会做类似
1 -in 1,2,3
的事情。以前我们只有-contains
Cmdlet:
您可以使用
Update-Help
cmdlet 更新帮助。有一些与 Web 相关的 cmdlet,例如Invoke-
。您还可以使用Web请求
ConverTo-JSON
和ConvertFrom-JSON
cmdlet 处理 JSON。Powershell does have a pretty clean syntax already, so there is not much that needs improvement.
One new addition that I do like is the
Hash Table as objects
, where you can create objects by passing hastable with its properties:So the newer, more succinct way of creating custom objects is:
The redirection has been beefed up. You have now streams for verbose, debug and warning in addition to normal ( pipeline ) and error and so you can do redirections like
5>&1
You can use
$PSDefaultParameterValues
preference variable to set default parameter values for cmdlets.There is the new
[ordered]
accelerator to create ordered hastable (dictionary):From another answer here in SO, I realized that
-in
was new in Powershell v3.0:So you do something like
1 -in 1,2,3
. Previously we only had-contains
Cmdlets:
You can update help with
Update-Help
cmdlet. There are web related cmdlets likeInvoke-
. You can also handle JSON usingWebRequest
ConverTo-JSON
andConvertFrom-JSON
cmdlets.