Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DHCP to Static IP

Status
Not open for further replies.

Ross1811

MIS
Oct 1, 2004
122
0
0
US
Hi everyone,

Just need to make a script that sets the computer from DHCP to a static IP so far I have this it gives me the error errEnable, or is there an easier way??????

P.S. I define all of the IP settings also,

Thanks,
Ross

***********************************
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway)
errDNSServers = objNetAdapter.SetDNSServerSearchOrder(strDNSServers)

If errEnable = 0 Then
WScript.Echo "Static IP addressing now configured for "&strComputer
Else
WScript.Echo "Static IP addressing could not be configured."
End If
Next

set strIPAddress = nothing
set strSubnetMask = nothing
set strGateway = nothing
set strComputer = nothing

wscript.quit
 
if they are w2k or xp machines you could use netsh commands?!
 
You have not specified how you are reading your values in. Also, If you have set On Error Resume Next, you will not see the reason for the failure. It could be that you have a type mismatch on the parameter type.

Try the following:

Code:
[COLOR=red]strComputer = "."
strIPAddress = Array("10.1.1.2")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("10.1.1.1")
strDNSServers = Array("10.1.1.50", "10.1.1.51")[/color]

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

For Each objNetAdapter in colNetAdapters
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    errGateways = objNetAdapter.SetGateways(strGateway)
    errDNSServers = objNetAdapter.SetDNSServerSearchOrder(strDNSServers)

    If errEnable = 0 Then
        WScript.Echo "Static IP addressing now configured for "&strComputer
    Else
        WScript.Echo "Static IP addressing could not be configured."
    End If
Next

set strIPAddress = nothing
set strSubnetMask = nothing
set strGateway = nothing
set strComputer = nothing

wscript.quit

Replace the IP addresses with your IP Addresses.

Spong
 
If the pc is Windows XP and you have a need for only one static IP. Set the IP to DHCP and an Alternate Configuration tab will appear. Set the static IP in the alternate config. When the computer is powered on if a dhcp server is unavailable then it will use the alternate config ip.

DDNWolff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top