【问题标题】:Powershell Hashtable: Cannot convert value "System.Collections.Hashtable" to type "Microsoft.PowerShell.Cmdletization.GeneratedTypes"Powershell Hashtable:无法将值“System.Collections.Hashtable”转换为类型“Microsoft.PowerShell.Cmdletization.GeneratedTypes”
【发布时间】:2020-11-27 14:24:59
【问题描述】:

我正在尝试使用以下脚本为新部署的虚拟机分配新的 IP 地址

    $newVmList = @(
        @{"Name" = "TESTVM21"; "NetworkName" = "LS_10.123.16.0/22"; "IPAddress" = "10.123.19.250"; "PrefixLength" = "22"; "DefaultGateway" = "10.123.16.1"; "IfAlias" = "Ethernet0"; "IPFamily" = "IPv4"; "DNS" = "10.163.4.14, 10.163.4.15";}
        @{"Name" = "TESTVM31"; "NetworkName" = "LS_10.123.16.0/22"; "IPAddress" = "10.123.19.251"; "PrefixLength" = "22"; "DefaultGateway" = "10.123.16.1"; "IfAlias" = "Ethernet0"; "IPFamily" = "IPv4"; "DNS" = "10.163.4.14, 10.163.4.15";}
    )
    $setIP = "New-NetIPAddress –InterfaceAlias ""$vm.IfAlias"" -AddressFamily ""$vm.IPFamily"" -IPAddress ""$vm.IPAddress"" -PrefixLength ""$vm.PrefixLength"" -DefaultGateway ""$vm.DefaultGateway"""
    $setDNS = "Set-DnsClientServerAddress -InterfaceAlias ""$vm.IfAlias"" -ServerAddresses ""$vm.DNS"""
    
    Foreach($VM in $newVmList) { 
        Invoke-VMScript -VM $VM.Name -GuestCredential $GuestAccount -ScriptText $setIP
        Invoke-VMScript -VM $VM.Name -GuestCredential $GuestAccount -ScriptText $setDNS
    }

但我的脚本使用字符串作为参数的输入类型似乎存在问题。我查看了几个线程,但我无法使用提供的解决方案修改我的脚本。有人可以帮我解决吗?

ScriptOutput
--------------------------------------------------------------------------------------
|  New-NetIPAddress : Cannot process argument transformation on parameter
|  'AddressFamily'. Cannot convert value "System.Collections.Hashtable.IPFamily"
|  to type "Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.Address
|  Family". Error: "Unable to match the identifier name
|  System.Collections.Hashtable.IPFamily to a valid enumerator name.  Specify one
|  of the following enumerator names and try again: IPv4, IPv6"
|  At line:1 char:91
|  + ... -AddressFamily "System.Collections.Hashtable.IPFamily" -IPAddress
|  "System.Collec ...
|  +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|      + CategoryInfo          : InvalidData: (:) [New-NetIPAddress], ParameterBi
|     ndingArgumentTransformationException
|      + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-NetIPAd
|     dress
|
|
--------------------------------------------------------------------------------------
ScriptOutput
--------------------------------------------------------------------------------------
|  Set-DnsClientServerAddress : No MSFT_DNSClientServerAddress objects found with
|  property 'InterfaceAlias' equal to 'System.Collections.Hashtable.IfAlias'.
|  Verify the value of the property and retry.
|  At line:1 char:4
|  + & {Set-DnsClientServerAddress -InterfaceAlias
|  "System.Collections.Hashtable.IfAl ...
|  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|  ~~~
|      + CategoryInfo          : ObjectNotFound: (System.Collections.Hashtable.If
|     Alias:String) [Set-DnsClientServerAddress], CimJobException
|      + FullyQualifiedErrorId : CmdletizationQuery_NotFound_InterfaceAlias,Set-D
|     nsClientServerAddress
|
|
--------------------------------------------------------------------------------------

【问题讨论】:

  • 我已将 $setIP$setDNS 移到 Foreach 块内,但这并没有什么不同。我怀疑与此有关 - Error: "Unable to match the identifier name System.Collections.Hashtable.IPFamily to a valid enumerator name. Specify one of the following enumerator names and try again: IPv4, IPv6" 尽管$VM.IPFamily 有一个价值。
  • 谢谢@CFou。你的语法有效。我不知道如何将您的评论标记为Answer
  • @CFou:请发布与答案相同的内容.. 并让 joso 将其标记为已接受。
  • 完成 :),你现在可以标记它了 :) 很高兴它有帮助

标签: powershell hashtable lookup-tables


【解决方案1】:

您的变量应该在ForEach 块内定义($vm 之前不存在),而不是之前。将您的 $setIP$setDNS 声明移动到第一个 Invoke-VMSCript 之前。

并使用以下语法:

$setIP = "New-NetIPAddress –InterfaceAlias $($vm.IfAlias) -AddressFamily $($vm.IPFamily) -IPAddress $($vm.IPAddress) -PrefixLength $($vm.PrefixLength) -DefaultGateway $($vm.DefaultGateway)" 
$setDNS = "Set-DnsClientServerAddress -InterfaceAlias $($vm.IfAlias) -ServerAddresses $($vm.DNS)"

所以,你应该有这个:

$newVmList = @(
   @{"Name" = "TESTVM21"; "NetworkName" = "LS_10.123.16.0/22"; "IPAddress" = "10.123.19.250"; "PrefixLength" = "22"; "DefaultGateway" = "10.123.16.1"; "IfAlias" = "Ethernet0"; "IPFamily" = "IPv4"; "DNS" = "10.163.4.14, 10.163.4.15";}
   @{"Name" = "TESTVM31"; "NetworkName" = "LS_10.123.16.0/22"; "IPAddress" = "10.123.19.251"; "PrefixLength" = "22"; "DefaultGateway" = "10.123.16.1"; "IfAlias" = "Ethernet0"; "IPFamily" = "IPv4"; "DNS" = "10.163.4.14, 10.163.4.15";}
)
    
Foreach($VM in $newVmList) { 
   $setIP = "New-NetIPAddress –InterfaceAlias $($vm.IfAlias) -AddressFamily $($vm.IPFamily) -IPAddress $($vm.IPAddress) -PrefixLength $($vm.PrefixLength) -DefaultGateway $($vm.DefaultGateway)" 
   $setDNS = "Set-DnsClientServerAddress -InterfaceAlias $($vm.IfAlias) -ServerAddresses $($vm.DNS)"
   Invoke-VMScript -VM $VM.Name -GuestCredential $GuestAccount -ScriptText $setIP
   Invoke-VMScript -VM $VM.Name -GuestCredential $GuestAccount -ScriptText $setDNS
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-13
    • 2015-06-12
    • 2018-06-17
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多