Hi everyone im am having a ton of trouble with this script

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 11 months ago.

  • Author
    Posts
  • #5983

    by balloonhands at 2012-12-11 22:10:00

    My assignment was to write a script that does these things, I am having a ton of trouble with it any help would be greatly appreciated.

    1.Create a new Windows log called Activity13_1 and assign a provider.
    2.Create a new file named "c:\temp\mytarget.txt"
    3.Add a few lines of text to it
    4.Assign the value "NO" to a variable called $trigger
    5.Assign an empty string to a variable called $old_len
    6.Assign an empty string to a variable called $old_writetime
    7.Create a while loop that will loop until $trigger is equal to "YES"
    8.Write a log entry stating that your script is starting up

    9.Inside of the loop:
    ?Check to see if the file exists; if it does not, write a log entry to your new log stating that the script is terminating with error, set $trigger equal to "YES", and then continue the loop
    ?Get file information for your file using Get-ChildItem
    ?If $old_len and $old_writetime are empty strings, set your variables equal to the Lengh and LastWriteTime properties from your object and then continue the loop
    ?Assign the Length of your object to a new variable called $curr_len
    ?Assign the LastWriteTime of your object to a new variable called $curr_writetime
    ?Compare the old and current variables:
    ?If both sets of variables are equal, then sleep the script for 5 seconds and then continue the loop
    ?If the length variables are different, write a log entry describing your findings, sleep the script for 3 seconds and then continue the loop
    ?If the writetime variables are different, write a log entry describing your findings, sleep the script for 3 seconds and then continue the loop

    by balloonhands at 2012-12-11 22:13:46

    This is how I started and I am completely stuck I am a newb...

    $my_file = "c:\mytarget.txt"
    $trigger = "NO"
    $old_len = ""
    $old_writetime = ""

    New-EventLog Activity13_1 –source Me

    if(!(Test-Path $my_file))
    {
    New-Item -ItemType file $my_file

    }
    add-Content $my_file "red"
    add-Content $my_file "blue"

    New-EventLog Lesson13Events –source E
    Write-EventLog Lesson13Events –message “script is starting up” –source e -id 0001

    New-EventLog trigger –source E2
    Write-EventLog trigger –message “script is starting up” –source E2 -id 0001

    $ndx = 0
    while ( $trigger -eq "no")
    {
    if(!(Test-Path $my_file))

    {
    Write-EventLog trigger –message “script is starting up” –source E2 -id 0001
    $trigger = "Yes"
    get-Childitem "c:\mytarget.txt"
    if ($old_len and $old_writetime -eq "")
    {

    }

    $curr_len = $my_file.length

    }

    by nohandle at 2012-12-12 03:48:10

    So the purpose of the script is to watch a file and log if there was a change to the file. Checking the length of the file content when the file was not changed is redundant. so you should end up with something like this (the log function is just prototyped):
    Function Write-log ([string]$Message, [switch]$Error)
    {
    if (-not (!"log exists"))
    {
    #Create Log
    }

    If ($error)
    {
    #mark the message type as error
    }
    else
    {
    #mark the message as informative
    }
    #write message to the log

    #test
    $Message
    }

    Function Get-FileDateAndLength ([string]$Path)
    {
    if (-not (Test-Path $file))
    {
    throw "file '$file' not found"
    }
    $file = Get-Item $Path
    $hash = @{LastWriteTime=$file.LastWriteTime; Length=$file.Length}
    New-Object -TypeName psObject -Property $hash
    }

    ###### main
    #to catch and log any terminating exception and exit afterwards
    trap
    {
    Write-Log -Error -Message $error[0].Exception.Message
    break
    }

    $file = 'c:\temp\test.txt'
    write-log -Message "starting script"
    $old = Get-FileDateAndLength -Path $file

    #true to run forever (or until exception is raised)
    while ($true)
    {
    $new = Get-FileDateAndLength -Path $file
    if ($old.LastWriteTime -ne $new.LastWriteTime)
    {
    Write-log "File '$file': last write time changed from $($old.LastWriteTime) to $($new.LastWriteTime)"
    if ($old.Length -ne $new.Length)
    {
    Write-log "File '$file': length changed from $($new.Length) to $($new.Length)"
    }
    $old = $new
    Start-Sleep -Seconds 3
    }
    else
    {
    #nothing has changed
    Start-Sleep 5
    }
    }

    this should work pretty reliably if you are able to write the log. if you are not able to write the log it probably dies "silently" because there is no place to save the error and it is only output to screen.
    another problem is when there is exception the looping is not resumed and the script terminates – as you asked in the request. maybe you should reconsider the approcach.

    by nohandle at 2012-12-12 05:59:49

    You can also leverage the wait parameter on the Get-Content cmdlet.
    in something like this:
    Get-Content $file -Wait | foreach { [string](get-date) + ": " + $_ | Out-File C:\temp\logfile.txt -Append }

    by balloonhands at 2012-12-12 12:25:16

    Thanks alot! that helps a ton, I really appreciate the help.

    by megamorf at 2012-12-12 23:15:25

    You could also use a filesystem watcher: http://www.out-web.net/?p=1117

    by nohandle at 2012-12-13 04:46:28

    [quote="megamorf"]You could also use a filesystem watcher: Great, haven't heard of it before. Thanks.

You must be logged in to reply to this topic.