How to stop change ip powershell script from looping

Viewed 48

Hi all and thank you for all the help. I am still fairly new to powershell scripting so the answer might be simple but can't find it. I have the below script but after changing the ip of the first computer it loops and doesnt proceed to the next steps. Any ideas?

$ComputerName = Import-Csv "C:\Users\User\Desktop\1.txt" -Header name,IP
$MaskBits = 27 
$Gateway = "172.24.80.193"
$Dns = "172.25.193.32"
$IPType = "IPv4"
foreach ($Computer in $ComputerName) {
# Retrieve the network adapter that you want to configure
$adapter = Get-NetAdapter -cimSession $Computer.name | ? {$_.Status -eq "up"}
# Remove any existing IP, gateway from our ipv4 adapter
If (($adapter | Get-NetIPConfiguration -cimSession $Computer.name).IPv4Address.IPAddress) {
 $adapter | Remove-NetIPAddress -cimSession $Computer.name -AddressFamily $IPType -Confirm:$false
}
If (($adapter | Get-NetIPConfiguration -cimSession $Computer.name).Ipv4DefaultGateway) {
 $adapter | Remove-NetRoute -cimSession $Computer.name -AddressFamily $IPType -Confirm:$false
}
 # Configure the IP address and default gateway
$adapter | New-NetIPAddress `
 -AddressFamily $IPType `
 -IPAddress Computer.$IP `
 -PrefixLength $MaskBits `
 -DefaultGateway $Gateway
# Configure the DNS client server IP addresses
$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS
}

0 Answers
Related