Solved

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

Posted on 2008-10-31
18
642 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
  • 10
  • 8
18 Comments
 
LVL 7

Expert Comment

by:Hubasan
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 7

Assisted Solution

by:Hubasan
Hubasan earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ok so the domain joining section of the script is setup correctly?
0
 

Author Comment

by:k3eper
Comment Utility
The computer name wont be in use as its a service tag and unique. Sigh how anoying lol
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 7

Expert Comment

by:Hubasan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
If i join the machine manually via system properies it works fine :(( sigh
0
 
LVL 7

Expert Comment

by:Hubasan
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now