【问题标题】:Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Name'无法将“System.Object []”转换为参数“名称”所需的类型“System.String”
【发布时间】:2020-10-09 04:27:02
【问题描述】:

我有一个包含如下键和值的哈希表,

$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 ...

请提出一种调用这些值的方法。提前致谢

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0 powershell-4.0


    【解决方案1】:

    我没有 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
    #>
    

    【讨论】:

    • 我能否分别读取键 3 和键 2 的值?比如'hostpc-1'、'hostpc-3'、'hostpc-5'
    • 查看我的更新。然而,根据你发布的内容,你让我去假设一些东西,因为你不清楚你想用那个哈希表做什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 2021-06-08
    相关资源
    最近更新 更多