【发布时间】: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