This week my blog post will be about setting a static IP address during a deployment. I’m going to do this via a combination of a PowerShell script, a computer variable and a task sequence. I know it is possible in the Task Sequence Wizard to supply static IP information, but there are two reason why I did not use that solution:
- I’ve seen a lot of situations where the IP configuration got applied after the policy requests were done to the Management Point. This would cause the task sequence to fail.
- I wanted a zero touch experience for the persons performing the deployment.
PowerShell Script
Let’s start with the most important part of my solution, a PowerShell script. This script sets a static IP address with, a subnet, a gateway and two DNS servers. It needs an IP address as input and needs to be adjusted per situation, as I hardcoded my subnet, gateway and DNS servers in the call of the function. It can be easily adjusted to need more input variables for a subnet, a gateway and two DNS servers. The variable names tell the story of which number is needed when.
param ( [string]$strIPAddress ) function Set-StaticIPAddress ($strIPAddress, $strSubnet, $strGateway, $strDNSServer1, $strDNSServer2) { $NetworkConfig = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IpEnabled = 'True'" $NetworkConfig.EnableStatic($strIPAddress, $strSubnet) $NetworkConfig.SetGateways($strGateway, 1) $NetworkConfig.SetDNSServerSearchOrder(@($strDNSServer1, $strDNSServer2)) } Set-StaticIPAddress $strIPAddress "255.255.255.0" "192.168.137.1" "192.168.137.100" "192.168.137.101"
Task Sequence
Now add the PowerShell script to an old-school Package, so it will be available for a task sequence. Then create a standard Install an existing image package task sequence. Now edit the task sequence and make sure the following step is included:
- Add a step Run PowerShell Script with the following settings:
- Package: <NameOfPackageThatContainsStaticIPScript>
- Script name: <NameOfTheStaticIPScript>
- Parameters: %IPAddress%
- Note: IPAddress is the computer variable that I use to set the IP address for a device.
- PowerShell execution policy: Bypass
Computer Variable
Just a small note about the specified computer variable. There are multiple way’s of adding this variable. One way is creating the variable by hand via the nice sun/star-shaped button, another way is creating a small addition for my Import Computer Information Form.
Hello Peter
How can I use the script when I have multiple NICs?
Hi Tim,
That’s hard to say. In the simplest case you could turn it in a foreach loop to run through your NICs.
Peter
Is it possible to only set static PRIMARY and SECONDARY DNS?
Sure, but that would require some adjustments to the script.
Hi Peter,
is it also possible to extend the script with multiple variables? Now you run the PowerShell script with one variable (%IPAddress%).
It should be nice if we can also put the gateway and DNS servers into variables.
Regards,
Mark
Hi Mark,
Yes, you can extend the script to use more input variables. That should be a minor adjustment.
Peter