0

I have to add text from one file to the end of another file, but the contents of one file is significantly shorter than the other, so I want to keep looping through the shorter file until the longer file is complete. I generate the intial file (scopeout.txt) with the following code

$a = "NETSH Dhcp Server 192.168.10.1 Scope "
$b = " set optionvalue 6 IPADDRESS "
$Scopelist = Get-Content C:\ListOfScopes.txt

foreach ($i in $ListOfScopes)

{
$a + $i + $b >> C:\TestArea\scopeout.txt
}

I then have a file called newdns.txt that contains 4 entries (192.168.0.10, 192.168.0.11, 192.168.0.12, 192.168.0.13). I want to read each line of scopeout.txt, add 1 entry from newdns.txt, then move onto the next line in each file. I want to repeat this process until scopeout.txt is finished, so the final file will look similar to the following:

NETSH Dhcp Server 192.168.10.1 Scope 192.168.50.1 set optionvalue 6 IPADDRESS 192.168.0.10
NETSH Dhcp Server 192.168.10.1 Scope 192.168.51.0 set optionvalue 6 IPADDRESS 192.168.0.11
NETSH Dhcp Server 192.168.10.1 Scope 192.168.52.0 set optionvalue 6 IPADDRESS 192.168.0.12
NETSH Dhcp Server 192.168.10.1 Scope 192.168.53.0 set optionvalue 6 IPADDRESS 192.168.0.13
NETSH Dhcp Server 192.168.10.1 Scope 192.168.54.0 set optionvalue 6 IPADDRESS 192.168.0.10
NETSH Dhcp Server 192.168.10.1 Scope 192.168.55.0 set optionvalue 6 IPADDRESS 192.168.0.11
NETSH Dhcp Server 192.168.10.1 Scope 192.168.56.0 set optionvalue 6 IPADDRESS 192.168.0.12
NETSH Dhcp Server 192.168.10.1 Scope 192.168.57.0 set optionvalue 6 IPADDRESS 192.168.0.13
NETSH Dhcp Server 192.168.10.1 Scope 192.168.58.0 set optionvalue 6 IPADDRESS 192.168.0.10
NETSH Dhcp Server 192.168.10.1 scope 192.168.59.0 set optionvalue 6 IPADDRESS 192.168.0.11

I have experimented with using a foreach statement, but I am unsure how to combine 2 foreach statements and also how to keep looping through the newdns.txt until scopeout.txt is complete.

1 Answer 1

2

Try this:

$c = 0
$ip = Get-Content C:\newdns.txt

Get-Content C:\ListOfScopes.txt | Foreach-Object{
    if ($c -gt 3) {$c=0} 
    "NETSH Dhcp Server 192.168.10.1 Scope $_ set optionvalue 6 IPADDRESS $($ip[$c])"
    $c++
}
Sign up to request clarification or add additional context in comments.

1 Comment

That is exactly what I needed, worked perfectly. Thank you very much for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.