这个数字序列有效,但它是否正确?(This number sequence works but is it correct?)

编程入门 行业动态 更新时间:2024-10-27 11:16:45
这个数字序列有效,但它是否正确?(This number sequence works but is it correct?)

我需要将虚拟硬盘添加到多个虚拟机中,所以我最终使用它来执行此操作:

get-vm server[2..3..6][1-3].domain.com | new-harddisk -CapacityGB 10

这为服务器增加了一个10gig硬盘:

server21.domain.com server22.domain.com server23.domain.com server31.domain.com server32.domain.com server33.domain.com server61.domain.com server62.domain.com server63.domain.com

这就是我想要的......但我使用的语法是否正确? 我知道1..3通常会扩展到1 2 3,但在这种情况下,这并不是为什么我在第二部分使用1-3而2..3..6最终只做2 3 6。

有人可以验证吗?

I needed to add an virtual HDD to several VMs so I ended up using this to do it:

get-vm server[2..3..6][1-3].domain.com | new-harddisk -CapacityGB 10

This added a 10gig HDD to server:

server21.domain.com server22.domain.com server23.domain.com server31.domain.com server32.domain.com server33.domain.com server61.domain.com server62.domain.com server63.domain.com

That is what I wanted... but is the syntax I used correct? I know that 1..3 would normally expand to 1 2 3 but in this case it does not which is why i used 1-3 in the second part and 2..3..6 ended up only doing 2 3 6.

Can somebody please verify?

最满意答案

1..3是生成数组的PowerShell数组语法,但在Get-VM xyz[1..3]的上下文中却没有。 您可以从PowerShell ISE中的着色中看到:

如何解析它显示ISE语法高亮的示例

如果您通过解析器运行它并查看它是如何被读取的,您也可以看到它:

{get-vm server[1..3..5]}.Ast.FindAll({$true}, $true) # includes output StringConstantType : BareWord Value : server[1..3..5] StaticType : System.String

{get-vm $server[1..3..5]}.Ast.FindAll({$true}, $true) Operator : DotDot Left : 1 Right : 3 Value : 1 StaticType : System.Int32 Value : 3 StaticType : System.Int32 Value : 5 StaticType : System.Int32

换句话说,你的语法对于PowerShell数组扩展是错误的(它们不能链接在一起),但这些不是PowerShell数组文字,它们是作为参数传递给Get-VM cmdlet的文字字符串,这可能是(推测) )以某种方式将它们视为通配符。

Hyper-V Get-VM表示它不支持通配符。

VMware Get-VM确实在其示例中演示了通配符。

从使用PowerCLI开始,它看起来像..被忽略了, [236]意味着“ 2,3或6在这个位置 ”, [1-3]意味着“ 1到3之间的数字位置 “`。 哪个是你想要的输出,所以在这个意义上,语法很好。 并且可能比您能够使用PS阵列更容易。

1..3 is PowerShell array syntax to generate an array, but in the context of Get-VM xyz[1..3] it's not. You can see from the colouring in the PowerShell ISE:

Examples of how it's being parsed showing ISE syntax highlighting

You can also see it if you run it through the parser and see how it's being read:

{get-vm server[1..3..5]}.Ast.FindAll({$true}, $true) # includes output StringConstantType : BareWord Value : server[1..3..5] StaticType : System.String

vs.

{get-vm $server[1..3..5]}.Ast.FindAll({$true}, $true) Operator : DotDot Left : 1 Right : 3 Value : 1 StaticType : System.Int32 Value : 3 StaticType : System.Int32 Value : 5 StaticType : System.Int32

In other words, your syntax is wrong for PowerShell array expansions (they can't be chained together), but these aren't PowerShell array literals, they're literal strings passed as parameters to the Get-VM cmdlet, which is (presumably) treating them as wildcards in some way.

Hyper-V Get-VM says it doesn't support wildcard characters.

VMware Get-VM does have wildcards demonstrated in its examples.

From a bit of playing around with PowerCLI, it looks like the .. are being ignored, and [236] means "2, 3 or 6 in this position" and [1-3] means "a digit between 1 and 3 in this position"`. Which is getting the output you want, so in that sense, the syntax is fine. And probably easier than if you had been able to use PS arrays.

更多推荐

本文发布于:2023-07-30 11:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1337891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但它   序列   是否正确   数字   works

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!