Solved

VB Script Menu option

Posted on 2011-03-07
10
1,061 Views
Last Modified: 2013-11-26
Hi Guys, Need a bit of assistance.

I have a couple of questions and i appreciate any assistance i can get.

I have been working on a script that will do some of the basics i need to add a computer to a domain.  This script is intended for multiple computers scattered all around australia.

It will check if the user account exists on the domain and then delete it,  Will then give a user a menu to select there local site.  Based on the choice it will then proceed to add the computer to the domain, move it to the correct ou and add it to the correct security group.  It will change the time zone and complete a gpupdate before restarting the computer.

I am utilising this script inconjunction with a sysprepped windows 7 image. The image will do the basics such as computer name , activation and set admin accounts and as part of the setupcomplete.cmd file it will copy this join_domain/cmd script to the public all users desktop.
This will call on the vbscript as below and after completion do a clean up and delete the relevant reg, vbs and cmd files.

I am stuck at the moment.  In the user menu that is thrown up it prompts the user to select a number based on the site.  When the user selects a number it will then ask the user to confirm the selection.   My problem is that the first part works fine and the user selects a number but when they are prompted to confirm it only asks them to confirm the number selected and not the associated site name that the number refers to. Line 40 - 60 in below script.

i.e.
it asks
you selected option 1 is this correct,

it should ask
you selected option 1 - sydney state office


The second thing i need help with is if any key is pressed except the correct numbers it should loop back to the menu screen so the user can select again.  If 0 is selected the script will exit.
Once the user selects a correct number and is asked to confirm they are prompted for a Y or N answer.  I am trying to make this Y or N case insensative but i keep getting stuck.  


I also have a question. I am using this script to add the computer to a domain using a privileged domain account.  I am also going to add some lines to run a cscript command to complete the office 2010 activation.  The site is currently using a proxy that is not open and that requires a domain account for authentication. If i add the lines in this script to complete the office activation (which requires web access) will it use the same account that adds the device to the domain set at the beginning of this script or do i have to tell it to use another account before these extra lines are added.


Any assistance will be greatly appreciated.

' join_domain.vbs

on error resume next

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 = "domainname" 
strPassword = "password" 
strUser = "username"   
strLDAPServer = "192.168.83.101" '- <-- Enter domain controller

Set objNetwork = CreateObject("WScript.Network") 
set shell = CreateObject("Wscript.Shell") 

strComputer = objNetwork.ComputerName   

set objComputer = Nothing
' first lets delete the computer from AD

strComputerDN = GetDN(strComputer)
If Len(strComputerDN) > 0 Then
   DeleteCOmputer(strComputerDN)
End If


set objComputer = Nothing

' prompt user input for state location
intState = -1
strResponse = "No"

do
     'on error resume next
     intState = CInt(Left(UserInput( "Enter the below choice for your Site Location: " _
                      & VbCrLf & "1. NSW State Office " _
					  & VbCrLf & "2. NSW Factory " _
					  & VbCrLf & "3. NSW Display Home " _
					  & VbCrLf & "4. MEL State Office " _
					  & VbCrLf & "5. VIC Display Home " _
					  & VbCrLf & "6. BRI State Office " _
					  & VbCrLf & "7. QLD Display Home " _
					  & VbCrLf & "8. SA Stae Office " _
					  & VbCrLf & "9. SA Display Home " _	
                      & VbCrLf & "0. To Exit" & VbCrLf ),1))
	
    strResponse = UserInput("You selected:" & intState & VbCrLf & "Are you sure Y or N")
     
' keep looping until correct input is detected
loop while ((Not(intState >= 0) OR Not(intState < 10) OR (Not (strResponse = "y"))))




'intState = 100

