# encoding: ascii
# api: powershell
# title: Set-IPAddress
# description: A function to set an IP Address
# version: 0.1
# type: script
# author: Shane Powser
# license: CC0
# function: Set-IPAddress
# x-poshcode-id: 1806
# x-archived: 2010-05-01T15:42:39
#
# Andy Schneider
# Get-PowerShell.com
#
# ahhh ... much better. Don't ask for prompts. It's not scriptable if you do.
function Set-IPAddress {
param( [string]$networkinterface,
[string]$ip,
[string]$mask,
[string]$gateway,
[string]$dns1,
[string]$dns2,
[string]$registerDns=$true
)
$dns = $dns1
if($dns2){$dns ="$dns1","$dns2"}
$index = (gwmi Win32_NetworkAdapter | where {$_.netconnectionid -eq $networkinterface}).InterfaceIndex
$NetInterface = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.InterfaceIndex -eq $index}
$NetInterface.EnableStatic($ip, $subnetmask)
$NetInterface.SetGateways($gateway)
$NetInterface.SetDNSServerSearchOrder($dns)
$NetInterface.SetDynamicDNSRegistration($registerDns)
}