?
Solved

Vbscript to run some other scripts (Kix) on the network.

Posted on 2008-10-31
18
Medium Priority
?
681 Views
Last Modified: 2012-05-05
Ok, ive got a vbscript that will change the computer name to the service tag of the machine then join it to the domain. Ive got stuck, after its joined the domain i need to run some kix scripts to update current software on the machine. But as the machine has not restarted yet it failes to run the kix. Is there a way to maybe restart the machine and login as a domain user? then run the kix. Ive already got a script that will run the kix, but only when logged in as a domain user.
0
Comment
Question by:k3eper
[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
  • 10
  • 8
18 Comments
 
LVL 7

Expert Comment

by:Hubasan
ID: 22850457
Hi k3eper,

You can restart the PC but simple batch script:

SHUTDOWN -r will restart the PC in 30 seconds with the message and timer on the screen or use:

SHUTDOWN -r -t ss

Latter will restart the PC in what ever number of seconds you put instead of "ss"

As for the Automatic Logon, you can use the registry to accomplish that. Here is a Microsoft article on Autologon:
http://support.microsoft.com/kb/315231
0
 

Author Comment

by:k3eper
ID: 22865291
Hi Thanks for the Microsoft Article. Im already using he shutdown command. Just thought there might be a vbscript alternative to it all. If i use the registry entry method if need to be able to delete the reg entry after all operations are complete. Any idea how to delete entries using vbs?
0
 
LVL 7

Expert Comment

by:Hubasan
ID: 22867038
Sure, depending what type of registry entries you want to delete (DWORD, STRING, BINARY...etc) here is the script example that reads and writes DWORD and String registry entries.

Basic principles are these:

Top of the script are your Constants for declaring Root keys in the registry and their hex values.
Even tho you are not all of them I like to keep them in the scripts just in case I would need them later.

Then you create an object (oReg) in my case, to connect to WMI StdRegProv of the PC, who's registry you want to change.

Then put the Reg key path in the variable, in the case below we are connecting to HKLM - Winlogon key.
Then you put the ValueName in the variable (sValueName)
And then do the same thing with value data of that Value name. (sValue)
Then just tell the registry to SetStringValue in HKEY_LOCAL_MACHINE....to what ever your value was.
All of the above you do if you want to write the key.

To just read the key, you would put oReg.GetStringValue....and then which key you want to read....

Let me know if you have any questions. :-)


Const HKEY_CLASSES_ROOT = &h80000000
Const HKEY_CURRENT_USER = &h80000001
Const HKEY_LOCAL_MACHINE = &h80000002
Const HKEY_USERS = &h80000003
Const HKEY_CURRENT_CONFIG = &h80000005
 
 
'==================================================================
'============READING Registry values===============================
'==================================================================
sComputer = "."
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    sComputer & "\root\default:StdRegProv")
 
sKeyPath = "SOFTWARE\System Admin Scripting Guide"
sValueName = "String Value Name"
sValue = "string value"
oReg.SetStringValue HKEY_LOCAL_MACHINE,sKeyPath,sValueName,sValue
 
sValueName = "DWORD Value Name"
dwValue = 82
oReg.SetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,sValueName,dwValue
'==================================================================
 
sComputer = "."
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    sComputer & "\root\default:StdRegProv")
 
sKeyPath = "SOFTWARE\System Admin Scripting Guide"
sValueName = "String Value Name"
oReg.GetStringValue HKEY_LOCAL_MACHINE,sKeyPath,sValueName
 
sValueName = "DWORD Value Name"
oReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,sValueName

Open in new window

0
Industry Leaders: 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!

 
LVL 7

Assisted Solution

by:Hubasan
Hubasan earned 400 total points
ID: 22867048
I forgot to add the section heading for Writing registry. so here it is, Same script just with a heading:
'==================================================================
'============READING Registry values===============================
'==================================================================
 
sComputer = "."
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    sComputer & "\root\default:StdRegProv")
 
sKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
sValueName = "String Value Name"
oReg.GetStringValue HKEY_LOCAL_MACHINE,sKeyPath,sValueName
 
sValueName = "DWORD Value Name"
oReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,sValueName
 
'==================================================================
'============Writing Registry values===============================
'==================================================================
sComputer = "."
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    sComputer & "\root\default:StdRegProv")
 
sKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
sValueName = "String Value Name"
sValue = "string value"
oReg.SetStringValue HKEY_LOCAL_MACHINE,sKeyPath,sValueName,sValue
 
sValueName = "DWORD Value Name"
dwValue = 82
oReg.SetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,sValueName,dwValue

Open in new window

0
 

Author Comment

by:k3eper
ID: 22867665
thanks alot ill try work it out. Ive been using this script. It seems to work fine just having a problem with the joining domain section of it. It seems to join the domain but i cant login to the domain. Any ideas?
'''CHANGECOMPUTER NAME
 
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSMBIOS = objWMIService.ExecQuery ("Select * from Win32_SystemEnclosure")
For Each objSMBIOS in colSMBIOS
  strSN = objSMBIOS.SerialNumber
  If strSN <> "" Then exit For
Next 
 
'''JoinDomain
 
