Posted On October 8, 2019

PowerShell: Change IP of Remote Windows

kimconnect 2 comments
blog.KimConnect.com >> Codes >> PowerShell: Change IP of Remote Windows
# Change-IP-Remote-Windows.ps1

$oldIP="192.168.40.135"
$newIP="192.168.40.136"
$netmask="255.255.255.0"
$gateway="192.168.40.1"
$dnsServers=@("10.10.8.1","10.10.18.1")

function changeIpRemoteComputer{
Param (
[string]$oldIP,
[string]$newIP,
[string]$newSubnetMask = "255.255.255.0",
[string]$newDefaultGateway,
[array]$newDNS = @("8.8.8.8","4.4.2.2")
)

$remoteComputerName=[System.Net.Dns]::GetHostEntry($oldIP).HostName
<# PowerShell 4.0
$wmi = Get-WmiObject -ComputerName $oldIP Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -eq $oldIP }
New-NetIPAddress -InterfaceIndex $wmi.Index - IPAddress $newIP -PrefixLength $newSubnetMask -DefaultGateway $newDefaultGateway
Register-DnsClient;
#>

# Powershell 2.0
function changeIP{
Param (
$oldIP,
$newIP,
$newSubnetMask,
$newDefaultGateway,
$newDNS
)
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" | Where-Object { $_.IpAddress -eq $oldIP }
$wmi.EnableStatic($newIP, $newSubnetMask)
$wmi.SetGateways($newDefaultGateway, 1)
$wmi.SetDNSServerSearchOrder($newDNS)

ipconfig /registerdns;
$validatedComputername=[System.Net.Dns]::GetHostEntry($newIP).HostName;
Write-Host "$validatedComputername now has the ip address of $newIP";
}

Invoke-Command -computerName $remoteComputerName -ScriptBlock{
param($importedFunc,$a,$b,$c,$d,$e);
[ScriptBlock]::Create($importedFunc).Invoke($a,$b,$c,$d,$e);
} -Args ${function:changeIP},$oldIP,$newIP,$newSubnetMask,$newDefaultGateway,$newDNS
}
changeIpRemoteComputer -oldIP $oldIP -newIP $newIP -newSubnetMask $netmask -newDefaultGateway $gateway -newDNS $dnsServers

Sample Output

PS C:\Windows\system32> changeIpRemoteComputer -oldIP $oldIP -newIP $newIP -newSubnetMask $netmask -newDefaultGateway $g
ateway -newDNS $dnsServers
WARNING: The network connection to komputer.something.local has been interrupted. Attempting to reconnect for up to 4
minutes...
WARNING: Attempting to reconnect to komputer.something.local ...
WARNING: The network connection to komputer.something.local has been restored.
komputer.something.local now has the ip address of 192.168.40.135


PSComputerName : komputer.something.local
RunspaceId : e0593b1f-9419-4f78-8a2c-45eb4b47d7de
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0

PSComputerName : komputer.something.local
RunspaceId : e0593b1f-9419-4f78-8a2c-45eb4b47d7de
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0

PSComputerName : komputer.something.local
RunspaceId : e0593b1f-9419-4f78-8a2c-45eb4b47d7de
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0


Windows IP Configuration

Registration of the DNS resource records for all adapters of this computer has been initiated. Any errors will be report
ed in the Event Viewer in 15 minutes.

2 thoughts on “PowerShell: Change IP of Remote Windows”

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Practical Usage of Robocopy

Quick Commands: $source='C:\vssSnapshot_F'$destination='\\DESTINATIONSERVER\F$'robocopy $source $destination /E /R:0 /NP /XD '$RECYCLE.BIN' 'System Volume Information' /xf 'pagefile.sys'…

Simple Active Directory & DNS Synchronization Script

@echo offGOTO push_or_pull:push_or_pullset /p action="Type in Push, Pull, or Quit. Press Enter for default action…

Renew or Replace a SSL Certificate in Dynamics CRM

Error Message: "Exchange Online Security Certificate Expiration Please update your certificate or Exchange Online integration…
Other project: DragonCoin.com