If intState = 1 then

 JoinToDomain(strComputer)
   ChangeTimeZone("AUS Eastern Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=Syd State Office,OU=NSW,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=NSW_SYD_State_Office_All_Computers,OU=NSW Security Groups,OU=NSW,OU=Construction,DC=SHA,DC=LOCAL"
   Set WSHShell = WScript.CreateObject("WScript.Shell")
   wshshell.run "C:\windows\regedit.exe /s c:\windows\setup\scripts\CTRL_ALT_DELETE.reg"
   wshshell.run "C:\windows\system32\gpupdate.exe /force"
   wshshell.run "C:\windows\system32\wuauclt.exe /resetauthorization /detectnow"
   WshShell.Run "C:\WINDOWS\system32\shutdown.exe -f -r -t 0"


elseif intState = 2 then
 JoinToDomain(strComputer)
   ChangeTimeZone("AUS Eastern Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=NSW Factory,OU=NSW,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=NSW_Factory_All_Computers,OU=NSW Security Groups,OU=NSW,OU=Construction,DC=SHA,DC=LOCAL"
   Set WSHShell = WScript.CreateObject("WScript.Shell")
   wshshell.run "C:\windows\regedit.exe /s c:\windows\setup\scripts\CTRL_ALT_DELETE.reg"
   wshshell.run "C:\windows\system32\gpupdate.exe /force"
   wshshell.run "C:\windows\system32\wuauclt.exe /resetauthorization /detectnow"
   WshShell.Run "C:\WINDOWS\system32\shutdown.exe -f -r -t 0"


elseif intState = 3 then
 JoinToDomain(strComputer)
   ChangeTimeZone("AUS Eastern Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=NSW Display Homes,OU=NSW,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=NSW_Factory_All_Computers,OU=NSW Security Groups,OU=NSW,OU=Construction,DC=SHA,DC=LOCAL"
   Set WSHShell = WScript.CreateObject("WScript.Shell")
   wshshell.run "C:\windows\regedit.exe /s c:\windows\setup\scripts\CTRL_ALT_DELETE.reg"
   wshshell.run "C:\windows\system32\gpupdate.exe /force"
   wshshell.run "C:\windows\system32\wuauclt.exe /resetauthorization /detectnow"
   WshShell.Run "C:\WINDOWS\system32\shutdown.exe -f -r -t 0"


elseif intState = 4 then
   JoinToDomain(strComputer)
   ChangeTimeZone("AUS Eastern Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=Mel State Office,OU=VIC,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=VIC_MEL_State_Office_All_Computers,OU=VIC Security Groups,OU=VIC,OU=Construction,DC=SHA,DC=LOCAL"
   Set WSHShell = WScript.CreateObject("WScript.Shell")
   wshshell.run "C:\windows\regedit.exe /s c:\windows\setup\scripts\CTRL_ALT_DELETE.reg"
   wshshell.run "C:\windows\system32\gpupdate.exe /force"
   wshshell.run "C:\windows\system32\wuauclt.exe /resetauthorization /detectnow"
   WshShell.Run "C:\WINDOWS\system32\shutdown.exe -f -r -t 0"


elseif intState = 5 then
   JoinToDomain(strComputer)
   ChangeTimeZone("AUS Eastern Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=VIC Display Homes,OU=VIC,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=VIC_Display_Homes_All_Computers,OU=VIC Security Groups,OU=VIC,OU=Construction,DC=SHA,DC=LOCAL"
   Set WSHShell = WScript.CreateObject("WScript.Shell")
   wshshell.run "C:\windows\regedit.exe /s c:\windows\setup\scripts\CTRL_ALT_DELETE.reg"
   wshshell.run "C:\windows\system32\gpupdate.exe /force"
   wshshell.run "C:\windows\system32\wuauclt.exe /resetauthorization /detectnow"
   WshShell.Run "C:\WINDOWS\system32\shutdown.exe -f -r -t 0"


elseif intState = 6 then
   JoinToDomain(strComputer)
   ChangeTimeZone("E. Australia Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=BRI State Office,OU=QLD,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=QLD_Bri_State_Office_All_Computers,OU=QLD Security Groups,OU=QLD,OU=Construction,DC=SHA,DC=LOCAL"
   Set WSHShell = WScript.CreateObject("WScript.Shell")
   wshshell.run "C:\windows\regedit.exe /s c:\windows\setup\scripts\CTRL_ALT_DELETE.reg"
   wshshell.run "C:\windows\system32\gpupdate.exe /force"
   wshshell.run "C:\windows\system32\wuauclt.exe /resetauthorization /detectnow"
   WshShell.Run "C:\WINDOWS\system32\shutdown.exe -f -r -t 0"


elseif intState = 7 then
   JoinToDomain(strComputer)
   ChangeTimeZone("E. Australia Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=QLD Display Homes,OU=QLD,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=QLD_Display_Homes_All_Computers,OU=QLD Security Groups,OU=QLD,OU=Construction,DC=SHA,DC=LOCAL"
   Set WSHShell = WScript.CreateObject("WScript.Shell")
   wshshell.run "C:\windows\regedit.exe /s c:\windows\setup\scripts\CTRL_ALT_DELETE.reg"
   wshshell.run "C:\windows\system32\gpupdate.exe /force"
   wshshell.run "C:\windows\system32\wuauclt.exe /resetauthorization /detectnow"
   WshShell.Run "C:\WINDOWS\system32\shutdown.exe -f -r -t 0"


elseif intState = 8 then
   JoinToDomain(strComputer)
   ChangeTimeZone("Cen. Australia Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=ADL State Office,OU=SA,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=SA_ADL_State_Office_All_Computers,OU=SA Security Groups,OU=SA,OU=Construction,DC=SHA,DC=LOCAL"
   Set WSHShell = WScript.CreateObject("WScript.Shell")
   wshshell.run "C:\windows\regedit.exe /s c:\windows\setup\scripts\CTRL_ALT_DELETE.reg"
   wshshell.run "C:\windows\system32\gpupdate.exe /force"
   wshshell.run "C:\windows\system32\wuauclt.exe /resetauthorization /detectnow"
   WshShell.Run "C:\WINDOWS\system32\shutdown.exe -f -r -t 0"


elseif intState = 9 then
   JoinToDomain(strComputer)
   ChangeTimeZone("Cen. Australia Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=SA Display Homes,OU=SA,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=SA_Display_Homes_All_Computers,OU=SA Security Groups,OU=SA,OU=Construction,DC=SHA,DC=LOCAL"
   Set WSHShell = WScript.CreateObject("WScript.Shell")
   wshshell.run "C:\windows\regedit.exe /s c:\windows\setup\scripts\CTRL_ALT_DELETE.reg"
   wshshell.run "C:\windows\system32\gpupdate.exe /force"
   wshshell.run "C:\windows\system32\wuauclt.exe /resetauthorization /detectnow"
   WshShell.Run "C:\WINDOWS\system32\shutdown.exe -f -r -t 0"

elseif intState = 0 then

   Jwscript.quit(0)

End If


Function JoinToDomain(strComputer)

  ' let's try to join to domain
  Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")   
  If (objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "\" & strUser, NULL, JOIN_DOMAIN + ACCT_CREATE) = 0) then
       shell.Popup  "Computer Added to Domain." , 3, Timer, 64

  else 
       wscript.echo "Computer Could not be added to the domain at this point in time."
  end if

  wscript.sleep(3000) ' sleep 3 seconds to ensure AD join completes OK  

End Function

Function MoveComputer(strNewOU, strComputerDN)
  shell.Popup "Attempting to move computer to " & strNewOU, 3, Timer, 64
  Set objNS = GetObject("LDAP:") 
  Set objNewOU = objNS.OpenDSObject("LDAP://" & strLDAPServer & "/" & strNewOU, strUser, strPassword, ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION) 
  Set objMoveComputer = objNewOU.MoveHere("LDAP://" &  strComputerDN, "cn=" & strComputer)
  shell.Popup  "Moved computer to new OU : " & strNewOU , 3, Timer, 64

ENd Function


Function UserInput( strPrompt )
    ' Check if the script runs in CSCRIPT.EXE
    If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
        ' If so, use StdIn and StdOut
        WScript.StdOut.Write strPrompt & " "
        UserInput = WScript.StdIn.ReadLine
    Else
        ' If not, use InputBox( )
        UserInput = InputBox( strPrompt )
    End If
End Function



Function GetDN(strComputer)

  Set objConnection = CreateObject("ADODB.Connection")

  Set oConnect = CreateObject("ADODB.Connection")
  oConnect.Provider = "ADsDSOObject"
  oConnect.Properties("User ID") = strUser
  oConnect.Properties("Password") = strPassword
  oConnect.Properties("Encrypt Password") = True
  oConnect.Open "DS Query", strUser, strPass


  Set objCommand = CreateObject("ADODB.Command")
  objCommand.ActiveConnection = oConnect

  objCommand.CommandText = "<LDAP://"& strLDAPServer &"/dc=sha,dc=local>;(&(objectCategory=Computer)(Name=" & strComputer & "));distinguishedName;subtree"

  Set objRecordSet = objCommand.Execute

  If objRecordset.RecordCount = 0 Then
   shell.Popup("sAMAccountName: " & strComputer & " does not exist.") , 3, Timer, 64
  Else
     objRecordset.MoveFirst
     GetDN = objRecordset.Fields("distinguishedName").value
    shell.Popup(strComputer & " exists."), 3, Timer, 64
  End If	 

End Function

Sub AddComputerToGroup(strComputerDn,strGroupDN)
    shell.Popup "Attempting to add pc to group : " & strGroupDN , 3, Timer, 64
    Set objNS = GetObject("LDAP:") 
    set objComputer = objNS.OpenDSObject("LDAP://" & strLDAPServer & "/" & strComputerDn, strUser, strPassword, ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION) 
    set objGroup = objNS.OpenDSObject("LDAP://" & strLDAPServer & "/" & strGroupDN, strUser, strPassword, ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION) 
    objGroup.add objComputer.adspath
    shell.Popup "Performed action to Add computer to group" , 3, Timer, 64

End Sub

Sub ChangeTimeZone(strTimeZone)
   Set objShell = CreateObject("WScript.shell")  
   objShell.Run "c:\windows\system32\tzutil.exe /s " & chr(34) & strTimeZone & chr(34)
   Set shell = CreateObject("WScript.shell")
   shell.popup ("Timezone changed to " & chr(34) & strTimeZone & chr(34)), 3, Timer, 64
End Sub




Sub DeleteComputer (strDN)
    shell.Popup "Attempting to delete computer - " & strDN , 3, Timer, 64
    Set objNS = GetObject("LDAP:") 
    set objComputer = objNS.OpenDSObject("LDAP://" & strLDAPServer & "/" & strDN, strUser, strPassword, ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION) 
    objComputer.DeleteObject (0) 
    shell.Popup "Delete computer object from AD" , 3, Timer, 64


End Sub

Open in new window

0
Comment
Question by:tetran_au
  • 4
  • 4
  • 2
10 Comments
 
LVL 10

Expert Comment

by:Korbus
ID: 35063704
you call the function
UserInput
the value this function returns is a number only.

I suggest you make your options an ARRAY of strings.
you can then referince the appropriate string, using a number.

do you need help with arrays?

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 450 total points
ID: 35063751
Hi, I have fixed the code so that it prompts correctly.  I have also added a dictionary object for your options, so we can dynamically generate the options.

In regards to your Office activation question, the activation will try to authenticate with whatever credentials have initiated the activation. That is, if the activation is executed using some sort of run as approach (like PSExec using the credentials at the top of the script), then it will use those credentials.  On the other hand, if you use objShell.Run *without* specifying any other credentials, then it would try to run as the user that ran the script, which probably wouldn't work.

Regards,

Rob.
' join_domain.vbs

on error resume next

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 = "domainname" 
strPassword = "password" 
strUser = "username"   
strLDAPServer = "192.168.83.101" '- <-- Enter domain controller

Set objNetwork = CreateObject("WScript.Network") 
Set objShell = CreateObject("WScript.Shell") 

strComputer = objNetwork.ComputerName   

set objComputer = Nothing
' first lets delete the computer from AD

strComputerDN = GetDN(strComputer)
If Len(strComputerDN) > 0 Then
   DeleteCOmputer(strComputerDN)
End If


set objComputer = Nothing

' prompt user input for state location

Set dctOptions = CreateObject("Scripting.Dictionary")
dctOptions.Add 1, "NSW State Office"
dctOptions.Add 2, "NSW Factory"
dctOptions.Add 3, "NSW Display Home"
dctOptions.Add 4, "MEL State Office"
dctOptions.Add 5, "VIC Display Home"
dctOptions.Add 6, "BRI State Office"
dctOptions.Add 7, "QLD Display Home"
dctOptions.Add 8, "SA Stae Office"
dctOptions.Add 9, "SA Display Home"
dctOptions.Add 0, "To Exit"

strMsg = "Enter the below choice for your Site Location: "
For Each intOption In dctOptions
	strMsg = strMsg & VbCrLf & intOption & ". " & dctOptions(intOption)
Next

blnValid = False
While blnValid = False
	'on error resume Next
	intState = -1
	strResponse = "N"
	While Asc(intState) < 48 Or Asc(intState) > 57
		intState = Left(UserInput(strMsg & VbCrLf ), 1)
	Wend
	intState = CInt(intState)
	
	While Asc(strResponse) <> 110 And Asc(strResponse) <> 121
		strResponse = LCase(UserInput("You selected:" & intState & ". " & dctOptions(intState) & VbCrLf & "Are you sure Y or N"))
	Wend
	If strResponse = "y" Then blnValid = True
     
	' keep looping until correct input is detected
Wend

'intState = 100

If intState = 1 then

 JoinToDomain(strComputer)
   ChangeTimeZone("AUS Eastern Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=Syd State Office,OU=NSW,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strComputer)
   AddComputerToGroup strComputerDN,"CN=NSW_SYD_State_Office_All_Computers,OU=NSW Security Groups,OU=NSW,OU=Construction,DC=SHA,DC=LOCAL"

elseif intState = 2 then
 JoinToDomain(strComputer)
   ChangeTimeZone("AUS Eastern Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=NSW Factory,OU=NSW,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=NSW_Factory_All_Computers,OU=NSW Security Groups,OU=NSW,OU=Construction,DC=SHA,DC=LOCAL"

elseif intState = 3 then
 JoinToDomain(strComputer)
   ChangeTimeZone("AUS Eastern Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=NSW Display Homes,OU=NSW,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=NSW_Factory_All_Computers,OU=NSW Security Groups,OU=NSW,OU=Construction,DC=SHA,DC=LOCAL"

elseif intState = 4 then
   JoinToDomain(strComputer)
   ChangeTimeZone("AUS Eastern Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=Mel State Office,OU=VIC,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=VIC_MEL_State_Office_All_Computers,OU=VIC Security Groups,OU=VIC,OU=Construction,DC=SHA,DC=LOCAL"

elseif intState = 5 then
   JoinToDomain(strComputer)
   ChangeTimeZone("AUS Eastern Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=VIC Display Homes,OU=VIC,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=VIC_Display_Homes_All_Computers,OU=VIC Security Groups,OU=VIC,OU=Construction,DC=SHA,DC=LOCAL"

elseif intState = 6 then
   JoinToDomain(strComputer)
   ChangeTimeZone("E. Australia Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=BRI State Office,OU=QLD,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=QLD_Bri_State_Office_All_Computers,OU=QLD Security Groups,OU=QLD,OU=Construction,DC=SHA,DC=LOCAL"

elseif intState = 7 then
   JoinToDomain(strComputer)
   ChangeTimeZone("E. Australia Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=QLD Display Homes,OU=QLD,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=QLD_Display_Homes_All_Computers,OU=QLD Security Groups,OU=QLD,OU=Construction,DC=SHA,DC=LOCAL"

elseif intState = 8 then
   JoinToDomain(strComputer)
   ChangeTimeZone("Cen. Australia Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=ADL State Office,OU=SA,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=SA_ADL_State_Office_All_Computers,OU=SA Security Groups,OU=SA,OU=Construction,DC=SHA,DC=LOCAL"

elseif intState = 9 then
   JoinToDomain(strComputer)
   ChangeTimeZone("Cen. Australia Standard Time")
   strCOmputerDN = GetDN(strComputer)
   MoveComputer "OU=Computers,OU=SA Display Homes,OU=SA,OU=Construction,DC=SHA,DC=LOCAL", strComputerDN
   strComputerDN = GetDN(strCOmputer)
   AddComputerToGroup strComputerDN,"CN=SA_Display_Homes_All_Computers,OU=SA Security Groups,OU=SA,OU=Construction,DC=SHA,DC=LOCAL"

elseif intState = 0 then

   wscript.quit(0)

End If

   ' run these lines for all locations
   objShell.run "C:\windows\regedit.exe /s c:\windows\setup\scripts\CTRL_ALT_DELETE.reg"
   objShell.run "C:\windows\system32\gpupdate.exe /force"
   objShell.run "C:\windows\system32\wuauclt.exe /resetauthorization /detectnow"
   objShell.Run "C:\WINDOWS\system32\shutdown.exe -f -r -t 0"


Function JoinToDomain(strComputer)

  ' let's try to join to domain
  Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")   
  If (objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "\" & strUser, NULL, JOIN_DOMAIN + ACCT_CREATE) = 0) then
       objShell.Popup  "Computer Added to Domain." , 3, Timer, 64

  else 
       wscript.echo "Computer Could not be added to the domain at this point in time."
  end if

  wscript.sleep(3000) ' sleep 3 seconds to ensure AD join completes OK  

End Function

Function MoveComputer(strNewOU, strComputerDN)
  objShell.Popup "Attempting to move computer to " & strNewOU, 3, Timer, 64
  Set objNS = GetObject("LDAP:") 
  Set objNewOU = objNS.OpenDSObject("LDAP://" & strLDAPServer & "/" & strNewOU, strUser, strPassword, ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION) 
  Set objMoveComputer = objNewOU.MoveHere("LDAP://" &  strComputerDN, "cn=" & strComputer)
  objShell.Popup  "Moved computer to new OU : " & strNewOU , 3, Timer, 64

ENd Function


Function UserInput( strPrompt )
    ' Check if the script runs in CSCRIPT.EXE
    If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
        ' If so, use StdIn and StdOut
        WScript.StdOut.Write strPrompt & " "
        UserInput = WScript.StdIn.ReadLine
    Else
        ' If not, use InputBox( )
        UserInput = InputBox( strPrompt )
    End If
End Function



Function GetDN(strComputer)

  Set objConnection = CreateObject("ADODB.Connection")

  Set oConnect = CreateObject("ADODB.Connection")
  oConnect.Provider = "ADsDSOObject"
  oConnect.Properties("User ID") = strUser
  oConnect.Properties("Password") = strPassword
  oConnect.Properties("Encrypt Password") = True
  oConnect.Open "DS Query", strUser, strPass


  Set objCommand = CreateObject("ADODB.Command")
  objCommand.ActiveConnection = oConnect

  objCommand.CommandText = "<LDAP://"& strLDAPServer &"/dc=sha,dc=local>;(&(objectCategory=Computer)(Name=" & strComputer & "));distinguishedName;subtree"

  Set objRecordSet = objCommand.Execute

  If objRecordset.RecordCount = 0 Then
   objShell.Popup("sAMAccountName: " & strComputer & " does not exist.") , 3, Timer, 64
  Else
     objRecordset.MoveFirst
     GetDN = objRecordset.Fields("distinguishedName").value
    objShell.Popup(strComputer & " exists."), 3, Timer, 64
  End If	 

End Function

Sub AddComputerToGroup(strComputerDn,strGroupDN)
    objShell.Popup "Attempting to add pc to group : " & strGroupDN , 3, Timer, 64
    Set objNS = GetObject("LDAP:") 
    set objComputer = objNS.OpenDSObject("LDAP://" & strLDAPServer & "/" & strComputerDn, strUser, strPassword, ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION) 
    set objGroup = objNS.OpenDSObject("LDAP://" & strLDAPServer & "/" & strGroupDN, strUser, strPassword, ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION) 
    objGroup.add objComputer.adspath
    objShell.Popup "Performed action to Add computer to group" , 3, Timer, 64

End Sub

Sub ChangeTimeZone(strTimeZone)
   objShell.Run "c:\windows\system32\tzutil.exe /s " & chr(34) & strTimeZone & chr(34)
   objShell.popup ("Timezone changed to " & chr(34) & strTimeZone & chr(34)), 3, Timer, 64
End Sub




Sub DeleteComputer (strDN)
    objShell.Popup "Attempting to delete computer - " & strDN , 3, Timer, 64
    Set objNS = GetObject("LDAP:") 
    set objComputer = objNS.OpenDSObject("LDAP://" & strLDAPServer & "/" & strDN, strUser, strPassword, ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION) 
    objComputer.DeleteObject (0) 
    objShell.Popup "Delete computer object from AD" , 3, Timer, 64


End Sub

Open in new window

0
 
LVL 10

Assisted Solution

by:Korbus
Korbus earned 50 total points
ID: 35064016
 set shell = CreateObject("Wscript.Shell") 


 dim options(10)
 options(0)="1. NSW State Office "
 options(1)="2. NSW Factory "
 options(2)="3. NSW Display Home "
 options(3)="4. MEL State Office "
 options(4)="5. VIC Display Home " 
 options(5)="6. BRI State Office "
 options(6)="7. QLD Display Home "
 options(7)="8. SA Stae Office "
 options(8)="9. SA Display Home "
 options(9)="0. To Exit"

 
 intState = CInt(Left(InputBox( "Enter the below choice for your Site Location: " _
    & VbCrLf & options(0) _   
    & VbCrLf & options(1) _
    & VbCrLf & options(2) _
    & VbCrLf & options(3) _
    & VbCrLf & options(4) _
    & VbCrLf & options(5) _
    & VbCrLf & options(6) _
    & VbCrLf & options(7) _
    & VbCrLf & options(8) _	
    & VbCrLf & options(9) & VbCrLf ),1))
' you can make that a loop now	

 strResponse = InputBox("You selected:" & options(intState-1) & VbCrLf & "Are you sure Y or N")

Open in new window


Note: I swapped out userinput for inputbox- same diff tho
0
 
LVL 2

Author Comment

by:tetran_au
ID: 35064921
Hey guys, Thanks for your assistance,

I have just copied the modified codes and will test it out.

I will let you know.  I will be back shortly
0
 
LVL 2

Author Comment

by:tetran_au
ID: 35065294
Hey Korbus and Rob,

  I have tested this and it seems to be working fine. Thank you both for your input and your speedy response.  I have gone with the solution that Rob presented and can see that the script is doing basically everything i initially wanted it to do.,

In terms of doing the activation Rob,  When you mention things like PSexec, do you mean use psexec to launch the vbscript, or call on psexec from within the vbscript to complete the activation
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 65

Expert Comment

by:RobSampson
ID: 35065358
>> do you mean use psexec to launch the vbscript, or call on psexec from within the vbscript to complete the activation

You could do either, but this depends on the rights of the user account being used.

If the account you will specify will have rights to do everything in the script, including proxy authentication, then using PSExec to launch the script will be fine.

On the other hand, if the account you specify would have no local admin rights, but will have proxy rights, then only use PSExec to launch the activation process.

Regards,

Rob.
0
 
LVL 2

Author Comment

by:tetran_au
ID: 35065725
Thanks for your help again rob,

This will be run before the computer is part of the domain so it will be running as the local workstation administrator (Sysprep is has autoadmin login unitl after the join domain vbs is run).

I need to run the activations commands as the same user that will be specified at the top of the script to do the domain addition

These are the commands i need to run

C:\program files (x86)\microsoft office\office14\ospp.vbs /inpkey:xxx:xxx:xxx:xxx:xxx
C:\program files (x86)\microsoft office\office14\ospp.vbs /act

Would i be able to use the objshell.run line to use the psexec.

objShell.run C:\psexec.exe -u domainname\username -u password -d "C:\program files (x86)\microsoft office\office14\ospp.vbs"  /inpkey:xxx:xxx:xxx:xxx:xxx

objShell.run C:\psexec.exe -u domainname\username -u password -d C:\program files (x86)\microsoft office\office14\ospp.vbs /act

I am pretty new with this scripting could you please help me identify where i need to put allthe " " and ' ' if needed.

I think if i can get this completed then my script would be 100%


0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 450 total points
ID: 35065749
To do it that way, you need to double any required quotes, then enclose the whole thing in quotes.

objShell.run "C:\psexec.exe -u " & strDomain & "\" & strUser & " -p " & strPassword & " -i ""C:\program files (x86)\microsoft office\office14\ospp.vbs"" /inpkey:xxx:xxx:xxx:xxx:xxx", 1, True

objShell.run "C:\psexec.exe -u " & strDomain & "\" & strUser & " -p " & strPassword & " -i ""C:\program files (x86)\microsoft office\office14\ospp.vbs"" /act", 1, True


Regards,

Rob.
0
 
LVL 2

Author Closing Comment

by:tetran_au
ID: 35220803
Sorry for the late response guys,  Rob i went with your advice and now have a prefectly formed menu script that will also allow me to activate my computers via user controlled proxy,


Thanks guys for all your support and assistance
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35224172
Cool. Thanks for the grade,

Regards,

Rob.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

746 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