I am editing this first post, so i can Keep the current version of this script as the first post. \:\)

Version .6 Changes:
------> Auto Reboot is Turned OFF By Default You must Un-Comment that line!!!! (Line 130) <-----------
* I have added a Checking Function to make sure that the config file (RS_Config.ini) does exist. If not it prompts for needed info.
* I have also produced a section of code that will write all imputed data to a perfectly formated RS_Config.ini file.
* Please Note When Filling in the Log & Udfs path.... NO TRAILING \ is needed \:\)
* I also wanted to bring up "For the Email function to work you need to have Bmail"

Version .5 Changes:
* I added the config file portion to this script it will now import all the configuration information from the configfile, and assign it to the correct variables.
Config File Name must be RS_Config.ini, and it must be placed in the location where the script is run from.

 Code:
[CONFIG]
Kixlib=Kix_Lib_Path
Logs=Reboot_Script_Log_Locaion
NoReboot=PC1,PC2,PC3
SkipUsers=User1,User2,User3
EmailServer=Hostname_OF_EmailServer(emailserver)
EmailSuffix=Email_Suffix_(company.com)
AdminEmail=IT_Email_Address(it@comapny.com)


* I have also put some color into the output display just for Giggles..:)

Version .4 Changes:
* I made the script more agressive, and it now reboots any system not logged into over the 7 day marker.

Version .3 Changes:
* I have corrected a bug that wouldn't check (User Not Logged In) and send a net message to X machine.
* Glenn helped me correct the issue with the editing out the Domain\ out of the Domain\username.
* I have also fixed the Net Send Message now. It works as intended.. Sends a net message to every user in the Userstoskip array. Reason for the net send is we have some generic users that don't apply to anyones email address, This will send the net send reboot alert message, so these systems arn't skipped from being notified.

Future Code Changes:

* Pull email address without depending on the user being logged in to send the email.
* I would like to have this program to learn how to link systems with users, but this falls under not having a better way to configure how emails are being calcualted and sent out. (Wink Wink...)

Ok thats it for now.

 Code:
; ===========================================================================================
;
;	Title:   Windows XP Reboot of network clients
;	Author:  Indigo			
;	Description:  Sends out emails at 7 Days, and Reboots at 10	
;	Version: .6
;
;	Special Thanks: Glenn, I wanted to say thanks for all the Key help when needed to overcome some hurdles in getting this script this far.
;
;	Revision Notes:
;{Version .6} ------> Auto Reboot is Turned OFF By Default You must Un-Comment that line!!!!  (Line 130) <-----------
;	* I have added a Checking Function to make sure that the config file (RS_Config.ini) does exist.  If not it prompts for needed info.
;	* I have also produced a section of code that will write all imputed data to a perfectly formated RS_Config.ini file.
;	* Please Note When Filling in the Log & Udfs path.... NO TRAILING \ is needed :)
;	* I also wanted to bring up "For the Email function to work you need to have Bmail"
;{Version .5}	
;	* I added the Ability to read from a config file.  The config file must remain in the location from where the script is called InI file is RS_Config.ini	
;	* I have also added some color to the Current Status Display, and I moved it to its own function.
;	* I also made the Script more agressive when it comes to users that arn't logged in, It will auto reboot those systems instead of sending text message.
;	* Note: the system will still do a Net Send Messages to the current system being proccess for any generic users that are defined in the Users to skip section. 
;
; ===========================================================================================
CLS
; == Seting Kixtart Options == 

$Rc = SetOption('Explicit', 'Off')
$Rc = SetOption('NoVarsInStrings', 'On')
$Rc = SetOption('NoMacrosInStrings', 'On')
$Rc = SetOption('WrapAtEOL', 'On')

Break on

; == Declaring Variables. == 

Dim $DontReboot		; == Systems that won't be rebooted
Dim $UserstoSkip	; == Users that will be skipped
Dim $EmailServer	; == The Hostname of the Email Server
Dim $EmailSufix		; == The "company.com" in the Email addresses
Dim $RS_Logs		; == The Log Location \\server\share\path\
Dim $IT_Email		; == The Administrator Email
Dim $Rc				; == Return Code for Kixtart Options
Dim $Computers		; == An Array that holds all the Computers Comnetview gathers
Dim $Computer		; == Each Compuer in Computers
Dim $Online			; == Systems Online / Not Online
Dim $Winver			; == Windows Version
Dim $WMICheck		; == WmiCheck pass/fail
Dim $Uptime			; == Uptime in Days IE. 10
Dim $Kixlib			; == Kix Library location

