Answer the question
In order to leave comments, you need to log in
What cmdlet to use to remotely change static IP win 2008r2?
It is necessary to automate the process of changing IP addresses. I just started to get acquainted with powershell, help me fix the script so that it would work on Win Server 2008 r2.
$vybor = Read-Host "Выбирете группу компьютеров: 1: 80-81; 2: 82-83; 3: 84-85; 4: 86-87"
#Постоянные переменные
$NewGateway = '10.247.1.254'
$TempIPAddress ='10.247.0.99'
$NewNetMask = '23'
$NewDNS1 = '10.76.0.163'
$NewDNS2 = '10.76.0.64'
#Переменные после выбора
if ($vybor -eq 1)
{
$IPAddress = '10.247.0.80'
$NewIPAddress = '10.247.0.81'
$InetrfaceName1 = 'local area connection 7'
$InetrfaceName2 = 'local area connection 7'
}
if ($vybor -eq 2)
{
$IPAddress = '10.247.0.82'
$NewIPAddress = '10.247.0.83'
$InetrfaceName1 = 'local area connection 7'
$InetrfaceName2 = 'local area connection 7'
}
if ($vybor -eq 3)
{
$IPAddress = '10.247.0.84'
$NewIPAddress = '10.247.0.85'
$InetrfaceName1 = 'local area connection 7'
$InetrfaceName2 = 'local area connection 7'
}
if ($vybor -eq 4)
{
$IPAddress = '10.247.0.86'
$NewIPAddress = '10.247.0.87'
$InetrfaceName1 = 'local area connection 7'
$InetrfaceName2 = 'local area connection 7'
}
Write-Host "Выбраны компьютеры с IP-адресами $IPAddress и $NewIPAddress"
#Скрипт смены адреса
#Проверяем занятость 1 айпи адреса
if (Test-Connection -ComputerName $IPAddress -Count 1 -Quiet)
{
#Если 1 айпи адрес занят, то проверяем занят ли 2 адрес
if (Test-Connection -ComputerName $NewIPAddress -Count 1 -Quiet)
{
Write-Host "Меняем на компьютере $IPAddress IP-адрес интерфейса $InetrfaceName1 с основного адреса $IPAddress на временный адрес $TempIPAddress"
invoke-command -computername $IPAddress {SmenaIPServera ($InetrfaceName, $IPAddress, $TempIPAddress, $NewNetMask, $NewGateway, $NewDNS1, $NewDNS2)}
#Проверяем освободился ли 1 айпи адрес
if (-not (Test-Connection -ComputerName $IPAddress -Count 1 -Quiet))
{
Write-Host "Меняем на компьютере $NewIPAddress IP-адрес интерфейса $InetrfaceName2 с основного адреса $NewIPAddress на адрес $IPAddress"
invoke-command -computername $NewIPAddress {SmenaIPServera ($InetrfaceName2, $NewIPAddress, $IPAddress, $NewNetMask, $NewGateway, $NewDNS1, $NewDNS2)}
}
#Проверяем освободился ли 2 айпи адрес
if (-not (Test-Connection -ComputerName $NewIPAddress -Count 1 -Quiet))
{
Write-Host "Меняем на компьютере $TempIPAddress IP-адрес интерфейса $InetrfaceName1 с временного адреса $TempIPAddress на адрес $NewIPAddress"
invoke-command -computername $TempIPAddress {SmenaIPServera ($InetrfaceName1, $TempIPAddress, $NewIPAddress, $NewNetMask, $NewGateway, $NewDNS1, $NewDNS2)}
}
}
else
{
Write-Host "Недоступен компьютер с IP-адресом $NewIPAddress"
}
}
else
{
Write-Host "Недоступен компьютер с IP-адресом $IPAddress"
}
function SmenaIPServera ($InetrfaceName, $IP2, $IP1, $NewNetMask, $NewGateway, $NewDNS1, $NewDNS2)
{
$NetAdapter = Get-NetAdapter -Name $InetrfaceName
$NetAdapter | New-NetIPAddress -IPAddress $IP1 -PrefixLength $NewNetMask
$NetAdapter | Remove-NetIPAddress -IPAddress $IP2 -PrefixLength $NewNetMask -Confirm:$false
$NetAdapter | Set-NetRoute -NextHop $NewGateway
$NetAdapter | Set-DnsClientServerAddress -ServerAddresses $NewDNS1,$NewDNS2
}
Answer the question
In order to leave comments, you need to log in
$NetAdapter = Get-NetAdapter -Name $InetrfaceName
$NetAdapter | New-NetIPAddress -IPAddress $IP1 -PrefixLength $NewNetMask
$NetAdapter | Remove-NetIPAddress -IPAddress $IP2 -PrefixLength $NewNetMask -Confirm:$false
$NetAdapter | Set-NetRoute -NextHop $NewGateway
$NetAdapter | Set-DnsClientServerAddress -ServerAddresses $NewDNS1,$NewDNS2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question