#190180 - 2008-10-20 09:15 PM
Re: Hi, All - (Where do i Post a Full Script I want to Share)
[Re: Indigo]
|
Indigo
Fresh Scripter

Registered: 2008-10-01
Posts: 34
|
Reason I Wrote this: (Here is the Basics..) (I am thankful for everyones help in making this script now I want to give back the result of my work.)
We have several users in our network that like to leave their systems on up until it starts doing funky things, and I have learned that it is possible through scripting to do a host of checks and make decisions based on those checks to email users that their systems have been up to long, and reboot those systems where the users are defiant and just won't reboot their systems.
The Script works like this: 1) Pulls a list of systems down from the domain. 2) It Pings the system to make sure its online if not the system is skipped. 3) It then checks to see if the system is "Windows XP" This is a simple way for me to filter out the servers. 4) Then it check the uptime of the current system. 5) It checks to make sure the Uptime returns a Good Value.
Note: I made this script to be able to skip systems that need to be left on. Note: The Script will also check the users logged in and if not logged in it will send an email to $IT_Email (IT Admins email) to let them know. Note: I have added in this portion of the code the ability to Do a Net Send message to the computer for when the system is logged into next.
6) Checks the system to make sure its not in the protected list. 7) If the system has been left on for >= 7 days it Creates an email and sends it to the user that was logged into the system being checked.
Note: It will send emails everyday until the system is rebooted. Note: IF the system has been on less than 7 Days it will delete the Semaphore file created as a marker used on some decisions in the script if it exists.
8) At >= 10 Days it will check to make sure they user has been emailed if not it skips the reboot and sends out a warning email. If the email has been sent it reboots the system. 9) At the End their is a email sent to $IT_Email with the systems that have been on more than 7 days. (For Follow up)
I would like to point out I am not a vet scripter, but I consolidated a few different Udfs and with a dash of my own code I have created a script that I think is very useful.
I also think an easier way to do this (I.e. Just Add to each system a reboot job in the Scheduled Tasks), but that would defeat the purpose of this exersise for me Also As far as the log files, they may not all be needed... (I have a little more work to do in that area, as well as increasing detail of log files as needed)
This is the very first rendition of this script desinged to use variables instead of hard coding the desired information like log locations, and IT_Email addresses.
I am sure there is many things that can be done to enhance this script beyond my current scripting ability which is part of the reason I would like to offer it up, so I can work on more advanced ideas.
My desire is to have people look at the script, and offer up ideas to add to the script, so the script will be lean, mean, effeciant with some nice features.
I would also like to remove the need to call an external emailing software.
To run the emailing portion of this script you will need the email software called Bmail. It is a command line emailer.
This Script Requies the following UDF Scripts to be Included. "Script Location" is the location of where your Udfs are placed:
WmiUptime gave me a Blank return on one system, but I adjusted for this, so I created a way to evaluate the Uptime variable and if it had a 0 digit value I discarded the system as failing the WmiUptime check. I am under the impression there is somthing wrong with the system because it is the only one that failes the WmiUptime check.
Include "\\Script Location\Comnetview.udf"
Include "\\Script Location\WmiUptime.udf"
Include "\\Script Location\Translatename.udf"
Include "\\Script Location\Ping.udf"
Include "\\Script Location\FnWMIloggedin.udf"
Include "\\Script Location\WMIConfirm.udf"
Below are the configurable Variables for different options in the script: I think the Variables below are self explanitory.
$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.
NOTE: I TURNED OFF THE SYSTEM REBOOT ACTION INCASE THE SCRIPT IS RUN DURING BUSINESS HOURS.
Review the segment below to locate this section in the code to turn it on.
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)
;===========================================================================================
;
; Title: Windows XP Reboot of network clients
; Author: Indigo
; Description: Sends out emails at 7 Days, and Reboots at 10
; Version: .1
;
; ===========================================================================================
BREAK on
; === Calls all Udfs needed for this script ===
Include "\\Script Location\Comnetview.udf"
Include "\\Script Location\WmiUptime.udf"
Include "\\Script Location\Translatename.udf"
Include "\\Script Location\Ping.udf"
Include "\\Script Location\FnWMIloggedin.udf"
Include "\\Script Location\WMIConfirm.udf"
; == Intializing Computer & User Special Consideration Veriables ==
$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.
; == 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."
$ = RedirectOutput("")
EndIf
Edited by Indigo (2008-10-20 09:44 PM)
|
|
Top
|
|
|
|
Client Reboot Script / Sends out Email warnings) Version .6
|
Indigo
|
2008-10-20 06:44 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-20 07:21 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Lonkero
|
2008-10-20 09:04 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-20 09:08 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-20 09:15 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Lonkero
|
2008-10-20 09:19 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-20 09:21 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-21 01:39 AM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-21 02:12 AM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
NTDOC
|
2008-10-21 02:38 AM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-21 04:12 AM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-21 03:55 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-21 04:02 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-21 04:46 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-21 05:11 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-21 08:09 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-21 09:03 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-21 10:31 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-21 10:52 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-22 05:57 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-22 08:06 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Lonkero
|
2008-10-22 09:12 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-22 09:16 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Lonkero
|
2008-10-22 09:20 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-22 09:34 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Mart
|
2008-10-23 09:16 AM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-23 03:10 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-23 03:27 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-23 04:47 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-23 04:59 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Mart
|
2008-10-23 05:01 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-23 05:02 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-23 05:07 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Allen
|
2008-10-23 05:20 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-23 05:41 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Kdyer
|
2008-10-23 05:45 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-23 06:16 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Kdyer
|
2008-10-23 06:53 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-23 08:00 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-23 09:39 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-27 04:13 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-27 04:21 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-27 04:32 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-27 04:34 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-27 04:41 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-28 08:06 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-28 08:14 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-29 04:40 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-29 04:47 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-29 04:57 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-29 05:05 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-29 05:52 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-30 04:23 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-28 08:24 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Lonkero
|
2008-10-28 10:03 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-28 10:10 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Mart
|
2008-10-27 04:25 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Gargoyle
|
2008-10-23 04:54 PM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-21 12:47 AM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Glenn Barnas
|
2008-10-21 12:53 AM
|
Re: Hi, All - (Where do i Post a Full Script I want to Share)
|
Indigo
|
2008-10-20 09:00 PM
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 657 anonymous users online.
|
|
|