[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

VBS Script

Posted on 2014-03-18
13
Medium Priority
?
328 Views
Last Modified: 2014-03-18
Hey guys,

I am trying to write a VB script to return a value or "DHCP" or "Static" depending on how the NIC is configured.

Windows 7 and Server 2008 R2
0
Comment
Question by:triphen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 39937951
You can try this:

' If no argument is supplied then script is executed on local computer
set args = Wscript.Arguments
If Wscript.Arguments.Count = 0 Then
	strComputer = "." 
Else
	strComputer = args.item(0)
end if

'Query WMI
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_NetworkAdapterConfiguration") 
'Output to console
For Each objItem in colItems 
    Wscript.Echo "DHCPEnabled: " & objItem.DHCPEnabled
Next

Open in new window


This will return true or false depending on wheter or not DHCP is enabled
0
 

Author Comment

by:triphen
ID: 39937968
It returns 1 True, then about 10 Falses.
0
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 39937972
How many NIC cards do you have?  Do you have VMWare installed?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:triphen
ID: 39938002
1, Yes

Let me check another machine.....good call

Is it possible to Return "DHCP" and "Static" vs True and False?
0
 

Author Comment

by:triphen
ID: 39938010
Same result on another machine  with only 1 NIC and no VM installed. Several Pop ups. True, False, False, False, False, False, False, True, False, False, etc.
0
 
LVL 15

Accepted Solution

by:
Juan Ocasio earned 2000 total points
ID: 39938024
Try this

' If no argument is supplied then script is executed on local computer
set args = Wscript.Arguments
If Wscript.Arguments.Count = 0 Then
	strComputer = "." 
Else
	strComputer = args.item(0)
end if

'Query WMI
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = true") 
'Output to console
For Each objItem in colItems 
    'Wscript.Echo "DeviceName: " & objItem.DHCPEnabled
If objItem.DHCPEnabled Then
     Wscript.Echo "Enabled"
Else
    Wscript.Echo "Static"
End If

Next

Open in new window

0
 

Author Comment

by:triphen
ID: 39938061
This looks promising :)


I modified to this, since I will always be executing on local PC.

'Query WMI
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = true") 
'Output to console
For Each objItem in colItems 
    'Wscript.Echo "DeviceName: " & objItem.DHCPEnabled
If objItem.DHCPEnabled Then
     Wscript.Echo "DHCP"
Else
    Wscript.Echo "Static"
End If

Next

Open in new window



This works great!

Now one small problem. I am trying to use this with BGInfo by SysInternals. When loading this script I get:

Line 8, position 5 Object Required 'Wscript'
0
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 39938086
If you run the script you can send arguments so for example if you named it IsDHCPEnabled.vbs and you're querying a server named Server1, you would type in at the command line:

IsDHCPEnabled.vbs Server1

You have to have permission on the remote computer, otherwise it won't run
0
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 39938105
Scrap my last comment.  I misunderstood your last post.  I'm not sure why SysInternal is producing an error.  Perhaps you have to reference the Wscript object in the IDE?
0
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 39938136
In BGInfo, you can create a custom Field and select WMI.  Browse Path then select the class Win32_NetworkAdapterConfiguration and class property of DHCPEnabled  Under the WMI query modify the query to read

SELECT DHCPEnabled FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = true

Evaluate it to make sure the query evaluates as expected then click OK.  Make sure you name the field and then select it from the field list.

This should accomplish what you want within BGInfo
0
 

Author Comment

by:triphen
ID: 39938261
This worked for me and BGInfo...


Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = true")
'Output to console
For Each objItem in colItems
    'Wscript.Echo "DeviceName: " & objItem.DHCPEnabled

If objItem.DHCPEnabled Then
On Error Resume Next
     Wscript.Echo "DHCP"
Echo "DHCP"
on error goto 0
Else
On Error Resume Next
    Wscript.Echo "Static"
Echo "Static"
on error goto 0
End If

Next
0
 

Author Closing Comment

by:triphen
ID: 39938263
Thank you! Couldn't have done it without you :)
0
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 39938278
No problem!  Glad you got it working!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question