############################################################################
#Author : Richie Schuster C5 - SCCMOG.com
#ModdedBy : Your Name here....
#Date : 23/06/2017
#Script : Set-MachineNetworkConfiguration.ps1
#Usage : Set-MachineNetworkConfiguration.ps1 -IPAdress %IPAddress% -Subnet %SubMask% -Gateway %GateWay%
# : -DNS1 %DNS1% -DNS2 %DNS2%
# : There is also a Switch -RevDNSAuto that allows for the DNS to be auto set on revert or not.
#Info : This script has been created to auto configure Network information during OSD using OSD Variables
# : or using strings.
############################################################################
param (
[string]$IPAddress,
[string]$Subnet,
[string]$Gateway,
[string]$DNS1,
[string]$DNS2,
[string]$PrimarySiteServer,
[switch]$RevDNSAuto
)
<##Testing Variables
[string]$IPAddress = "192.82.33.149"
[string]$Subnet = "255.255.255.0"
[string]$Gateway = "192.82.33.1"
[string]$DNS1 = "192.82.33.244"
[string]$DNS2 = "192.82.34.244"
[string]$PrimarySiteServer = "192.82.33.11"
[switch]$RevDNSAuto = $false
#Variables
$REVDNS1 = "192.82.1.22"
$REVDNS2 = "192.82.1.22"
#Create Task Sequence Variable Play!
$TSENV = New-Object -COMObject Microsoft.SMS.TSEnvironment
#Function to set IP Address - Credit "Petervanderwoude.nl"
function Set-StaticIPAddress ($IPAddress, $Subnet, $Gateway, $DNS1, $DNS2) {
$NewNetworkConfig = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IpEnabled = 'True'"
$NewNetworkConfig.EnableStatic($IPAddress, $Subnet)
$NewNetworkConfig.SetGateways($Gateway, 1)
$NewNetworkConfig.SetDNSServerSearchOrder(@($DNS1, $DNS2))
}
#Set new Configuration
Set-StaticIPAddress $IPAddress $Subnet $Gateway $DNS1 $DNS2
#Sleep to take wait effect
sleep -Seconds 10
#Test Connection
If (Test-Connection -ComputerName $PrimarySiteServer -Count 10) {
#If connection succeded...
Write-Host "Configuration Succeeded!" -ForegroundColor Green
#Set Task seqeunce Variable to False
$TSENV.Value("NetworkConfig") = "Static"
#Exit 0
}
Else {
#If connection failed...
Write-Host "Configuration Failed! Setting back to DHCP!" -ForegroundColor Yellow
#Set Task seqeunce Variable to False
$TSENV.Value("NetworkConfig") = "DHCP"
#Get current IP settings swap to DHCP
$CurrentNetworkConfig = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'"
#Set DNS Servers check switch
If ($RevDNSAuto -ne $true) {
#Set DNS to specified variable values
$CurrentNetworkConfig.SetDNSServerSearchOrder(@($REVDNS1,$REVDNS2))
}
Else {
#Set DNS to Auto
$CurrentNetworkConfig.SetDNSServerSearchOrder()
}
#Enable DHCP
$CurrentNetworkConfig.EnableDHCP()
#Wait for configuration to complete
Sleep -Seconds 10
#Exit 0
}
######################################################################################