Powershell FileSystem Event/Image manipulation issues


hello,

i'm working on project involving filesystem events, , image manipulation. i've written attached script, having trouble it. seems work 90% of time. unfortunately, 80% of time, (2) events trigger each file system event occurs. , crash. not sure if there blaring issues code, if willing take look, appreciated.

the filesystem event seems run smoothly (i.e. can remove of own code, , produces message everytime add file watch folder.) when start manipulating file, , remove watch folder, things start act funky. 2 things i'm seeing weird event happen twice each file added watchfolder, although script run correctly. , second weird thing happens, error thrown , stop. 

it's been frustrating, lol.

see script below:

#by bigteddy 05 september 2011    #this script uses .net filesystemwatcher class monitor file events in folder(s).  #the advantage of method on using wmi eventing can monitor sub-folders.  #the -action parameter can contain valid powershell commands.  have included 2 example.  #the script can set wildcard filter, , includesubdirectories can changed $true.  #you need not subscribe 3 types of event.  3 shown example.  # version 1.1    <#  $configpath = "c:\users\marcus garfunkel\dropbox\projects\powershell\settings.xml"  # import settings config file.   if (test-path $configpath){      $global:configfile = [xml](get-content $configpath)  }    import-module "..\imagemanipulation\image.psm1"    $global:watchfolder = $global:configfile.settings.folderpaths.watchfolder  write-host "watchfolder: $global:watchfolder"  $global:destfolder = $global:configfile.settings.folderpaths.destfolder  write-host "destfolder: $global:destfolder"  #>  #$global:watchfolder = 'c:\users\marcus garfunkel\documents\worldstage - local\projects\powershell\dragnewfileshere' # enter root path want monitor.  #$global:destfolder = 'c:\users\marcus garfunkel\documents\worldstage - local\projects\powershell\watchoutfiles'  $filter = '*.*'  # can enter wildcard filter here.    # in following line, can change 'includesubdirectories $true if required.                            $fsw = new-object io.filesystemwatcher $global:watchfolder, $filter -property @{includesubdirectories = $false;notifyfilter = [io.notifyfilters]'filename, lastwrite'}    # here, 3 events registerd.  need subscribe events need:    register-objectevent $fsw created -sourceidentifier filecreated -action {      $name = $event.sourceeventargs.name      $changetype = $event.sourceeventargs.changetype      $timestamp = $event.timegenerated        $configpath = "c:\users\marcus garfunkel\dropbox\projects\powershell\settings.xml"      # import settings config file.       if (test-path $configpath){          $global:configfile = [xml](get-content $configpath)      }        import-module "..\imagemanipulation\image.psm1"        $global:watchfolder = $global:configfile.settings.folderpaths.watchfolder      write-host "watchfolder: $global:watchfolder"      $global:destfolder = $global:configfile.settings.folderpaths.destfolder      write-host "destfolder: $global:destfolder"        $desired_width = $global:configfile.settings.imagesettings.imagewidth      $desired_height = $global:configfile.settings.imagesettings.imageheight        write-host "the file '$name' $changetype @ $timestamp" -fore green      #out-file -filepath c:\scripts\filechange\outlog.txt -append -inputobject "the file '$name' $changetype @ $timestamp"            try{          $sourcefile = "$global:watchfolder\$name"          $underlayfile = $global:configfile.settings.imagepaths.bkgdimage          $tempfile = $global:configfile.settings.imagepaths.tempfile          $outfile = $global:configfile.settings.imagepaths.outputfile                    write-host "loading image '$sourcefile'..." -fore darkgray          $image = get-image $sourcefile                    write-host "loading underlay file '$underlayfile'..." -fore darkgray          $otherimage = get-image $underlayfile                    write-host "adding appropriate filters."          $filter = add-scalefilter -width $desired_width -height $desired_height -passthru #|              #add-overlayfilter -image $otherimage -left 10 -top 10 -passthru          write-host "applying appropriate filters."          $image = $image | set-imagefilter -filter $filter -passthru                        write-host "image width: " $image.width -fore blue          write-host "image height: " $image.height -fore blue          write-host "scaling to: $desired_width x $desired_height."          if ($image.width -lt $desired_width) {              $left = ($desired_width - $image.width) / 2              $top = 0          } else {              $top = ($desired_height - $image.height) / 2              $left = 0          }            $filter = add-overlayfilter -image $image -left $left -top $top -passthru          $otherimage = $otherimage | set-imagefilter -filter $filter -passthru           write-host "checking if $tempfile exists." -fore darkgray          if (test-path $tempfile){              remove-item $tempfile              write-host "removed file $tempfile" -fore red          }          $otherimage.savefile($tempfile)          write-host "saved file '$tempfile'." -fore darkgray                    write-host "now converted file moved '$global:destfolder\$outfile'." -fore darkgray          move-item -path $tempfile -destination $global:destfolder\$outfile -force          write-host "now removing original file '$sourcefile'" -fore darkgray      #>          #remove-variable image          #remove-variable otherimage          remove-item $sourcefile                 }catch      {          $errormessage = $_.exception.message          $faileditem = $_.exception.itemname          write-host "error: $errormessage, $faileditem"          #send-mailmessage -from expensesbot@mycompany.com -to winadmin@mycompany.com -subject "hr file read failed!" -smtpserver exch01.ad.mycompany.com -body "we failed read file $faileditem. error message $errormessage"          #break      }      #>  }    <#   register-objectevent $fsw deleted -sourceidentifier filedeleted -action {  $name = $event.sourceeventargs.name  $changetype = $event.sourceeventargs.changetype  $timestamp = $event.timegenerated  write-host "the file '$name' $changetype @ $timestamp" -fore red  out-file -filepath c:\scripts\filechange\outlog.txt -append -inputobject "the file '$name' $changetype @ $timestamp"}    register-objectevent $fsw changed -sourceidentifier filechanged -action {  $name = $event.sourceeventargs.name  $changetype = $event.sourceeventargs.changetype  $timestamp = $event.timegenerated  write-host "the file '$name' $changetype @ $timestamp" -fore white  out-file -filepath c:\scripts\filechange\outlog.txt -append -inputobject "the file '$name' $changetype @ $timestamp"}   #>    <#  {      start-sleep -s 0.1  } while (1)  #>    # stop monitoring, run following commands:  # unregister-event filedeleted  # unregister-event filecreated  # unregister-event filechanged

thanks input,

marcus

there way many syntax errors.  recommend starting on , isolatingyour elements. avoid comments adding confusion code.

avoid "global" variables and import-module in action code.  action code should short.


\_(ツ)_/



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Event ID 64,77,1008 Certificates Events Windows Server 2008, 2008R2