none
How to use IO.FileSystemWatcher to Create a Print-Hotfolder / Why is my script not workling? RRS feed

  • Frage

  • I want to use the IO.FileSystemWatcher to Crate a Hotfolder. Every File that is thrown into this folder should be printed out on the local standard printer. 

    I put some extra in logging and sending an notification Mail but the main reason is to print the files and move them in a different folder after they have been printed.

    Unregister-Event -SourceIdentifier *
    
    $folder = 'C:\Users\R&D\Documents\Python_Powershell_print\printfolder' # Enter the root path you want to monitor. 
    $filter = '*.*'  # You can enter a wildcard filter here. 
     
    # In the following line, you can change 'IncludeSubdirectories to $true if required.                           
    $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 
     
    Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
    $name = $Event.SourceEventArgs.Name 
    $changeType = $Event.SourceEventArgs.ChangeType 
    $timeStamp = $Event.TimeGenerated
    
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
    Out-File -FilePath C:\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"
    start-process -FilePath $name.fullName -Verb Print
    Move-Item -FilePath "$name.fullName" C:\printed -Verbose -Force *>&1
    
    $MyEmail = "superspamtube@gmail.com"
    $SMTP= "smtp.gmail.com"
    $To = "superspamtube@gmail.com"
    $Subject = "PRINT HOTFOLDER HIT"
    $Body = "PRINT HOTFOLDER HIT"
    $Creds = (Get-Credential -Credential "$MyEmail")
    
    Start-Sleep 2
    
    Send-MailMessage -To $to -From $MyEmail -Subject $Subject -Body $Body -SmtpServer $SMTP -Credential $Creds -UseSsl -Port 587 -DeliveryNotificationOption never
    
    <# 
    $PSEmailServer variable can be used to pre-configure the
    SMTP server in your Powershell Profile. Then you don't need
    to specify -smtpserver paramter. Send-MailMessage will use the
    SMTP sever address assigned to $PSEmailServer
    Delivery Notification Options:
    -- None: No notification.        
    -- OnSuccess: Notify if the delivery is successful.      
    -- OnFailure: Notify if the delivery is unsuccessful.     
    -- Delay: Notify if the delivery is delayed.       
    -- Never: Never notify.
    #>
    }
    # To stop the monitoring, run the following commands: 
    # Unregister-Event FileDeleted 
    # Unregister-Event FileCreated 
    # Unregister-Event FileChanged

    Freitag, 19. Juli 2019 11:56

Antworten

  • Hi, this is the german forum, so I'll answer in german. (the forum your question gets posted in, depends on the language you set on the top right of the site):

    Dein Script funktioniert wunderbar. Allerdings nur so lange die Powershell-Session in der das Event registriert wurde, nicht geschlossen wurde. D.h. du musst dafür sorgen das dein Script sich entweder nicht beendet solange du das monitoring aufrecht erhalten willst oder zumindest die Konsole aus der du es gestartet hast offen bleibt.
    Für ersteres gibt es diverse Möglichkeiten, falls du da Hilfe brauchst, melde dich nochmal.

    Grüße, Denniver


    Blog: http://www.bytecookie.de

    Powershell Code Manager: Link
    (u.a. Codesnippets verwalten + komplexe Scripte graphisch darstellen)

    Hilf mit und markiere hilfreiche Beiträge mit dem "Abstimmen"-Button (links) und Beiträge die eine Frage von dir beantwortet haben, als "Antwort" (unten).
    Warum das Ganze? Hier gibts die Antwort.


    Sonntag, 28. Juli 2019 12:33
    Moderator