我没有 VMWare 环境来测试以下内容。
但是,这一切的基本部分在语法上都是错误的。如果您在 ISE/VSCode 中打开它,或者如果您使用 PSCriptAnalyzer 扫描它,它会显示给您。
• 别名的最佳实践
在 PowerShell 脚本中使用别名的最佳实践
https://devblogs.microsoft.com/scripting/best-practice-for-using-aliases-in-powershell-scripts
https://devblogs.microsoft.com/scripting/using-powershell-aliases-best-practices
首先为什么要担心别名?
无论如何,使用别名有什么大不了的?如果他们编写代码
更容易输入,在脚本中使用它们有什么害处?有
当涉及到脚本时,有两件事在起作用。第一个是没有
alias 保证存在——即使是由 Windows 创建的别名
PowerShell。
Invoke-ScriptAnalyzer -Path .\CannotConvert.ps1
RuleName Severity ScriptName Line Message
-------- -------- ---------- ---- -------
MissingArgument ParseError CannotConv 1 Missing argument in parameter list.
ert.ps1
MissingArgument ParseError CannotConv 1 Missing argument in parameter list.
ert.ps1
MissingPropertyName ParseError CannotConv 3 Missing property name after reference operator.
ert.ps1
PSAvoidUsingCmdletAliases Warning CannotConv 5 'foreach' is an alias of 'ForEach-Object'. Alias can
ert.ps1 introduce possible problems and make scripts hard to
maintain. Please consider changing alias to its full
content.
PSUseCompatibleSyntax Warning CannotConv 3 The dynamic member invocation syntax '10.91.91.XX7,
ert.ps1 10.91.91.XX8' is not available by default in PowerShell
versions 3
ISE VSCode 具有自动扩展别名的插件/功能。因此,您可以在您的开发工作中使用它们,但在签入和生产版本中,扩展它们。交互式命令内容和丢弃代码是它们的目标用例。
您不能以这种方式使用哈希表或 ForEach。单独或一起。
关于哈希表用例的真正好文章。
Everything you wanted to know about hashtables
在每个步骤检查您的结果,并确保在进行下一步之前您得到了您期望的结果。使用poor-mans的debug,通过PowerShell变量挤压赋值给一个变量,同时输出到屏幕
你的会立即出错:
($hostVM = [ordered]@{3 = {hostpc-1, hostpc-3, hostpc-5} ; 2 = {hostpc-2,hostpc-4}})
# Results
<#
At line:1 char:36
+ ($hostVM = [ordered]@{3 = {hostpc-1, hostpc-3, hostpc-5} ; 2 = {hostp ...
+ ~
Missing argument in parameter list.
At line:1 char:73
+ ... ordered]@{3 = {hostpc-1, hostpc-3, hostpc-5} ; 2 = {hostpc-2,hostpc-4 ...
+ ~
Missing argument in parameter list.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingArgument
#>
对,这样做:
($hostVM = [ordered]@{
3 = 'hostpc-1, hostpc-3, hostpc-5'
2 = 'hostpc-2,hostpc-4'
})
# Results
<#
Name Value
---- -----
3 hostpc-1, hostpc-3, hostpc-5
2 hostpc-2,hostpc-4
#>
您不能按照您尝试的方式在数组中使用点分字符串。另一篇文章。
Everything you wanted to know about arrays
与上述相同的问题:
($esxarray = @(10.91.91.XX7, 10.91.91.XX8))
# Results
<#
At line:1 char:22
+ ($esxarray = @(10.91.91.XX7, 10.91.91.XX8))
+ ~
Missing property name after reference operator.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingPropertyName
#>
VS 这样做:
($esxarray = @('10.91.91.XX7', '10.91.91.XX8'))
# Results
<#
10.91.91.XX7
10.91.91.XX8
#>
$vmf = 0
$hostVM.GetEnumerator() |
foreach {
Import-VApp -Source $hostpath -Name $_.Value -DiskStorageFormat Thin -VMHost $esxarray[$vmf] -Datastore $storage
$vmf = $vmf+1
}
更新
根据您的评论...
...
如果你是说……
$hostVM.SomeValue
... 那么不,因为它是一个字符串,而不是一个数组。你必须这样做。
((($hostVM.Values)[0]) -split ',')[0]
# Results
<#
hostpc-1
#>
或者在您的代码中,您必须对此进行重构。
($hostVM = [ordered]@{
3 = @('hostpc-1', 'hostpc-3', 'hostpc-5')
2 = @('hostpc-2','hostpc-4')
})
# Results
<#
Name Value
---- -----
3 {hostpc-1, hostpc-3, hostpc-5}
2 {hostpc-2, hostpc-4}
#>
$hostVM.Keys
# Results
<#
3
2
#>
$hostVM.Values
# Results
<#
hostpc-1
hostpc-3
hostpc-5
hostpc-2
hostpc-4
#>
$hostVM[0].GetValue(0)
# Results
<#
hostpc-1
#>
$hostVM[0].GetValue(2)
# Results
<#
hostpc-5
#>