Solved

Need to modify my powershell script to create event logs or perhaps a time stamp

Posted on 2011-09-29
6
527 Views
Last Modified: 2012-05-12
I got some help a few weeks ago and now use the following scrpt to clean up old .dat files that build up on my sharepoint server. The script works great and I am still thankful for the help I received, but my boss is hoping I can modify it to create event logs when it begins and ends or even a timestamp so we can use our network monitor to see how long it actually runs and be alerted if it stops running for some reason. I run this from a .bat file and it is scheduled to run in SCHEDULED TASKS. Everything runs fine, we just want to be able to monitor how long it takes and be alearted if it does not run. We were thinking we could monitor the event viewer and if a certain message does not appear we get alerted. All advice welcome..thanks

$Path = "D:\WSS_Files\data_realtime_archive\DataVolume\Farm(PTI-SQL#SHAREPOINT_CONFIG_FARM1)\http#80#processing\#"
gci $path -filter *.dat -recurse | ?{$_.lastwritetime -lt ((get-date).adddays(-60))} | Remove-Item -recurse -force
0
Comment
Question by:Thor2923
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36817584
What if you have it email you every time it finishes?  Set it to kick off at 9AM (or whatever) and then when you get the email, you know two things: how long it ran and that it's done.  If you don't get one, it didn't kick off.

http://blogs.msdn.com/b/powershell/archive/2009/10/30/sending-automated-emails-with-send-mailmessage-convertto-html-and-the-powershellpack-s-taskscheduler-module.aspx

HTH,

Dale Harris
0
 
LVL 1

Author Comment

by:Thor2923
ID: 36817672
interesting idea, I will run it by my boss....I am still hoping this job to create some kind of event log though....thanks
0
 
LVL 16

Accepted Solution

by:
Dale Harris earned 500 total points
ID: 36817943
What you're looking for is New-EventLog.  This can create both a new event log, or a new event in an existing log.

Just type in:

Get-Help New-EventLog


You can easily get the time it ran by doing this:

#This is at the top of your script
$StartTime = Get-Date

#This is at the end of your script
$EndTime = Get-Date

$Difference = $Endtime-$Starttime

"Your script took a total of $($Difference.seconds) seconds to run"

HTH,

Dale Harris
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 1

Author Comment

by:Thor2923
ID: 36894055
your time commands look like they are going to work. I was able to add your commands and copy and paste my script into a powershell window and after running it reported how many seconds it took. The next step would be to get this to create the output in a file if I call the PS1 from a BAT file. Do you know if there is an easy modification for that?? BTW the event comman looks great too, I am playing with that now
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36894980
You can hardcode the output:

"Your script took a total of $($Difference.seconds) seconds to run" > "C:\TestTimer.txt"

Good luck!

DH
0
 
LVL 1

Author Closing Comment

by:Thor2923
ID: 36897588
All your advice is working out perfectly!!! I have got much more interested and learned a lot about POWERSHELL from these few posts...THANKS
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problems creating account in AD with Powershell, data from SQL 57 51
WPF Select Items 3 27
Help to debug powershell script 5 43
Export the Results to CSV 2 20
Synchronize a new Active Directory domain with an existing Office 365 tenant
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question