Can anybody please help me make the vbScript more resilient & better logging
'*******************************************************
' - Join Standalone Machine to domain
' - Place computer into specific OU
' Preconditions:
' - strAccount must be delegated permissions to the OU to create new computer objects.
'*******************************************************
Option Explicit
'On Error Resume Next
'*******************************************************
' Script variables
'*******************************************************
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const strAccount = "PROD\SVCSCCMPC"
Const strPassword = "*******"
Const strDomain = "PROD"
Const strDNSDomain = "prod.domain.com"
Const strOU = "OU=Standard,OU=PCs,DC=prod,DC=domain,DC=Com"
Const strServerName = "prodldap.prod.domain.com"
Dim objFSO
Dim objFile
Dim WshShell
Dim strSysDir
Dim objNetwork
Dim strComputer
Dim ReturnValue
Dim objSWbemLocator
Dim objComputer
Dim strErrorDescription
Dim objReg
Dim objLocator
Dim objService
Dim colPing
Dim objCol
Dim item
Dim bPing
Dim intAnswer
Dim strNTDomain
Dim grp
Dim strCurrentName
Dim strNewName
Dim strSerial
'*******************************************************
' Create Log Files
'*******************************************************
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
strSysDir = wshShell.ExpandEnvironmentStrings("%systemdrive%")
If Not objFSO.FolderExists(strSysDir & "\NCS") Then
objFSO.CreateFolder(strSysDir & "\NCS")
End If
Set objFile = objFSO.CreateTextFile(strSysDir & "\NCS\Join_Domain.txt",True)
'**************************************************************************
' Regwrite - Code
'**************************************************************************
'WshShell.RegWrite "HKLM\SYSTEM\Setup\SystemSetupInProgress", 0, "REG_DWORD"
'*******************************************************
' WMI Connection
'*******************************************************
strComputer = "."
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objService = objLocator.ConnectServer(strComputer, "root\cimv2")
If Err.Number = 0 Then
Log "Object Locator - Connection to service successful."
Else
Log "Error: Object Locator " & Err.Number & " - " & Err.Description
Err.Clear
End If
'*************************************************************
' Get the computer name
'*************************************************************
Set objCol = objService.ExecQuery("Select * from Win32_ComputerSystem")
'
For Each Item In objCol
strComputer = item.name
Next
Set objCol = Nothing
strCurrentName = strComputer
log "Old Name: " & strCurrentName
'*******************************************************
' Get the Serial Number
'*******************************************************
Set objCol = objService.ExecQuery("Select * from Win32_BIOS")
For Each Item In objCol
Log "Serial Number: " & Item.SerialNumber
strSerial = item.SerialNumber
Next
'*******************************************************
' Declare new name
'*******************************************************
strNewName = "PC" & strSerial
log "New Name: " & strNewName
'*******************************************************
' Set the new PC name
'*******************************************************
'WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", strNewName, "REG_SZ"
'WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\ActiveComputerName", strNewName, "REG_SZ"
'WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Hostname", strNewName, "REG_SZ"
'WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\ialm\Device0\NV Hostname", strNewName, "REG_SZ"
'If Err.Number = 0 Then
' Log "PC Renamed Successfully with " & strNewName
'Else
' Log "Error: PC was not renamed successfully"
' Err.Clear
'End If
'**************************************************************************
' Start Ping check
'**************************************************************************
Log "*********************************************************************"
Log "Network Connectivity Test"
Log "*********************************************************************"
bPing = False
while bPing = False
Set colPing = objService.ExecQuery("Select * from Win32_PingStatus where address = '" & strServerName & "'",,48)
For Each Item In colPing
if item.StatusCode = 0 then
' Ping success
Log "Ping success"
bPing = True
else
' Move on afterwards. Hopefully it will work or it may need to be manually added
msgbox "Network Test Failed. Setup cannot continue without a network connection." & VBCRLF & VBCRLF & "Please connect the PC to the network, wait for 30 seconds and click OK."
end if
next
wend
'*******************************************************
' Start - Join Computer to the Domain.
'*******************************************************
log "**************************************"
log "Domain join:"
log "**************************************"
err.clear
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2:Win32_ComputerSystem.Name='" & strNewName & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDNSDomain, strPassword, strAccount, strOU, JOIN_DOMAIN + ACCT_CREATE)
If Err.Number = 0 Then
Log "Domain Join Success."
Else
Log "Error: Domain Join Error " & Err.Number & " - " & Err.Description
Err.Clear
End If
'*******************************************************
' List of 'system' and 'network management' error codes
'*******************************************************
Select Case ReturnValue
Case 0 strErrorDescription = "Success joining computer to the domain!"
Case 5 strErrorDescription = "Access is denied"
Case 87 strErrorDescription = "The parameter is incorrect"
Case 110 strErrorDescription = "The system cannot open the specified object"
Case 1323 strErrorDescription = "Unable to update the password"
Case 1326 strErrorDescription = "Logon failure: unknown username or bad password"
Case 1355 strErrorDescription = "The specified domain either does not exist or could not be contacted"
Case 1722 strErrorDescription = "The RPC server is unavailable"
Case 2224 strErrorDescription = "The account already exists"
Case 2691 strErrorDescription = "The machine is already joined to the domain"
Case 2692 strErrorDescription = "The machine is not currently joined to a domain"
Case Else strErrorDescription = "Unknown error"
End Select
Log vbtab & "Domain Join - Return Value: " & returnvalue
Log vbtab & "Domain Join - Error Description:" & strErrorDescription
Log vbtab & "***************************************************"
'*******************************************************
' End - Join Computer to the Domain.
'*******************************************************
'*************************************************************
' Start - Add user to the local Administrators group
'*************************************************************
Log ""
Log vbtab & "**********************************************************"
Log vbtab & " Adding Interactive User to Local Administrators Group"
Log vbtab & "**********************************************************"
Set grp=getobject("WinNT://" & strComputer & "/Administrators,group")
if grp.ismember("WinNT://NT Authority/Interactive") Then
Log vbtab & "NT Authority/Interactive" & " is already a member!"
else
Log vbtab & "NT Authority/Interactive" & " is being added to the Administrators group!"
grp.add ("WinNT://" & "NT Authority/Interactive")
grp.setInfo
end if
if err.number <> 0 then
Log vbtab & "Err Adding " & "NT Authority/Interactive" & " to the administrators group: " & err.number & " - " & err.Description
Err.Clear
end If
'*************************************************************
' end - Add user to the local Administrators group
'*************************************************************
'**************************************************************************
' Added registry fixes
'**************************************************************************
Log "*********************************************************************"
Log "Writing VGOFix(CycleInterval) to registry"
Log "*********************************************************************"
wshShell.RegWrite "HKLM\SOFTWARE\Passlogix\Extensions\SyncManager\CycleInterval", 0, "REG_DWORD"
If Err.Number = 0 Then
Log "VGOFix(CycleInterval) value added to registry"
Else
Log "Error: VGOFix(CycleInterval) value did not add correctly"
Err.Clear
End If
Log "*********************************************************************"
Log "Writing VGOFix(MultiAuth) to registry"
Log "*********************************************************************"
wshShell.RegWrite "HKLM\SOFTWARE\Passlogix\AUI\MultiAuth\CheckForParentProcess", 0, "REG_DWORD"
If Err.Number = 0 Then
Log "VGOFix(MultiAuth) value added to registry"
Else
Log "Error: VGOFix(MultiAuth) value did not add correctly"
Err.Clear
End If
Log "**********************************************************"
Log "Disabling DLU in the registry"
Log "**********************************************************"
WshShell.RegWrite "HKLM\SOFTWARE\Novell\Workstation Manager\DLUAllowed", 0, "REG_DWORD"
If Err.Number = 0 Then
Log "DLUAllowed value added to registry"
Else
Log "Error: DLUAllowed value did not add correctly"
Err.Clear
End If
WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", "PROD", "REG_SZ"
If Err.Number = 0 Then
Log "DefaultDomainName value added to registry"
Else
Log "Error: DefaultDomainName value did not add correctly"
Err.Clear
End If
WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AltDefaultDomainName", "PROD", "REG_SZ"
If Err.Number = 0 Then
Log "AltDefaultDomainName value added to registry"
Else
Log "Error: AltDefaultDomainName value did not add correctly"
Err.Clear
End If
WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\CachePrimaryDomainName", "PROD", "REG_SZ"
If Err.Number = 0 Then
Log "CachePrimaryDomainName value added to registry"
Else
Log "Error: CachePrimaryDomainName value did not add correctly"
Err.Clear
End If
WshShell.RegWrite "HKLM\Software\Novell\Location Profiles\Services\{1E6CEEA1-FB73-11CF-BD76-00001B27DA23}\Default\Tab3\DefaultDomainName", "PROD", "REG_SZ"
If Err.Number = 0 Then
Log "DefaultDomainName (novell tab) value added to registry"
Else
Log "Error: DefaultDomainName (novell tab) value did not add correctly"
Err.Clear
End If
'*************************************************************
'
'*************************************************************
'WshShell.RegWrite "HKLM\SYSTEM\Setup\SystemSetupInProgress", 1, "REG_DWORD"
'*******************************************************
' Functions Section
'*******************************************************
Function Log(strText)
'wscript.echo strText
objFile.writeline strText
End function
'*******************************************************
' - Join Standalone Machine to domain
' - Place computer into specific OU
' Preconditions:
' - strAccount must be delegated permissions to the OU to create new computer objects.
'*******************************************************
Option Explicit
'On Error Resume Next
'*******************************************************
' Script variables
'*******************************************************
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const strAccount = "PROD\SVCSCCMPC"
Const strPassword = "*******"
Const strDomain = "PROD"
Const strDNSDomain = "prod.domain.com"
Const strOU = "OU=Standard,OU=PCs,DC=prod,DC=domain,DC=Com"
Const strServerName = "prodldap.prod.domain.com"
Dim objFSO
Dim objFile
Dim WshShell
Dim strSysDir
Dim objNetwork
Dim strComputer
Dim ReturnValue
Dim objSWbemLocator
Dim objComputer
Dim strErrorDescription
Dim objReg
Dim objLocator
Dim objService
Dim colPing
Dim objCol
Dim item
Dim bPing
Dim intAnswer
Dim strNTDomain
Dim grp
Dim strCurrentName
Dim strNewName
Dim strSerial
'*******************************************************
' Create Log Files
'*******************************************************
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
strSysDir = wshShell.ExpandEnvironmentStrings("%systemdrive%")
If Not objFSO.FolderExists(strSysDir & "\NCS") Then
objFSO.CreateFolder(strSysDir & "\NCS")
End If
Set objFile = objFSO.CreateTextFile(strSysDir & "\NCS\Join_Domain.txt",True)
'**************************************************************************
' Regwrite - Code
'**************************************************************************
'WshShell.RegWrite "HKLM\SYSTEM\Setup\SystemSetupInProgress", 0, "REG_DWORD"
'*******************************************************
' WMI Connection
'*******************************************************
strComputer = "."
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objService = objLocator.ConnectServer(strComputer, "root\cimv2")
If Err.Number = 0 Then
Log "Object Locator - Connection to service successful."
Else
Log "Error: Object Locator " & Err.Number & " - " & Err.Description
Err.Clear
End If
'*************************************************************
' Get the computer name
'*************************************************************
Set objCol = objService.ExecQuery("Select * from Win32_ComputerSystem")
'
For Each Item In objCol
strComputer = item.name
Next
Set objCol = Nothing
strCurrentName = strComputer
log "Old Name: " & strCurrentName
'*******************************************************
' Get the Serial Number
'*******************************************************
Set objCol = objService.ExecQuery("Select * from Win32_BIOS")
For Each Item In objCol
Log "Serial Number: " & Item.SerialNumber
strSerial = item.SerialNumber
Next
'*******************************************************
' Declare new name
'*******************************************************
strNewName = "PC" & strSerial
log "New Name: " & strNewName
'*******************************************************
' Set the new PC name
'*******************************************************
'WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", strNewName, "REG_SZ"
'WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\ActiveComputerName", strNewName, "REG_SZ"
'WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Hostname", strNewName, "REG_SZ"
'WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\ialm\Device0\NV Hostname", strNewName, "REG_SZ"
'If Err.Number = 0 Then
' Log "PC Renamed Successfully with " & strNewName
'Else
' Log "Error: PC was not renamed successfully"
' Err.Clear
'End If
'**************************************************************************
' Start Ping check
'**************************************************************************
Log "*********************************************************************"
Log "Network Connectivity Test"
Log "*********************************************************************"
bPing = False
while bPing = False
Set colPing = objService.ExecQuery("Select * from Win32_PingStatus where address = '" & strServerName & "'",,48)
For Each Item In colPing
if item.StatusCode = 0 then
' Ping success
Log "Ping success"
bPing = True
else
' Move on afterwards. Hopefully it will work or it may need to be manually added
msgbox "Network Test Failed. Setup cannot continue without a network connection." & VBCRLF & VBCRLF & "Please connect the PC to the network, wait for 30 seconds and click OK."
end if
next
wend
'*******************************************************
' Start - Join Computer to the Domain.
'*******************************************************
log "**************************************"
log "Domain join:"
log "**************************************"
err.clear
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2:Win32_ComputerSystem.Name='" & strNewName & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDNSDomain, strPassword, strAccount, strOU, JOIN_DOMAIN + ACCT_CREATE)
If Err.Number = 0 Then
Log "Domain Join Success."
Else
Log "Error: Domain Join Error " & Err.Number & " - " & Err.Description
Err.Clear
End If
'*******************************************************
' List of 'system' and 'network management' error codes
'*******************************************************
Select Case ReturnValue
Case 0 strErrorDescription = "Success joining computer to the domain!"
Case 5 strErrorDescription = "Access is denied"
Case 87 strErrorDescription = "The parameter is incorrect"
Case 110 strErrorDescription = "The system cannot open the specified object"
Case 1323 strErrorDescription = "Unable to update the password"
Case 1326 strErrorDescription = "Logon failure: unknown username or bad password"
Case 1355 strErrorDescription = "The specified domain either does not exist or could not be contacted"
Case 1722 strErrorDescription = "The RPC server is unavailable"
Case 2224 strErrorDescription = "The account already exists"
Case 2691 strErrorDescription = "The machine is already joined to the domain"
Case 2692 strErrorDescription = "The machine is not currently joined to a domain"
Case Else strErrorDescription = "Unknown error"
End Select
Log vbtab & "Domain Join - Return Value: " & returnvalue
Log vbtab & "Domain Join - Error Description:" & strErrorDescription
Log vbtab & "***************************************************"
'*******************************************************
' End - Join Computer to the Domain.
'*******************************************************
'*************************************************************
' Start - Add user to the local Administrators group
'*************************************************************
Log ""
Log vbtab & "**********************************************************"
Log vbtab & " Adding Interactive User to Local Administrators Group"
Log vbtab & "**********************************************************"
Set grp=getobject("WinNT://" & strComputer & "/Administrators,group")
if grp.ismember("WinNT://NT Authority/Interactive") Then
Log vbtab & "NT Authority/Interactive" & " is already a member!"
else
Log vbtab & "NT Authority/Interactive" & " is being added to the Administrators group!"
grp.add ("WinNT://" & "NT Authority/Interactive")
grp.setInfo
end if
if err.number <> 0 then
Log vbtab & "Err Adding " & "NT Authority/Interactive" & " to the administrators group: " & err.number & " - " & err.Description
Err.Clear
end If
'*************************************************************
' end - Add user to the local Administrators group
'*************************************************************
'**************************************************************************
' Added registry fixes
'**************************************************************************
Log "*********************************************************************"
Log "Writing VGOFix(CycleInterval) to registry"
Log "*********************************************************************"
wshShell.RegWrite "HKLM\SOFTWARE\Passlogix\Extensions\SyncManager\CycleInterval", 0, "REG_DWORD"
If Err.Number = 0 Then
Log "VGOFix(CycleInterval) value added to registry"
Else
Log "Error: VGOFix(CycleInterval) value did not add correctly"
Err.Clear
End If
Log "*********************************************************************"
Log "Writing VGOFix(MultiAuth) to registry"
Log "*********************************************************************"
wshShell.RegWrite "HKLM\SOFTWARE\Passlogix\AUI\MultiAuth\CheckForParentProcess", 0, "REG_DWORD"
If Err.Number = 0 Then
Log "VGOFix(MultiAuth) value added to registry"
Else
Log "Error: VGOFix(MultiAuth) value did not add correctly"
Err.Clear
End If
Log "**********************************************************"
Log "Disabling DLU in the registry"
Log "**********************************************************"
WshShell.RegWrite "HKLM\SOFTWARE\Novell\Workstation Manager\DLUAllowed", 0, "REG_DWORD"
If Err.Number = 0 Then
Log "DLUAllowed value added to registry"
Else
Log "Error: DLUAllowed value did not add correctly"
Err.Clear
End If
WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", "PROD", "REG_SZ"
If Err.Number = 0 Then
Log "DefaultDomainName value added to registry"
Else
Log "Error: DefaultDomainName value did not add correctly"
Err.Clear
End If
WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AltDefaultDomainName", "PROD", "REG_SZ"
If Err.Number = 0 Then
Log "AltDefaultDomainName value added to registry"
Else
Log "Error: AltDefaultDomainName value did not add correctly"
Err.Clear
End If
WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\CachePrimaryDomainName", "PROD", "REG_SZ"
If Err.Number = 0 Then
Log "CachePrimaryDomainName value added to registry"
Else
Log "Error: CachePrimaryDomainName value did not add correctly"
Err.Clear
End If
WshShell.RegWrite "HKLM\Software\Novell\Location Profiles\Services\{1E6CEEA1-FB73-11CF-BD76-00001B27DA23}\Default\Tab3\DefaultDomainName", "PROD", "REG_SZ"
If Err.Number = 0 Then
Log "DefaultDomainName (novell tab) value added to registry"
Else
Log "Error: DefaultDomainName (novell tab) value did not add correctly"
Err.Clear
End If
'*************************************************************
'
'*************************************************************
'WshShell.RegWrite "HKLM\SYSTEM\Setup\SystemSetupInProgress", 1, "REG_DWORD"
'*******************************************************
' Functions Section
'*******************************************************
Function Log(strText)
'wscript.echo strText
objFile.writeline strText
End function