Set Static IP Remotely

Because I rule, I decided to be able to change people’s IP addresses remotely. This usually requires a Domain Administrator account (which I have/am). Here’s the code for my remote script:

intValue = InputBox("Please enter a computer name:")
intValue2 = InputBox("Please enter an IP address:")
strComputer = intValue
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strGateway = Array("10.1.3.1")
strIPAddress = Array(intValue2)
strSubnetMask = Array("255.255.255.0")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
arrDNSServers = Array("10.1.1.124", "10.1.1.241")
objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Next

Basically this script asks for the current computername (or IP address) and then the future IP address. I hardcoded the netmask and the DNS server IPs (as well as the default gateway) already because those never change for my needs. Obviously you should change the above numbers to what you require.

But then, after installing a new version of windows, I realized I was getting errors. The exact error is VBScript runtime error: 800A0046 Permission Denied: ‘GetObject’. Somehow I was being denied the ability to impersonate the local administrator using my script. I thought maybe it was a “new system” issue, so I tried it on an older machine (from my machine though) and it still failed. That narrowed it down to an issue on my machine.

It all boils down to DCOM not being enabled on the computer. DCOM, if disabled, makes for a more secure system (I’m basing that on all the trojans and viruses written to overflow the DCOM stack and take over systems – as well as all the patches written specifically to fix DCOM errors). However, DCOM is required by impersonation scripts like the one above.

Fix:
Open Control panels
Open Administrative Tools
Open Component Services
Navigate to Console Root >> Component Services >> Computers
Right Click My Computer and select Properties
Click on the Default Properties Tab
Click on Enable Distributed COM on this computer

Some people will require a reboot, but my script works just fine after clicking OK.

[EDIT]

It has come to my attention that a lot of people would like to do this remotely. And by a “lot of people” I mainly mean me.
Here’s how:
Open up regedit (start >> run >> regedit)
Navigate to HKEY_LOCAL_MACHINE\Software\Microsoft\OLE
You will see a REG_SZ key with the name EnableDCOM
Change the value of the key to Y for yes, or N for no. Then reboot the remote system.

Leave a Reply

Your email address will not be published. Required fields are marked *