Logging an Event into Windows Event Logs

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Tore Groneng Tore Groneng 3 years ago.

  • Author
    Posts
  • #13011
    Profile photo of Francis Lane
    Francis Lane
    Participant

    Hi

    Im trying to use the attached script file to log an event into the Windows Event Logs when the last previous version was more than 3 days old.

    Im guessing I will need to add an IF statement in with something along the lines of.

    if $Object.LatestShadowCopy lt (get-date).adddays 3 Write-EventLog Application -Source "VSS Monitoring" -EventId 0 -Message "No Shadow Copies created recently" -EntryType error
    Else Continue

    But im a bit clueless how to format this correctly in the script.

    Any advice would be greatly appreciated.

  • #13042
    Profile photo of Mike F Robbins
    Mike F Robbins
    Participant

    Was something like this what you had in mind?

    if ((.\Get-ShadowCopyStats.ps1 -ServerName $env:COMPUTERNAME).LatestShadowCopy -lt (Get-Date).AddDays(-3)) {
    Write-EventLog -LogName Application -Source 'VSS Monitoring' -EventId 0 -Message 'No Shadow Copies created recently' -EntryType Error
    }

    This will generate an error unless you first create the custom source named 'VSS Monitoring':

    New-EventLog –LogName Application –Source 'VSS Monitoring'

    Although I'm using the New-EventLog cmdlet in the previous one-liner, it actually doesn't create a new eventlog, instead it creates a new 'source' for the existing application log.

    µ

  • #13411
    Profile photo of Francis Lane
    Francis Lane
    Participant

    Hi Mike

    that works great, but im having a problem where there's more than one drive with shadow copy enabled. I assume this is because there would be two .LatestShadowCopy in the output (one for each drive) I tried putting a for each in the if statement but that didn't seem to work. Any ideas?

    Many thanks
    Francis

  • #13609
    Profile photo of Francis Lane
    Francis Lane
    Participant

    Hi All

    Im still struggling with this issue, if anyone has any thoughts on how to get this working it would be very much appreciated

    Thanks
    Francis

  • #14531
    Profile photo of Francis Lane
    Francis Lane
    Participant

    Hi All

    Im still struggling with this issue, appreciate any help if you have the time.

    Thanks
    Francis

  • #14540
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Could you please clarify what you're having a problem with? Is it writing to the event log? Formatting a message? You said you're, "having a problem where there's more than one drive..." but you didn't say what, exactly, the problem was. If you're able to provide some more detail on what you want to happen, and what problem you're having, it might be easier for folks to offer advice.

  • #14548
    Profile photo of Tore Groneng
    Tore Groneng
    Participant

    hi,

    what happens if you do:


    .\Get-ShadowCopyStats.ps1 -ServerName $env:COMPUTERNAME | foreach {
    if($_.LatestShadowCopy -lt (Get-Date).AddDays(-3)){
    Write-EventLog -LogName Application -Source 'VSS Monitoring' -EventId 0 -Message 'No Shadow Copies created recently' -EntryType Error
    }
    }

You must be logged in to reply to this topic.