使用CSV输入的批量IP配置 [英] Bulk IP configuration using CSV input

查看:146
本文介绍了使用CSV输入的批量IP配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于要使用静态IP进行远程重新配置的每台计算机,我都有一个CSV文件,其中包含以下值

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

我希望能够为列出的每个IP重新配置IP,但是如果列出的计算机不止一台,则脚本会卡住.这是因为,在第一次循环迭代结束时,除非我在运行脚本的服务器上手动执行ipconfig /flushdns,否则我将失去与正在配置的服务器的连接,并且脚本将挂起,而其余服务器将被挂起.到目前为止,我的情况是这样:

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?

推荐答案

我怀疑一旦更改IP地址,您就失去了与远程计算机的连接,而本地计算机挂起并试图保持连接.

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.

例如,您可以将具有所需值的文件复制到该计算机上,然后安排任务在未来5秒钟内运行,然后在运行之前断开连接.该任务将运行,并且由于您已经断开连接,因此不会受到影响.

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.

您还可以考虑使用具有静态保留的DHCP.比您在此尝试要做的事情要容易得多.

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

这篇关于使用CSV输入的批量IP配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