$CfgFile = @ScriptDir + "\RS_Config.ini"	; Defines the Script Location.

CheckforConfigFile($CfgFile)

; == Intializing  Computer & User Special Consideration Veriables == ***** Variables below must be changed to needed Values ******

$DontReboot  = Split(ReadProfileString($CfgFile, 'CONFIG', 'NoReboot'), ',')	; Computers to Be Skipped
$Userstoskip = Split(ReadProfileString($CfgFile, 'CONFIG', 'SkipUsers'), ',')	; Users to be Skipped
$EmailServer = ReadProfileString($CfgFile, 'CONFIG', 'EmailServer') 			; HostName of Email Server
$EmailSufix  = ReadProfileString($CfgFile, 'CONFIG', 'EmailSuffix')				; Sufix of Email Address Company.com

$RS_Logs = ReadProfileString($CfgFile, 'CONFIG', 'Logs')				; Don't use the trailing \ because it won't work right
$IT_Email = ReadProfileString($CfgFile, 'CONFIG', 'AdminEmail')			; Email Address of System Admin or Group
$Kixlib = 	ReadProfileString($CfgFile, 'CONFIG', 'Kixlib')				; Define Where your Kixtart Lib is here Do not place a trailing \

; === Includes all Udfs needed for this script ===				 	**********************************************************

 Call $Kixlib + "\Comnetview.udf"
 Call $Kixlib + "\WmiUptime.udf"
 Call $Kixlib + "\Translatename.udf"
 Call $Kixlib + "\Ping.udf"
 Call $Kixlib + "\FnWMIloggedin.udf"
 Call $Kixlib + "\WMIConfirm.udf"

