Postprepped, with some comments/changes


;=========================================================================================== 
; 
;	Title:   Windows XP Reboot of network clients 
;	Author:  Indigo 
;	Description:  Sends out emails at 7 Days, and Reboots at 10 
;	Version: .1 
; 
; =========================================================================================== 
 
BREAK on
 
 
; Need to declare variables! 
Dim $Var			; what it's used for 
 
 
 
; Need to set Kix Options (SetOption definitions) 
$ = SetOption('Explicit', 'On')
$ = SetOption('NoVarsInStrings', 'On')
$ = SetOption('NoMacrosInStrings', 'On')
$ = SetOption('WrapAtEOL', 'On')
 
 
 
; Define the Kix UDF library path once and reference it 
$KixLib = "\\server\share\path"
 
; include external UDFs from the standard kix lib path 
Call $KixLib + "\Comnetview.udf"
Call $KixLib + "\WmiUptime.udf"
Call $KixLib + "\Translatename.udf"
Call $KixLib + "\Ping.udf"
Call $KixLib + "\FnWMIloggedin.udf"
Call $KixLib + "\WMIConfirm.udf"
 
 
; Define settings - These might be best defined in an external INI file - use ReadProfileString to read them 
;$DontReboot  = Split(ReadProfileString('wsboot.ini', 'COMMON', 'NoReboot'), ',') 
;$Userstoskip = Split(ReadProfileString('wsboot.ini', 'COMMON', 'SkipUsers'), ',') 
;$EmailServer = ReadProfileString('wsboot.ini', 'COMMON', 'MailServer') 
;$EmailSufix  = ReadProfileString('wsboot.ini', 'COMMON', 'MailDomain') 
;RS_Logs      = ReadProfileString('wsboot.ini', 'COMMON', 'LogDrive') 
;IT_EMail     = ReadProfileString('wsboot.ini', 'COMMON', 'MailTo') 
 
$DontReboot = "Computer Name Here" , "Computer Name Here"
$Userstoskip = "User Name Here","User Name Here"
$EmailServer = "Email Server System Name" ; For email features to work you need Bmail and an open authentication turned on at your email server. 
$EmailSufix = "Company.com"
 
; == Intializing Log File Locations & IT - Email == 
$RS_Logs = "C:" 		    ; Don't use the \ because it won't work right. 
$IT_Email = "it@company.com"        ; Must have Double @@ for it to work right UNLESS YOU USE NOMACROSINSTRINGS option!. 
 
 
; == Clears Log Files If Script has been run before ==  
 
del "$RS_Logs\Systems_UpTime.txt"							
del "$RS_Logs\Systems_Offline.txt"		
 
$computers = COMNetView()			; == Locates computers on the network  
 
For Each $computer in $computers
 
  ;? "Pinging: " $computer 
  $online = Ping($computer,0,0,1)	; == Ping confirms computer online 
  If $online = "1"
    $winver = ReadValue("\\" + $computer + "\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion","ProductName")
    ;? "Reading OS of " $computer " ( " $winver " )" 
    If $winver = "Microsoft Windows XP"
      $WMICheck = WMIConfirm('$computer')
        If $WMICheck <> 0
          $UpTime = WMIuptime($computer)
 
          ; == Displays Current System Information 
          "System: " + $computer +" (Online)" ?
          "OS: ( " $winver " )" ?
          "Uptime: " + $Uptime[0] + " Days" ?
          ; -------------------------------------- 
 
          If len(ltrim($uptime[0])) <> 0		; == Checks digit value of Uptime to see if it is 0 or not 
            If Systemcheck($computer) = -1
 
              If $Uptime[0] >= 7
                If RedirectOutput ("$RS_Logs\Systems_UpTime.txt") = 0
                  $Userloggedin = GetloginName($computer)
                  ? $computer + "'s Uptime is: " + $Uptime[0] + " Days.  " + $Userloggedin ?
                  $ = RedirectOutput("")
                EndIf
              EndIf
 
              Select
                Case $Uptime[0] >= 0 and $Uptime[0] < 7
 
                  If Exist ("$RS_Logs\$computer.SentEmail.txt")
                    ? "Email Tag File found Deleting .... "?
                    del "$RS_Logs\$computer.SentEmail.txt"
                  Endif
                  ?
                Case $Uptime[0] >= 7 and $UpTime[0] < 10
 
                  ? "System is being processed."
                  $uptimex = $Uptime[0]
                  WriteEmail($computer,$uptimex)
                  $Userloggedin = GetloginName($computer)
                  SendEmail($userloggedin,$computer,$uptimex)
                  ? "Writing Log File, and Sending Email"
 
                Case $Uptime[0] >= 10
 
                  If Exist ("$RS_Logs\$computer.SentEmail.txt")
                    ? $computer + " is being rebooted.." + @CRLF
                    ;Shutdown ($computer, "Please Save Your Work,  This System Is Being Rebooted in 1 min.",60,1,1) 
                  Else
                    ? "Writing Log File, and Sending " + $userloggedin + " an Email to reboot their system."
                    $uptimex = $Uptime[0]
                    WriteEmail($computer,$uptimex)
                    $Userloggedin = GetloginName($computer)
                    SendEmail($userloggedin,$computer,$uptimex)
                    ? $computer + " Will not be Rebooted.." + @CRLF
                  Endif
 
              Endselect
            Else
              ? "System is in Protected List.."+ @CRLF + @CRLF
            EndIf
          Else
          ? "Uptime Check Failed!!!" + @CRLF
            If RedirectOutput ("$RS_Logs\System_UptimeChck_Failed.txt") = 0
              ?  "Uptime on: " + $computer + " Failed!!"
              $ = RedirectOutput("")
            EndIf
          EndIf
        Else
          ? "$computer was skipped & Logged for review. WmiReturn Code: " + $WMICheck
          If RedirectOutput ("$RS_Logs\Errorlog.txt") = 0
            ? $computer + "'s Error Code is " + @error + " - " + @SERROR
            ? "This system was skipped & Logged for review. WmiReturn Code: " + $WMICheck
            $ = RedirectOutput("")
          EndIf
        Endif
    EndIf
  Else
    If RedirectOutput ("$RS_Logs\Systems_Offline.txt") = 0
      $computer ?
      $ = RedirectOutput("")
    EndIf
  Endif