Set ws = WScript.CreateObject("WScript.Shell")
value1 = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\"
value2 = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\ParameterS\"
ws.RegWrite value1 & "ComputerName", strSN
ws.RegWrite value2 & "NV Hostname", strSN
 
 
Const JOIN_DOMAIN             = 1
Const ACCT_CREATE             = 2
Const ACCT_DELETE             = 4
Const WIN9X_UPGRADE           = 16
Const DOMAIN_JOIN_IF_JOINED   = 32
Const JOIN_UNSECURE           = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET        = 256
Const INSTALL_INVOCATION      = 262144
strDomain   = "xxxxx"
strPassword = "xxxx"
strUser     = "xxxx"
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
Set objComputer = _
    GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" _
    & strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
    strPassword, _
    strDomain & "\" & strUser, _
    NULL, _
    JOIN_DOMAIN + ACCT_CREATE)
 
''''REG ENTRIES
 
Dim WSHShell
Set WSHShell = WScript.CreateObject("WScript.Shell")
 
'WSHShell.Popup "Set value of DefaultDomainName to 'xxxx'"
WSHShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", "xxxx"
 
'WSHShell.Popup "Set value of DefaultUserName to 'xxxx'"
WSHShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName", "xxxx"
 
'WSHShell.Popup "Set Key DefaultPassword"
WSHShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", "DefaultPassword"
 
'WSHShell.Popup "Set value for Default Password"
WSHShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword", "xxxx!"
 
'WSHShell.Popup "Set value AutoAdminLogon to 1"
WSHShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon", "1"
 
'WSHShell.Popup "Set value AutoLogonCount to 1"
WSHShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoLogonCount", "1"

Open in new window

0
 

Author Comment

by:k3eper
ID: 22867779
once that script runs it restarts. then i want it to login automaticly with a domain account and run another script to update software on the machine then removed the entries from the registry and restart again. (attached)
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim WSHShell : Set WSHShell = WScript.CreateObject("WScript.Shell")
 
WinDir = WSHSHell.ExpandEnvironmentStrings ("%SystemRoot%") & "\System32\"
 
If Not objFSO.FileExists (WinDir & "vbscript.dll") Then objFSO.CopyFile "\\zeus\dtsupp\everyone\AFE Fixs\vbscript.dll", WinDir , True
If Not objFSO.FileExists (WinDir & "MSCOMCT2.OCX") Then objFSO.CopyFile "\\zeus\dtsupp\everyone\afe fixs\MSCOMCT2.OCX", WinDir, True
 
wshshell.Run "kix32 \\ntscti_4\cti\install_v5.kix $overwrite=1", 3, true
wshshell.Run "kix32 \\zeus\dtsupp\everyone\scripts\install.kix", 3, true
wshshell.Run "\\zeus\dtsupp\everyone\scripts\winupdate.exe", 3, true
wshshell.Run "regsvr32 /s c:\windows\system32\vbscript.dll"
 
WSHShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword", 3, true
wshshell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUsername", 3, true
wshshell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", 3, true
 
wshshell.run "shutdown /r"
 
Set WSHShell = Nothing
WScript.Quit(0)

Open in new window

0
 
LVL 7

Expert Comment

by:Hubasan
ID: 22869737
Hi k3eper,

From what I can see your script should work fine. After the computer is added to domain, a reboot is required for you to be able to log on. And if it doesn't work AFTER the reboot, then the only thing I can see that could be wrong is that the computer NAME you chose already exists in the domain.
Well actually there is one other possible problem:
IF your domain has multiple Domain Controllers like my environment, then if your script creates computer account on one DC, and other didn't replicate in time, then you reboot and logon attempt is done on the non-replicated domain. This is were the problem could be, however this is unlikely.
0
 

Author Comment

by:k3eper
ID: 22876288
ok so the domain joining section of the script is setup correctly?
0
 

Author Comment

by:k3eper
ID: 22876318
The computer name wont be in use as its a service tag and unique. Sigh how anoying lol
0
 
LVL 7

Expert Comment

by:Hubasan
ID: 22876354
Yeah Domain joining is done right. I have the same code and it works no problem. I believe this is not a scripting issue, but before I continue, tell me...Do these problems happen every time you build a new PC or are they intermittent? For example, today you join 10 PC's to the domain with this script but only on 4 of them you can actually log the user on after the restart? Or does the script fail to log the user on after the restart every time?
0
 

Author Comment

by:k3eper
ID: 22876508
OK im defo thinking the script isntworking correctly ive just tried loggin into the machine (domain account) and its still not loggin in. I dont think its creating the account on the domain.
0
 
LVL 7

Expert Comment

by:Hubasan
ID: 22876722
Well, that's easy enough to check:

Here use this script to check if you computer is in domain. It will prompt you for Computer name just type it in and it will search for it in the domain, and if found it will display the message. If not found then you get a different message.
On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
Const cTitle = "Check if Computer exists in Domain"
Set oWS = CreateObject("wscript.shell")
Set oNet = WScript.CreateObject("wscript.network")
 
sDomain = oNet.UserDomain
bCompFound = False
 
Do
	sComputer = InputBox("Please enter Computer Name of the computer you wish to connect to?", cTitle,sLocCompName)
	If Len(sComputer) = 0 Then
		sRes = oWS.Popup ("Sorry, you must enter Computer Name to continue.  Do you wish to try again?", , cTitle, vbExclamation+vbYesNo)
		If sRes = vbNo Then
			WScript.Quit
		End If
	End If
Loop Until Len(sComputer) <> 0
 
Set oRootDSE = GetObject("LDAP://rootDSE")
sADsPath = "LDAP://" & oRootDSE.Get("defaultNamingContext")
Set oDomain = GetObject(sADsPath)
 
Set oConnection = CreateObject("ADODB.Connection")
Set oCommand =   CreateObject("ADODB.Command")
oConnection.Provider = "ADsDSOObject"
oConnection.Open "Active Directory Provider"
 
Set oCommand.ActiveConnection = oConnection
oCommand.CommandText = "Select Name from '" & sADsPath & "' where objectClass='computer'"
oCommand.Properties("Page Size") = 5000
oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set oRecordSet = oCommand.Execute
oRecordSet.MoveFirst
 
Do Until oRecordSet.EOF
	sCompName = oRecordSet.Fields("Name").Value
	If sCompName = sComputer Then
		bCompFound = True
		oWS.Popup "Computer: " & sComputer & " is found in domain " & sDomain, , cTitle, vbInformation
		Exit do  	
  End If
    oRecordSet.MoveNext
Loop
 
If Not bCompFound Then
	oWS.Popup "Computer: " & sComputer & " NOT found in domain " & sDomain, , cTitle, vbcritical
End If

Open in new window

0
 
LVL 7

Expert Comment

by:Hubasan
ID: 22876736
small correction to the previous Text prompt:
On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
Const cTitle = "Check if Computer exists in Domain"
Set oWS = CreateObject("wscript.shell")
Set oNet = WScript.CreateObject("wscript.network")
 
sDomain = oNet.UserDomain
bCompFound = False
 
Do
	sComputer = InputBox("Please enter Computer Name you wish to search for?", cTitle,sLocCompName)
	If Len(sComputer) = 0 Then
		sRes = oWS.Popup ("Sorry, you must enter Computer Name to continue.  Do you wish to try again?", , cTitle, vbExclamation+vbYesNo)
		If sRes = vbNo Then
			WScript.Quit
		End If
	End If
Loop Until Len(sComputer) <> 0
 
Set oRootDSE = GetObject("LDAP://rootDSE")
sADsPath = "LDAP://" & oRootDSE.Get("defaultNamingContext")
Set oDomain = GetObject(sADsPath)
 
Set oConnection = CreateObject("ADODB.Connection")
Set oCommand =   CreateObject("ADODB.Command")
oConnection.Provider = "ADsDSOObject"
oConnection.Open "Active Directory Provider"
 
Set oCommand.ActiveConnection = oConnection
oCommand.CommandText = "Select Name from '" & sADsPath & "' where objectClass='computer'"
oCommand.Properties("Page Size") = 5000
oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set oRecordSet = oCommand.Execute
oRecordSet.MoveFirst
 
Do Until oRecordSet.EOF
	sCompName = oRecordSet.Fields("Name").Value
	If sCompName = sComputer Then
		bCompFound = True
		oWS.Popup "Computer: " & sComputer & " is found in domain " & sDomain, , cTitle, vbInformation
		Exit do  	
  End If
    oRecordSet.MoveNext
Loop
 
If Not bCompFound Then
	oWS.Popup "Computer: " & sComputer & " NOT found in domain " & sDomain, , cTitle, vbcritical
End If

Open in new window

0
 

Author Comment

by:k3eper
ID: 22876744
I just checked it manually through AD it is there so i really dont know whats wrong. Im getting a list of domains to join at login like normal just cant login to them :(
0
 

Author Comment

by:k3eper
ID: 22876757
If i join the machine manually via system properies it works fine :(( sigh
0
 
LVL 7

Expert Comment

by:Hubasan
ID: 22876772
when you say you can't log into them, what exactly do you see on the screen. What error message?
0
 

Author Comment

by:k3eper
ID: 22876856
im getting this

"Windows cannot connect to the domain either because the domain controller is down or otherwise unavailable, or because your computer account was not found. Please try again later. If this message continues to appear contact your System Administrator for assistance."
0
 

Accepted Solution

by:
k3eper earned 0 total points
ID: 23001812
Worked out the problem, looks like the account being used was causing the prob, some how it wasnt joining machines correctly (not sure how) but when i use my own account details in the script it works fine.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

777 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