通过 powershell 设置网络未按预期工作

问题描述 投票:0回答:0

CSV

我正在尝试从 CSV 中提取信息并使用 invoke-vmscript 更改特定网络适配器上的 ip 地址(按接口索引过滤)。

但是我收到以下错误

New-NetIPAddress:无法将参数绑定到参数“IPAddress” 因为它是一个空字符串。

Set-DnsClientServerAddress:无法验证参数的参数 '接口索引'。参数为空。提供一个 |参数的有效值,然后再次尝试运行该命令。

enter image description here

我有一个 csv 文件,标题如上

`$vms = Import-CSV "C:\\Scripts\\servers.csv"
$GC = $Host.UI.PromptForCredential("Please enter credentials", "Enter       Guest credentials for VM",   "xxxx", "")

foreach ($vm in $vms){
$VMName = $vm.Name
 $IP = $vm.IP
$SNM = $vm.SubnetMask
$GW = $vm.Gateway
 $DNS1 = $vm.DNS1
$DNS2 = $vm.DNS2
$adapter = Invoke-VMScript -VM $VMname -ScriptType Powershell -ScriptText '(Get-NetAdapter -InterfaceDescription Vm\*).ifindex' -GuestUser xxx -GuestPassword xxx
Write-Host "Setting IP address for $VMname..." -ForegroundColor Yellow
Sleep -Seconds 60
$ip1 = Invoke-VMScript -VM $VMname -ScriptType Powershell -ScriptText '(New-NetIPAddress -InterfaceIndex         $adapter -IPAddress $IP -PrefixLength 24 -DefaultGateway $GW)'
$ip2 = Invoke-VMScript -VM $VMname -ScriptType Powershell -ScriptText '(Set-DnsClientServerAddress -InterfaceIndex $adapter -ServerAddresses ("$DNS1","$DNS2"))'
Set-DnsClientServerAddress -InterfaceIndex $adapter -ServerAddresses ("$DNS1","$DNS2")
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType Powershell -ScriptText $ip1
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType Powershell -ScriptText $ip2
Write-Host "Setting IP address completed." -ForegroundColor Green
Sleep -Seconds 20
(\[WMIClass\]"\\$vmname\\ROOT\\CImv2:Win32_Process").Create("cmd.exe /c ipconfig /registerdns")
}`

如果我运行变量 $ip,我会得到返回给我的 ip

另外,如果我直接在主机上运行这些命令,它也可以正常工作

connect-viserver xxx
$vms = Import-CSV "\\in1grascm001\Source_Files\New-Nicservers.csv"
$adapter = (get-netadapter -InterfaceDescription Vm*).IfIndex
foreach ($vm in $vms){
$IP = $vm.ip
$GW = $vm.Gateway
New-NetIPAddress -InterfaceIndex $adapter -IPAddress $ip -PrefixLength 24 -DefaultGateway $vm.Gateway
Set-DnsClientServerAddress -InterfaceIndex $adapter -ServerAddresses ("x.x.x.x","x.x.x.x")
Sleep -Seconds 20
([WMIClass]"\\$vmname\ROOT\CImv2:Win32_Process").Create("cmd.exe /c ipconfig /registerdns")
}
powershell powercli
© www.soinside.com 2019 - 2024. All rights reserved.