Created
February 28, 2012 08:41
-
-
Save breezhang/1931069 to your computer and use it in GitHub Desktop.
set DNS just ipv4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Summary: Microsoft Scripting Guy, Ed Wilson, | |
#talks about using Windows PowerShell to set the static IP and DNS addresses on a server. | |
#Microsoft Scripting Guy, Ed Wilson, is here. | |
#One of the really cool things about computers is that you never get bored. | |
#At least for me this is true. | |
#for example, I have a server that has been running Exchange Server | |
#absolutely perfectly for more than a year. | |
#Today, it acted like a 150 pound St. Bernard that had become bored. | |
#That’s right, it threw a fit. Why did it do so? Well, I had changed the IP network configuration, | |
#and I did not change the IP address on this machine. for some reason, | |
#the IP changes caused a race condition in Exchange Server, and I could hardly get control of the box. | |
#I logged on to the computer, | |
#but I was unable to use the graphical tools to set a new IP address on the box—things would spin in circles, | |
#and then disappear. Dude, what now? | |
#Well, I thought I would run my Set-StaticIPAddress script on the machine to set the address, | |
#but there were two problems: | |
#The script execution policy in my Exchange Server does not permit the running of scripts. | |
#The server is completely isolated and cannot contact my script share. | |
#So what can I do? Well, I opened the Windows PowerShell ISE and | |
#typed the commands that follow into the script pane. | |
#SetStaticIP.ps1 | |
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" | |
$wmi.EnableStatic("10.0.0.15", "255.255.255.0") | |
$wmi.SetGateways("10.0.0.1", 1) | |
$wmi.SetDNSServerSearchOrder("10.0.0.100") | |
#This is the cool part, even when the Windows PowerShell script execution policy it set to Restricted, which disallows the execution of scripts, it is still possible to open the Windows PowerShell ISE and run commands. This technique allowed me to type the four previous commands, and to execute them all at once. | |
#What do the four previous commands do? | |
#The first command uses the Get-WmiObject cmdlet to retrieve the network adapter configuration for all the network adapters that are enabled for use with IP. To do this, I use the Win32_NetworkAdapterConfiguration WMI class. This class has a number of extremely useful methods—that is, it can do a lot! I store the resulting object in a variable I called $wmi. This line of code is shown here. | |
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" | |
#When the network adapter configuration object is stored in the $wmi variable, it is easy to use the following three methods: EnableStatic, SetGateways, and SetDnsServerSearchOrder. All that is required is to supply the required values. The EnableStatic method requires the static IP address in addition to a subnet mask. Each of these values are strings. The SetGateways method also requires two strings. The first parameter is the IP address of the gateway, and the second parameter is the metric. The last method I used is the SetDnsServerSearchOrder method. This code is shown here. | |
$wmi.EnableStatic("10.0.0.15", "255.255.255.0") | |
$wmi.SetGateways("10.0.0.1", 1) | |
$wmi.SetDNSServerSearchOrder("10.0.0.100") | |
#The figure that is shown here illustrates the four lines of code, and the output from those commands. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment