我需要将虚拟硬盘添加到多个虚拟机中,所以我最终使用它来执行此操作:
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 10This 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.comThat 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中的着色中看到:
如果您通过解析器运行它并查看它是如何被读取的,您也可以看到它:
{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:
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.Stringvs.
{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.Int32In 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.
更多推荐
发布评论