PoshCode Archive  Artifact Content

Artifact 7d85b9e382fcbe36a488e20a547730b3d846022529c30c479860ddcabfaa5817:

  • File Set-custom-IP-address.ps1 — part of check-in [8a7a3dd851] at 2018-06-10 14:19:29 on branch trunk — Set custom IP address (user: Yves Rosius size: 2729)

# encoding: ascii
# api: powershell
# title: Set custom IP address
# description: Set custom IP address
# version: 0.1
# type: function
# author: Yves Rosius
# license: CC0
# function: Set-CustomIP
# x-poshcode-id: 6700
# x-archived: 2017-02-09T13:59:00
# x-published: 2017-01-18T18:30:00
#
#
function Set-CustomIP {

        <#
    	.SYNOPSIS
    	Set network settings.
    	.DESCRIPTION
    	Set network settings.
    	.EXAMPLE
    	Set-CustomIP 192.168.1.123
    	.PARAMETER IPAddress
    	Preferred IP Address. DHCP is available to set IP with DHCP.
    	.PARAMETER DefaultGateway
    	Preferred Default Gateway. If no value is provided, a value is calculated.
	.PARAMETER PrimaryDNSServer
	Primary DNS server. Defaults to 8.8.8.8
	.PARAMETER SecondaryDNSServer
	Secondary DNS server. Defaults to 8.8.4.4
	.PARAMETER AddressFamily
	Currently only available in IPv4
	.PARAMETER InterfaceIndex
	Specify which interface using InterfaceIndex. Defaults to physical interface.
    	#>
	
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True, Position=1)]
        [string] $IPAddress,

        [Parameter(Mandatory=$False, Position=2)]
        [string] $DefaultGateway,

        [Parameter(Mandatory=$False, Position=3)]
        [string] $PrimaryDNSServer = "8.8.8.8",

        [Parameter(Mandatory=$False, Position=4)]
        [string] $SecondaryDNSServer = "8.8.4.4",
		
	[Parameter(Mandatory=$False, Position=5)]
        [string] $AddressFamily = "IPv4",
		
	[Parameter(Mandatory=$False, Position=6)]
        [string] $PrefixLength = "24",
		
	[Parameter(Mandatory=$False, Position=7)]
        [string] $InterfaceIndex = (Get-NetAdapter -physical).ifIndex
    )
	
	### Removes current IP settings to avoid clutter.
	Get-NetIPAddress -InterfaceIndex $InterfaceIndex -AddressFamily $AddressFamily | % {Remove-NetIPAddress $_.IPAddress -Confirm:$false} 
	Remove-NetRoute -InterfaceIndex $InterfaceIndex -Confirm:$false
	
	if ($IPAddress -eq "DHCP") {
		Get-NetAdapter -InterfaceIndex $InterfaceIndex | Set-NetIPInterface -Dhcp enabled
		break
		}
	
	### If DefaultGateway not specified, calculate standard value for Default Gateway.
	if (!$DefaultGateway) {
		$IPArray = $IPAddress.split(".")
		$DefaultGateway = $IPArray[0] + "." + $IPArray[1] + "." + $IPArray[2] + ".1"
		}
	
    	New-NetIPAddress `
		-InterfaceIndex $InterfaceIndex `
		-IPAddress $IPAddress `
		-DefaultGateway $DefaultGateway `
		-AddressFamily $AddressFamily `
		-PrefixLength $PrefixLength `
        	-Confirm:$false
		
    	Set-DnsClientServerAddress `
		-InterfaceIndex $InterfaceIndex `
		-ServerAddresses $PrimaryDNSServer, $SecondaryDNSServer

    }