Solved

VBScript help

Posted on 2013-01-28
5
400 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

18 Experts available now in Live!

Get 1:1 Help Now