Bulk IP configuration using CSV input

半腔热情 提交于 2020-04-30 12:13:43

问题


I have a CSV file with the following values for each machine I want to remotely reconfigure using static IP

name,nic,ip,mask,defaultgw

I was hoping to be able to reconfigure the IPs for each listed but if I have more than one machine listed the script gets stuck. This is because at the end of the first loop iteration, unless I manually do an ipconfig /flushdns on the server the script is running from, I will lose connection to the server being configured and the script just hangs leaving the rest of the servers. What I have so far is this:

$csv = import-csv "c:\scripts\builds\machines.csv"

foreach ($Row in $csv) {
    $machine = $Row.name
    $Nic = $row.Nic
    $address = $row.IP
    $mask =$row.mask
    $defaultgw = $row.gw

    invoke-command -computername $machine -scriptblock { Get-NetIpAddress - InterfaceAlias $using:nic | New-NetIPAddress -ipaddress $using:address -PrefixLength $using:mask -DefaultGateway $using:defaultgw | Register-DnsClient}}
}

Can this be done using workflows or just simple start-job?


回答1:


I suspect you're losing the connection to the remote machine once you change the IP address, while the local machine hangs trying to keep the connection.

Try making the call, and dropping off a payload, then running it after you disconnect.

That is, upload the code, then spawn it, then disconnect before it has a chance to run (add a sleep to the remote code maybe?). This way, you can launch your payload and disconnect before it affects you.

For example, you could copy a file to that machine with the values it needs, then schedule a task to run in 5 seconds in the future, then disconnect before it runs. The task will then run, and since you're already disconnected, you won't be affected by it.

You might also consider DHCP with static reservations. It's far easier to manage than what you're trying to do here.



来源:https://stackoverflow.com/questions/55126976/bulk-ip-configuration-using-csv-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!