Next
; ----- System Reboot Email with attached System Uptimes Log ----- 
  ? "Sendig admin email of systems with errors..."
  Shell 'bmail -s $EmailServer -t $IT_Email -f $IT_Email -a "Systems That need to be rebooted" -m $RS_Logs\Systems_uptime.txt'
  ;del "$RS_logs\Errorlog.txt" 
 
; ================== Functions Below This Point ==================== 
 
;----- This function just checks to make sure we don't reboot a pc that needs to stay up. -- 
Function Systemcheck($chkcomputer)
 
  $result = 0
  $chkcomputer = TRIM(LCASE($chkcomputer))
  $result = Ascan($DontReboot,$chkcomputer,0)
  $systemcheck = $result
 
EndFunction
 
; ---- Removes invalid email users ---- 
Function checkusers($users)
 
  $result = 0
  $chkuser = TRIM(LCASE($users))
  $result = Ascan($Userstoskip,$users,0)
  $checkusers = $result
 
EndFunction
 
;----- Pulls in the user that is logged into X machine  ---------------------------- 
Function GetloginName($chkcomputers)
 
  $usrloggedin = fnWMILoggedin (1,'$chkcomputers')			; == Current user logged in. 
  $usrname = substr ($usrloggedin,6)					; == Removes "Domain\" Domain. 
  $Getloginname = $usrname						; == Return Value. 
 
EndFunction
 
 
;----- Sends an email to the user whos system has been up for more than 7 days, and sends email to administrator. 
Function SendEmail($user,$system,$systemup)
If len(ltrim($user)) <> 0	; == This checks to make sure that a 0 digit value didn't show up for $User 
  If checkusers($user) = -1
    Shell 'bmail -s $EmailServer -t $user@@$EmailSufix -f $IT_Email -a "(System Reboot Needed!) Uptime: $systemup Days." -m $RS_Logs\$system.SentEmail.txt'
  Else
    ;Shell 'bmail -s $EmailServer -t $IT_Email -f $IT_Email -a "$system - $user Uptime: $systemup Days." -m $RS_Logs\$system.SentEmail.txt' 
    ;Shell '%compsec% net send \\$system "Alert Notice: This system has been turned on for $systemup days please reboot!, Thanks IT"' 
  Endif
Else
  $Unli = "(User Not Logged In)"
  Shell 'bmail -s $EmailServer -t $IT_Email -f $IT_Email -a "$system - $Unli System Reboot Needed! It Has been Turned on for $systemup Days." -m $RS_Logs\$system.SentEmail.txt'
  ;Shell '%compsec% net send \\$system "Alert Notice: This system has been turned on for $systemup days please reboot!, Thanks IT"' 
Endif
EndFunction
 
 
;----- Writes the email file that is included in the body of the email sent. ------------ 
Function WriteEmail($cmpt,$sysup)
 
  If RedirectOutput ("$RS_Logs\$cmpt.SentEmail.txt",1) = 0  ; == Creates a Log File to indicate the sending of an email. 
    ? $cmpt + "'s Uptime is: " + $sysup + " Days."
    ?
    ? "This is the Body of the Email that will be sent... Fill it in with whatever you want."
 
EndFunction
 


Edited by Lonkero (2008-10-22 09:11 PM)
Edit Reason: LONG LINE POLICE
_________________________
Actually I am a Rocket Scientist! \:D