Solved

VBScript help

Posted on 2013-01-28
5
399 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
  • 3
  • 2
5 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks Rob. Worked like a charm.
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Very good.  Thanks for the grade.

Regards,

Rob.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now