; == 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 
CLS
For Each $computer in $computers											
	$online = Ping($computer,0,0,1)	; == Ping confirms computer online
	If $online = "1"														
		$winver = ReadValue("\\" + $computer + "\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion","ProductName")
		If $winver = "Microsoft Windows XP" 								
			$WMICheck = WMIConfirm($computer)
				If $WMICheck <> 0										
					$UpTime = WMIuptime($computer)
					$Userloggedin = GetloginName($Computer)	
					
					
					DisplayInfo($Computer,$Winver,$Userloggedin,$Uptime); == This Function displays the current system info
					
					
					If len(ltrim($Uptime[0])) <> 0						; == Checks digit value of Uptime to see if it is 0 or not
						If Systemcheck($Computer,$DontReboot) = -1		; == Checks to make sure system isn't in the Protected list
							
							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 .... < 7 " + @CRLF
										del $RS_Logs + "\"+ $computer + ".SentEmail.txt"
									Endif
									@CRLF
									
								Case $Uptime[0] >= 7 and $UpTime[0] < 10
									
									If $Userloggedin = "(User Not Logged In)"
										@CRLF + $Computer + " Is being rebooted.. Because no one is logged in." + @CRLF
										Shutdown ($computer, "This System Is Being Rebooted in 3 seconds....",3,1,1)
									Else	
										"System is being processed." + @CRLF
										WriteEmail($computer,$Uptime[0],$RS_Logs)			
										SendEmail($Userloggedin,$Computer,$Uptime[0],$EmailServer,$IT_Email,$EmailSufix,$RS_Logs,$Userstoskip,0)
									EndIf
								
								Case $Uptime[0] >= 10 										
									
									If Exist ($RS_Logs + "\" + $Computer + ".SentEmail.txt")
										@CRLF + $Computer + " Is being rebooted.." + @CRLF
										;Shutdown ($computer, "Please Save Your Work,  This System Is Being Rebooted in 1 min.",60,1,1)
									Else
										WriteEmail($computer,$Uptime[0],$RS_Logs)
										SendEmail($Userloggedin,$Computer,$Uptime[0],$EmailServer,$IT_Email,$EmailSufix,$RS_Logs,$Userstoskip,0)
									Endif
							
							Endselect
						Else
							@CRLF + "This System Is In The Protected List..(Skipping)" + @CRLF
							Sleep 2
						EndIf
					Else
					@CRLF +  "Uptime Check Failed!!! (Skipping & Logging)" + @CRLF + "Logfile: " + $RS_Logs + "\System_UptimeChck_Failed.txt" + @CRLF
						If RedirectOutput ($RS_Logs + "\System_UptimeChck_Failed.txt") = 0 
							"Uptime Check On: " + $computer + " Failed!!" + @CRLF
							$ = RedirectOutput("")
						EndIf
					EndIf
				Else
					$computer + " was skipped & Logged for review. WmiReturn Code: " + $WMICheck + @CRLF
					If RedirectOutput ($RS_Logs + "\Errorlog.txt") = 0 
						$computer + "'s Error Code is " + @error + " - " + @SERROR + @CRLF
						"This system was skipped & Logged for review. WmiReturn Code: " + $WMICheck + @CRLF
						$ = RedirectOutput("")
					EndIf 
				Endif
		EndIf
	Else
		If RedirectOutput ($RS_Logs + "\Systems_Offline.txt") = 0 
			$computer + @CRLF
			$ = RedirectOutput("")
		EndIf
	Endif
Next
; ----- System Reboot Email with attached System Uptimes Log -----
	
	SendEmail($Userloggedin,$Computer,$Uptime[0],$EmailServer,$IT_Email,$EmailSufix,$RS_Logs,$Userstoskip,1)

; ================== Functions Below This Point ====================

; ----- This Function checks for a config file if not there prompts for the info --
Function CheckforConfigFile($_CfgFile)
	CLS
	"Checking For RS_Config.ini...." +@CRLF +@CRLF
	If Exist ($_CfgFile)
		"Config File Found .." + @CRLF + "Loading: " + $_CfgFile + @CRLF
		Sleep 5
		Return
	Else
		"Config File Not Found!! Prompting For Needed Data..." + @CRLF
		Sleep 3
		CLS
		"Please Enter The Location Of Where All Dependent Kix UDFs" + @CRLF
		"Format: C:\logs or \\Server\Share\Path" + @CRLF
		Gets $_Kixlib 					; Lib of Kixtart Udfs 
				
		CLS
		"Please Enter Where You Want To Store The Script Logs" + @CRLF
		"Format: C:\logs or \\Server\Share\Path" + @CRLF
		Gets $_RS_Logs					; Don't use the trailing \ because it won't work right
		
		CLS
		"Please Enter The Systems You Don't Want Rebooted" + @CRLF
		"Format: System,System,System" +@CRLF
		Gets $_DontReboot				; Computers to Be Skipped
	
		CLS
		"Please Enter Users You Don't Want Getting Emails" + @CRLF
		"Format: User,User,User" + @CRLF
		Gets $_Userstoskip				; Users to be Skipped
	
		CLS
		"Please Enter The Hostname Of The Email Server" + @CRLF
		"Format: hostname" + @CRLF
		Gets $_EmailServer				; HostName of Email Server
		
		CLS
		"Please Enter The Email Sufix For Your Companies Email Addresses" + @CRLF
		"Format: company.com" + @CRLF
		Gets $_EmailSuffix   			; Sufix of Email Address Company.com
				
		CLS
		"Please Enter The IT Email Address" + @CRLF
		"Format: it@company.com" + @CRLF
		Gets $_IT_Email 				; Email Address of System Admin or Group
		
		"Writing Config File......" + @CRLF
		If RedirectOutput ($_CfgFile,1) = 0 
			"[CONFIG]" + @CRLF
			"Kixlib=" + $_Kixlib + @CRLF
			"Logs="+ $_RS_Logs + @CRLF
			"NoReboot=" + $_DontReboot + @CRLF
			"SkipUsers=" + $_Userstoskip + @CRLF
			"EmailServer=" + $_EmailServer + @CRLF
			"EmailSuffix="+ $_EmailSuffix + @CRLF
			"AdminEmail=" + $_IT_Email + @CRLF
			$ = RedirectOutput("")
		EndIf 
		Sleep 2
	Endif

EndFunction

; ----- This function displays on the screen the current system information. --
Function DisplayInfo($_Computer,$_Winver,$_Userloggedin,$_Uptime)
	
	CLS
	color m+/n
	"System"color c+/n": " color g+/n  $_Computer color m+/n " ("color c+/n"Online" color m+/n  ")" + @CRLF
	"OS"color c+/n": "color g+/n  $_Winver + @CRLF
	color m+/n
	"User"color c+/n": " color g+/n $_Userloggedin + @CRLF
	color m+/n
	"Uptime"color c+/n": " color g+/n  $_Uptime[0]  color c+/n " Days" + @CRLF color w+/n 

EndFunction

;----- This function just checks to make sure we don't reboot a pc that needs to stay up. --
Function Systemcheck($_Chkcomputer,$_DontReboot)		

; == Declaring Variables
Dim $_Result		; Holds the Result of the calculation
Dim $_Chkcomputer	; Computer to be checked
Dim $_DontReboot 	; Global Variable Transfered to Function.

	$_Result = 0
	$_Chkcomputer = TRIM(LCASE($_Chkcomputer))					
	$_Result = Ascan($_DontReboot,$_Chkcomputer,0)				
	$Systemcheck = $_Result										

EndFunction

; ---- Removes invalid email users ----
Function checkusers($_Users,$_Userstoskip)		

; == Declaring Variables
Dim $_Result		; Holds the Result of the calculation
Dim $_Chkuser		; User to be checked
Dim $_Users			; User passed to function (raw)
Dim $_Userstoskip 	; Global Variable Transfered to Function.

	$_Result = 0
	$_Chkuser = TRIM(LCASE($_Users))					
	$_Result = Ascan($_Userstoskip,$_Chkuser,0)				
	$checkusers = $_Result
	
EndFunction

;----- Pulls in the user that is logged into X machine  ----------------------------
Function GetloginName($_Chkcomputers)

; == Declaring Variables
Dim $_Usrloggedin	; User Logged into current system in current format Domain\Username
Dim $_Usrname		; Just username by itself

$_Usrname = Split(fnWMILoggedIn(1,$_Chkcomputers), '\')[1] ; Extracts the username out of Domain\user to be just User.

If len(ltrim($_Usrname)) = 0	; == Checks for a 0 digit value assigned to user, if it did (User not logged) in is Returned.
	$_Unli = "(User Not Logged In)"
	$Getloginname = $_Unli
	Return
Endif

$Getloginname = $_Usrname

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,$_EmailServer,$_IT_Email,$_EmailSufix,$_RS_Logs,$_Userstoskip,$_AdminEmail)
; == Declaring Variables
Dim $_User			; User to be emailed
Dim $_System		; Hostname of current system
Dim $_Systemup		; Uptime of Current system
Dim $_Unli			; Defined as (User Not Logged In) - Incase user isn't logged in
Dim $_EmailSufix 	; Global Variable Transfered to Function.
Dim $_IT_Email 		; Global Variable Transfered to Function.
Dim $_RS_log		; Global Variable Transfered to Function.
Dim $_Email			; Building Block Variable for Sending Emails.
Dim $_NetSend		; Building Block of the NetSend Message.
					; Through a Variable for On/Off maybe setup a Debug Admin Feature, so only admin is emailed

$_Email = 'bmail'
$_Email = $_Email + ' -s '
$_Email = $_Email + $_EmailServer
$_Email = $_Email + ' -f ' + $_IT_Email	
	
	If $_AdminEmail = 1
		$_Email = $_Email + ' -t ' + $_IT_Email
		$_Email = $_Email + ' -a ' + '"Systems That need to be rebooted"'
		$_Email = $_Email + ' -m ' + $_RS_Logs + '\Systems_uptime.txt'
	Else
		$_Email = $_Email + ' -t ' + $_User
		$_Email = $_Email + ' -a ' + '"(System Reboot Needed!) Uptime: ' + $_Systemup + ' Days."'
		$_Email = $_Email + ' -m ' + $_RS_Logs + '\' + $_System + '.SentEmail.txt'
	EndIf

$_NetSend = 'net send '
$_NetSend = $_NetSend + $_System
$_NetSend = $_NetSend + ' "Reboot Notice: This system has been turned on for ' 
$_NetSend = $_NetSend + $_Systemup
$_NetSend = $_NetSend + ' days please reboot!, Thanks IT"'


If checkusers($_User,$_Userstoskip) = -1		; Makes sure certian users don't get emails.
	"Sending Email..." + @CRLF
	Shell $_Email
Else
	"Sending Net Send Message..." + @CRLF
	Shell $_NetSend
Endif

EndFunction


;----- Writes the email file that is included in the body of the email sent. ------------
Function WriteEmail($_Computer,$_Uptime,$_RS_Logs)

"Writing Log File.." + @CRLF

; == Declaring Variables
Dim $_Computer 		; Hostname of Current System
Dim $_Uptime		; Uptime of Current System
Dim $_RS_Logs		; Handles Log locations

	If RedirectOutput ($_RS_Logs + "\"+ $_Computer + ".SentEmail.txt",1) = 0  ; == Creates a Log File to indicate the sending of an email.
		@CRLF $_Computer + "'s Uptime is: " + $_Uptime + " Days."	
		@CRLF
		@CRLF "This is the Body of the Email that will be sent... Fill it in with whatever you want."
		@CRLF
		$ = RedirectOutput("")
	EndIf 

EndFunction


Edited by Indigo (2008-10-30 04:24 PM)