Solved

VBScript help

Posted on 2013-01-28
5
408 Views
Last Modified: 2013-01-29
I would like to modify the following VB script to check chassis type and if the chassis type is a laptop I would like the script to end. If else I want the script to continue on and add the DNS. Can anyone help me out or point me in the right direction, preferably the former as I really no nothing about VB. :)

Thanks,
Mike

	strComputer = "."
	'wscript.echo strComputer
	'strComputer = InputBox("Enter the Machine Name you want to modify")
	On Error Resume Next
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set colNetCards = objWMIService.ExecQuery _
	("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
	'change arrDNSServers to reflect values for your environment
	For Each objNetCard in colNetCards
		arrDNSServers = Array("0.0.0.1","0.0.0.2","0.0.0.3")
		objNetCard.SetDNSServerSearchOrder(arrDNSServers)
	Next

	If Err.Number <> 0 Then 
		'wscript.echo err.number
		'If err.number =  462 then
			'strStatus = "Error-COULD NOT CONNECT," &strComputer
		'ElseIf err.number = 451 then
			'strStatus = "Error-ACCESS DENIED," &strComputer		
		'Else
			strStatus = "Error," & strComputer
		'End If
	Else
		strStatus = "Success," & strComputer
	End If
	w.writeline strStatus
	Err.Clear
	On Error Goto 0

Open in new window

0
Comment
Question by:PAdocIT
[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
  • 3
  • 2
5 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 38829086
Hi, your script seems to be missing the log file creation, but this should work.

Regards,

Rob.

strComputer = "."
Set objNetwork = CreateObject("WScript.Network")
If strComputer = "." Then strComputer = objNetwork.ComputerName
'wscript.echo strComputer
'strComputer = InputBox("Enter the Machine Name you want to modify")
strChassisType = CheckChassis(strComputer)
If strChassisType = "Laptop" Then
	strStatus = "Not changing laptop," & strComputer
ElseIf strChassisType = "Unknown" Then
	strStatus = "Unknown chassis," & strComputer
Else
	On Error Resume Next
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set colNetCards = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
	'change arrDNSServers to reflect values for your environment
	For Each objNetCard in colNetCards
		arrDNSServers = Array("0.0.0.1","0.0.0.2","0.0.0.3")
		objNetCard.SetDNSServerSearchOrder(arrDNSServers)
	Next

	If Err.Number <> 0 Then 
		'wscript.echo err.number
		'If err.number =  462 then
			'strStatus = "Error-COULD NOT CONNECT," &strComputer
		'ElseIf err.number = 451 then
			'strStatus = "Error-ACCESS DENIED," &strComputer		
		'Else
			strStatus = "Error," & strComputer
		'End If
	Else
		strStatus = "Success," & strComputer
	End If
End If
w.writeline strStatus
Err.Clear
On Error Goto 0

Function CheckChassis(sComputer)
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
	
	bIsLaptop = False
	bIsDesktop = False
	bIsServer = False

	Set colEnclosure = objWMIService.ExecQuery("Select ChassisTypes from Win32_SystemEnclosure")
	For Each objInstance In colEnclosure
		If objInstance.ChassisTypes(0) = 12 Or objInstance.ChassisTypes(0) = 21 then
			' Ignore docking stations
		Else
			Select Case objInstance.ChassisTypes(0)
				Case "8", "9", "10", "11", "12", "14", "18", "21"
					bIsLaptop = true
				Case "3", "4", "5", "6", "7", "15", "16"
					bIsDesktop = true
				Case "23"
					bIsServer = true
				Case Else
					' Do nothing
			End Select
		End If
	Next
	
	If bIsLaptop = True Then
		sReturn = "Laptop"
	ElseIf bIsDesktop = True Then
		sReturn = "Desktop"
	ElseIf bIsServer = True Then
		sReturn = "Server"
	Else
		sReturn = "Unknown"
	End If
	
	CheckChassis = sReturn

End Function

Open in new window

0
 
LVL 5

Author Comment

by:PAdocIT
ID: 38829479
Thanks I will try it out. I'm not really sure about log file creation as I didn't create the script. The script is run as part of our site policy in GP. Our AD/GPO guy added this script (don't think he wrote it either) to auto set DNS based on the machine's network info. This was done to help avoid user input errors or forgetting to set DNS all together during machine setup. Not much of an issue since switching to SCCM OSD but he refuses to get rid of the script. The problem I have now is when we deploy laptops they get these DNS addresses embedded in them and users can't resolve domain names when connected mobile broadband or wifi at home. Big problem since our VPN is web based. If only our networking guy would go to DHCP this would all be moot. Ahhh government. :)

Anyway a little background since my original post was a bit vague.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38829498
OK, don't worry about the log file then.  Remove this line
w.writeline strStatus

Wow, no DHCP...that must be a nightmare....I hope this script helps.

Rob.
0
 
LVL 5

Author Closing Comment

by:PAdocIT
ID: 38832560
Thanks Rob. Worked like a charm.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38832901
Very good.  Thanks for the grade.

Regards,

Rob.
0

Featured Post

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!

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

705 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