Thursday, August 7, 2014

PowerShell - Remote Configure Static/DHCP IP Address

For Static IP:
$sc = {
param($IP)
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
$wmi.EnableStatic($IP, "255.255.255.0");
$wmi.SetGateways("192.168.1.1", 1);
$wmi.SetDNSServerSearchOrder(@("192.168.1.1","8.8.8.8"))
}

$IP = "192.168.1.101"
$job = Invoke-Command 192.168.1.121 -ScriptBlock $sc -ArgumentList $IP -AsJob -Credential $cred
Wait-Job $job -Timeout 20
$IP = "192.168.1.102"
$job = Invoke-Command 192.168.1.122 -ScriptBlock $sc -ArgumentList $IP -AsJob -Credential $cred
Wait-Job $job -Timeout 20



For DHCP IP:
$sc = {
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
$wmi.EnableDHCP();
$wmi.SetDNSServerSearchOrder();
}

$job = Invoke-Command 192.168.1.101 -ScriptBlock $sc -AsJob -Credential $cred
Wait-Job $job -Timeout 20
$job = Invoke-Command 192.168.1.102 -ScriptBlock $sc -AsJob -Credential $cred
Wait-Job $job -Timeout 20


No comments:

Post a Comment