问题描述
$hostVM = [ordered]@{3 = {hostpc-1,hostpc-3,hostpc-5} ; 2 = {hostpc-2,hostpc-4}}
这是我的代码,用于从$jumpHash
哈希表中读取值,
$esxarray = @(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
}
使用GetEnumerator()方法已枚举$hostvm
中的所有值,但是我无法从哈希表中调用-Name
参数中使用的值,并且还收到以下错误
Import-VApp : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Name'. Specified method is not
supported.
At C:\ONTAP_Hashtable.ps1:65 char:45
+ Import-VApp -Source $hostvmpath -Name $_.Value -diskStorageForm ...
解决方法
我没有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
为什么首先要担心别名?
无论如何,使用别名有什么大不了的?如果他们编写代码 更易于输入,在脚本中使用它们有什么害处?有 关于脚本,有两件事在起作用。首先是没有 保证别名存在-甚至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。单独或一起。
有关哈希表用例的真实文章。
在每个步骤中检查结果,并确保在获得所需的结果之前进行下一步。使用穷人调试,通过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-5} ; 2 = {hostp ...
+ ~
Missing argument in parameter list.
At line:1 char:73
+ ... ordered]@{3 = {hostpc-1,hostpc-4 ...
+ ~
Missing argument in parameter list.
+ CategoryInfo : ParserError: (:) [],ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingArgument
#>
Vs,这样做:
($hostVM = [ordered]@{
3 = 'hostpc-1,hostpc-5'
2 = 'hostpc-2,hostpc-4'
})
# Results
<#
Name Value
---- -----
3 hostpc-1,hostpc-5
2 hostpc-2,hostpc-4
#>
您不能以尝试的方式在数组中使用点分字符串。另一篇文章。
与上述问题相同:
($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
#>
这样做:
($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-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
#>