<job>
<script language="VBScript"
    src="\\ScriptServer\Scripts\Libraries\Generic Library.vbs"/>
<script language="VBScript"
    src="\\ScriptServer\Scripts\Libraries\CUS Library.vbs"/>
<script language="VBScript">
BEGIN COMMENT
' *****************************************************************
' TITLE  : SimpleCUSClientTargetter.wsf
' INFO   : Installs CUS setup files on each client.
' INPUT  : None
' OUTPUT : None
' *****************************************************************
END COMMENT
Option Explicit
On Error Resume Next

BEGIN COMMENT
' Declare and initialize variables.
END COMMENT
Dim fso, strADOFilter, arrPCs(), bolADSearch
Dim bolGCSE, arrSuccess(), arrFailed(), filLog
Dim strLogFilePath, strPC, intIndex, strSummary
Dim intSuccess, intGrpAddSuccess, intGrpAddFail, adsMgmtGroup
Dim intFailedUnreachable, intFailedInstallScript, intReturn

bolADSearch = FALSE
intSuccess = 0
intGrpAddSuccess = 0
intGrpAddFail = 0
intFailedUnreachable = 0
intFailedInstallScript = 0
strSummary = ""
strLogFilePath = _ &
  CUS_INSTALL_LOGFILE_PATH & CreateFilenameFromTimestamp() & ".TXT"
Set adsMgmtGroup = GetObject(MGMT_GRP_ADSPATH)

BEGIN COMMENT
' Open and initialize the main log file.
END COMMENT
Set fso = CreateObject("Scripting.FileSystemObject")
Set filLog = fso.OpenTextFile(strLogFilePath, _
  & FSO_OPEN_FOR_WRITING, FSO_OPEN_CREATE)
filLog.WriteLine SEPARATOR
filLog.WriteLine "START OF SCRIPT RUN"
filLog.WriteLine SEPARATOR
filLog.WriteLine "TIMESTAMP                    : " & Now()
filLog.WriteLine "SCRIPT PATH                  : " _
  & WScript.ScriptFullName
filLog.WriteLine "USER SCRIPT WAS RUN UNDER    : " _
  & GetCurrentUsername()
filLog.WriteLine "COMPUTER SCRIPT WAS RUN FROM : " _
  & GetCurrentComputername()
filLog.WriteLine SEPARATOR

BEGIN COMMENT
' Retrieve IP names of all workstations not yet under CUS control.
END COMMENT
strADOFilter = "(& (objectClass=Computer)(!(memberOf=" _
  & MGMT_GRP & ")))"
  
bolADSearch = SearchAD(AD_ROOT, strADOFilter, "SubTree", _
  & "dNSHostName, ADsPath", arrPCs)
  
If bolADSearch Then
  BEGIN COMMENT
  ' Log the success.
  END COMMENT
  filLog.WriteLine "AD Search successful."
  filLog.WriteLine SEPARATOR
  BEGIN COMMENT
  ' Attempt to run script on each client.
  END COMMENT
  For intIndex = 0 To UBound(arrPCs)
    strPC = arrPCs(intIndex,0)
	  If IsHostReachable(strPC) Then
	  BEGIN COMMENT
	  ' PC is reachable, so run script against strPC.
	  END COMMENT
      intReturn = CUSClientFilesInstaller(strPC)
	  If intReturn = 0 Then
       BEGIN COMMENT
	    ' Add PC to successes and to group.
		END COMMENT
		filLog.WriteLine "(" & strPC & _
		  ") installation script successful [" & Now() & "]."
		intSuccess = intSuccess + 1
        Err.Clear
        adsMgmtGroup.Add arrPCs(intIndex,1)
		If Err = 1 Then
          intGrpAddFail = intGrpAddFail + 1
          filLog.WriteLine vbTab & "FAILED TO ADD TO GROUP."
        Else
          intGrpAddSuccess = intGrpAddSuccess + 1
          filLog.WriteLine vbTab & "SUCCESS IN ADDING TO GROUP."
        End If
      Else
        BEGIN COMMENT
        ' intReturn was 1, so add PC to failures.
        END COMMENT
        filLog.WriteLine "(" & strPC & ") install script failed ["_
          & Now() & "]."
        intFailedInstallScript = intFailedInstallScript + 1
      End If
    Else
      BEGIN COMMENT
      ' PC isn't reachable, so update failures.
    END COMMENT
      filLog.WriteLine "(" & strPC & ") was unreachable [" _
        & Now() & "]."
      intFailedUnreachable = intFailedUnreachable + 1
    End If
  Next
Else
  BEGIN COMMENT
  ' Log the failure.
  END COMMENT
  filLog.WriteLine "AD Search failed"
  filLog.WriteLine SEPARATOR
End If

BEGIN COMMENT
' Finalize the log file.
END COMMENT
If Not bolADSearch Then
  strSummary = SEPARATOR & "TOTALS" & SEPARATOR & vbCrLf _
    & "PCs FOUND IN AD THAT WERE NOT IN (" _
    & MGMT_GRP & ") : " & UBound(arrPCs) & vbCrLf _
	& "SUCCESSFUL INSTALLATIONS : " & intSuccess & vbCrLf _
    & "SUCCESSFUL INSTALLATIONS ALSO SUCCESSFULLY ADDED TO " _
    & "GROUP : " & intGrpAddSuccess & vbCrLf _
    & "SUCCESSFUL INSTALLATIONS THAT FAILED TO ADD TO GROUP: " _
    & intGrpAddFail & vbCrLf _
    & "FAILED DUE TO BEING UNREACHABLE : " _
    & intFailedUnreachable & vbCrLf _
    & "FAILED DUE TO INSTALLATION FAILURE : " _
    & intFailedInstallScript & vbCrLf
  filLog.WriteLine strSummary
  filLog.WriteLine SEPARATOR
End If

filLog.WriteLine "END OF SCRIPT RUN"
filLog.WriteLine SEPARATOR
filLog.WriteLine "TIMESTAMP : " & Now()
filLog.WriteLine SEPARATOR
filLog.Close

BEGIN COMMENT
' Email the results.
END COMMENT
SendMailToAdmins "CUS Installer Results - Please check", _
  & "See attached file for full details: " _
  & "(" & strLogFilePath & ")", strLogFilePath
  
</script>
</job>