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

**URL:** <https://forums.powershell.org/t/hi-everyone-im-am-having-a-ton-of-trouble-with-this-script/1038>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/hi-everyone-im-am-having-a-ton-of-trouble-with-this-script/1038 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/hi-everyone-im-am-having-a-ton-of-trouble-with-this-script/1038/1 "2011-12-31T18:30:00Z")

</div>

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 afterwardstrap {	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](http://www.out-web.net/?p=1117)

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

> [quote="megamorf"]\<span\>You could also use a filesystem watcher: \<a class="smarterwiki-linkify" href="http]  
> Great, haven’t heard of it before. Thanks.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:58pm UTC](https://forums.powershell.org/t/hi-everyone-im-am-having-a-ton-of-trouble-with-this-script/1038/2 "2024-05-16T20:58:00Z")

</div>


