Solved

VBScript help

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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