Powershell 哈希表:无法将值 "System.Collections.Hashtable" 转换为类型 "Microsoft.PowerShell.Cmdletization.GeneratedTypes"
Powershell Hashtable: Cannot convert value "System.Collections.Hashtable" to type "Microsoft.PowerShell.Cmdletization.GeneratedTypes"
我正在尝试使用以下脚本为新部署的 VM 分配新的 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
|
|
--------------------------------------------------------------------------------------
你的变量应该定义在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
}
我正在尝试使用以下脚本为新部署的 VM 分配新的 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
|
|
--------------------------------------------------------------------------------------
你的变量应该定义在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
}