Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SSMS Assembly Error - Microsoft.SqlServer.Sqm.dllSQL Saturday #99 Minnesota - #SQLAwesome »
    comments

    I’m in love. With a PowerShell script I wrote.

    We have multiple SQL Server Agent jobs that run on a daily or weekly basis. These jobs write logs to a folder on the server. Example: D:\mssqlserver\backup

    When a job fails, we receive a ticket for it. The ticket does not include the reason for the failure. The existing procedure to find the error and resolve it was to RDP to the server, navigate to the directory, and read the text file.

    I knew there had to be a better way. Once I learned enough about PowerShell to be dangerous, I put together this script.

    Disclaimer: I know it’s not perfect. Yes, there are probably improvements I could make. It’s a giant step forward for me.

    1. #Go to a server share, read log
    2. #server
    3. $server='GRRLGEEK2008R2'
    4. #instance - if default, enter mssqlserver
    5. $instance='mssqlserver'
    6. #job
    7. $job='backup'
    8. #build share path
    9. $path='\\' + $Server + '\' + $instance + '$\logs\' + $job
    10. #$path
    11. #go to server
    12. Get-ChildItem -Path $Path -Filter *log | Sort-Object -Descending LastWriteTime | Select -First 1 -Property LastWriteTime
    13. Get-ChildItem -Path $Path -Filter *log | Sort-Object -Descending LastWriteTime | Select -First 1 | Get-Content

    Breaking it down:

    $server, $instance, $job, and $path are the variables.

    Get-ChildItem –Path will look for all the objects in the current directory, which is the $path variable.

    -Filter will narrow those results down to only the files with “log” in the name.

    That output is sent to the Sort-Object –Descending LastWriteTime command, which is sorting the files, in descending order, by the last write date. I only want to view the latest file.

    Here, the two commands split.

    The first command sends output to the Select - First command, which pulls the latest file. –Property LastWriteTime will display the time the log was written. This is helpful to me so I can see when my job started.

    The second command sends output to the Get-Content command, which displays the text file in my ISE window. I can then review it for errors.

    It used to take about 3 minutes to determine the cause of the error. It now takes about 30 seconds. My team averages about 20 tickets per day that this can be used on. Over the course of a year, this saves my team approximately 600 hours of labor.

    Six hundred hours.

    If you haven’t learned PowerShell yet, now is the time.

    About the Author

    User bio imageJes Schultz Borland is a Consultant with Brent Ozar PLF, and a Microsoft SQL Server MVP. She holds an AAS - Programmer/Analyst degree, and has worked with SQL Server since 2007, focusing on Reporting Services and day-to-day administration. She is an active member of PASS, Director of Communication for MADPASS, founder of Tech on Tap, and a LessThanDot.com blogger. She's a frequent presenter at user groups, SQL Saturdays, and other community events. She is also an avid runner and chef.
    Social SitingsTwitterLinkedInLTD RSS Feed
    Instapaper

    2 comments

    Comment from: SQLDenis [Member] Email
    SQLDenis You can also use undocumented T-SQL, see here Read the error log with T-SQL

    here are some commands

    --Read current error log
    EXEC xp_readerrorlog 0
    GO

    --Search for backup database messages

    EXEC sp_readerrorlog 0, 1, 'Database backed up'
    GO
    10/28/11 @ 11:11
    Comment from: Thomas Rushton [Visitor] · http://thelonedba.wordpress.com
    Thomas Rushton 600 hours saved per year? That's one helluva return on investment - don't forget to put it into your appraisal!
    10/28/11 @ 11:32

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)