none
Verzeichnis regelmässig überprüfen RRS feed

  • Frage

  • hi,

    ich hab jetzt das geschrieben:

    Get-ChildItem -Path 'D:\Kemal test xml\XML' | SELECT CreationTime, name | Format-Table -AutoSize | Out-File -filepath 'D:\Kemal test xml\Logfiles\log.txt'   .

    Also: in den order XML kommen täglich xml dateien rein (get-ChildItem). wenn das passiert soll das geschriebene skript automatisch es in den logfiles\log.txt schreiben ohne das man es mit f8 jedesmal durchlaufen lassen muss!

    und es soll jedentag eine neue log.txt erstellen!!

    Bitte um eure hilfe..!!!  Danke!!! :-)

    (bin leider praktikant und arbeite heut erste mal mit powershell)


    Montag, 21. März 2016 14:46

Antworten

  • Hallo,

    ich würde das ganze relativ kurz direkt in PowerShell mit einem CIMIndicationEvent lösen:

    Register-CimIndicationEvent -Query "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE
    TargetInstance ISA 'CIM_DataFile' AND (TargetInstance.Drive='D:' AND TargetInstance.Path='\\Kemal test xml\\XML\\' AND TargetInstance.Extension='xml')" -Action {
    Get-ChildItem -Path 'D:\Kemal test xml\XML' | SELECT CreationTime, name | Format-Table -AutoSize | Out-File -filepath 'D:\Kemal test xml\Logfiles\log.txt'
    }
    


    Mittwoch, 23. März 2016 15:30

Alle Antworten

  • Hallo,

    Wenn Du weist, zu welcher Uhrzeit die Dateien kommen, kannst Du den Windows Scheduler verwenden, um das PowerShell Skript zu starten.


    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Dienstag, 22. März 2016 07:29
  • ne leider nicht.. weiss nur das täglich daten reinkommen und ich soll nicht ausdrücklich mit task scheduler arbeiten weil ich das au angeboten hatte..

    aktuel:

    Get-ChildItem -Path 'D:\Kemal test xml\XML' | SELECT CreationTime, name | Format-Table -AutoSize | Out-File -filepath ('D:\Kemal test xml\Logfiles\log'+((Get-Date).ToString("yyyy_MM_dd"))+'.txt')

    • Bearbeitet kemal_84 Mittwoch, 23. März 2016 08:30
    Mittwoch, 23. März 2016 08:27
  • Dann siehe
    Powershell FileSystemWatcher
    Use PowerShell to Monitor for the Creation of New Files

    wie man Verzeichnis auf neue Dateien überwacht, damit das Skript darauf reagieren kann.


    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Mittwoch, 23. März 2016 08:43
  • #By BigTeddy 05 September 2011

    #This script uses the .NET FileSystemWatcher class to monitor file events in folder(s).
    #The advantage of this method over using WMI eventing is that this can monitor sub-folders.
    #The -Action parameter can contain any valid Powershell commands.  I have just included two for example.
    #The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true.
    #You need not subscribe to all three types of event.  All three are shown for example.
    # Version 1.1




    # To stop the monitoring, run the following commands:
    Unregister-Event FileDeleted
    Unregister-Event FileCreated
    Unregister-Event FileChanged

    Unregister-Event -SourceIdentifier *


     
    $folder = 'D:\Kemal test xml\XML' # 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'}
     
    # Here, all three events are registerd.  You need only subscribe to events that you need:
     
    Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "$timeStamp was $changeType at $name" -fore green
    Out-File -FilePath ('D:\Kemal test xml\Logfiles\log'+((Get-Date).ToString("yyyy_MM_dd"))+'.txt') -Append -InputObject "$timeStamp was $changeType at $name"}
     
    Register-ObjectEvent $fsw Deleted -SourceIdentifier FileDeleted -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "$timeStamp was $changeType at $name" -fore red
    Out-File -FilePath ('D:\Kemal test xml\Logfiles\log'+((Get-Date).ToString("yyyy_MM_dd"))+'.txt') -Append -InputObject "$timeStamp was $changeType at $name"}  
     
    Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "$timeStamp was $changeType at $name" -fore white
    Out-File -FilePath ('D:\Kemal test xml\Logfiles\log'+((Get-Date).ToString("yyyy_MM_dd"))+'.txt') -Append -InputObject "$timeStamp was $changeType at $name"}

    Get-ChildItem -Path 'D:\Kemal test xml\XML' | SELECT CreationTime, name | Format-Table -AutoSize | Out-File -filepath ('D:\Kemal test xml\Logfiles\log'+((Get-Date).ToString("yyyy_MM_dd"))+'.txt')

    jetzt wird jeden tag eben ne neue logyyyy_MM_dd.txt datei erstellt alles passt nur das change problem hab i no.!! beim create (datei kommt in ordner XML rein) wird grün nen eintrag geschrieben und zusätzlich 2x change was i gar nicht will! wenn i im ordner XML die datei ändere soll es change meldung geben die kommt aba au doppelt! wenn i die datei lösch wird wie gewünscht nur 1x rote meldung delete gemeldet!

    ich will nich das change meldung beim erstellen kommt und nicht imma 2 mal..  irgendwo is da was zuviel geschrieben denk ich...

    • Bearbeitet kemal_84 Donnerstag, 24. März 2016 09:10
    Mittwoch, 23. März 2016 13:39
  • Hallo,

    ich würde das ganze relativ kurz direkt in PowerShell mit einem CIMIndicationEvent lösen:

    Register-CimIndicationEvent -Query "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE
    TargetInstance ISA 'CIM_DataFile' AND (TargetInstance.Drive='D:' AND TargetInstance.Path='\\Kemal test xml\\XML\\' AND TargetInstance.Extension='xml')" -Action {
    Get-ChildItem -Path 'D:\Kemal test xml\XML' | SELECT CreationTime, name | Format-Table -AutoSize | Out-File -filepath 'D:\Kemal test xml\Logfiles\log.txt'
    }
    


    Mittwoch, 23. März 2016 15:30
  • danke!! log.txt kommt kein doppel change eintrag mehr!! 


    • Bearbeitet kemal_84 Donnerstag, 31. März 2016 12:47
    Dienstag, 29. März 2016 12:19
  • hi,

    das script sollte jetzt über powershell gestartet werden und dann im hintergrund für immer am server laufen!! es startet aber nicht oder funzt nich..

    ich soll function einbauen!! weiss jemand was das ist?

    lg


     
    #This script uses the .NET FileSystemWatcher class to monitor file events in folder(s).
    #The advantage of this method over using WMI eventing is that this can monitor sub-folders.
    #The -Action parameter can contain any valid Powershell commands.  I have just included two for example.
    #The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true.
    #You need not subscribe to all three types of event.  All three are shown for example.
    # Version 1.1


    Start-Job -filepath 'D:\Kemal test xml\Brudy überwachung test2.ps1'

    function global:fileChanged ($text) {
    Write-Host "This is the filechanged function, globally defined!"
    Write-Host $text

    }

    #Set-ExecutionPolicy Unrestricted

    # To stop the monitoring, run the following commands:
    #Unregister-Event FileDeleted
    #Unregister-Event FileCreated
    #Unregister-Event FileChanged

    Unregister-Event -SourceIdentifier *

    Register-CimIndicationEvent -Query "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE
    TargetInstance ISA 'CIM_DataFile' AND (TargetInstance.Drive='D:' AND TargetInstance.Path='\\Kemal test xml\\XML\\' AND TargetInstance.Extension='xml')" -Action {
    Get-ChildItem -Path 'D:\Kemal test xml\XML' | SELECT CreationTime, name | Format-Table -AutoSize | Out-File -filepath ('D:\Kemal test xml\Logfiles\log'+((Get-Date).ToString("yyyy_MM_dd"))+'.txt')
    }
     
    $folder = 'D:\Kemal test xml\XML' # 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 = $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
     
    # Here, all three events are registerd.  You need only subscribe to events that you need:

     
    Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "$timeStamp   $name  $changeType" -fore green
    Out-File -FilePath ('D:\Kemal test xml\Logfiles\log'+((Get-Date).ToString("yyyy_MM_dd"))+'.txt') -Append -InputObject "$timeStamp  $name  $changeType"}

     
    Register-ObjectEvent $fsw Deleted -SourceIdentifier FileDeleted -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "$timeStamp   $name  $changeType" -fore red
    Out-File -FilePath ('D:\Kemal test xml\Logfiles\log'+((Get-Date).ToString("yyyy_MM_dd"))+'.txt') -Append -InputObject "$timeStamp  $name  $changeType"}

     
    Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "$timeStamp   $name  $changeType" -fore white
    Out-File -FilePath ('D:\Kemal test xml\Logfiles\log'+((Get-Date).ToString("yyyy_MM_dd"))+'.txt') -Append -InputObject "$timeStamp  $name  $changeType"}

    #Get-ChildItem -Path 'D:\Kemal test xml\XML' | SELECT CreationTime, name | Format-Table -AutoSize | Out-File -filepath ('D:\Kemal test xml\Logfiles\log'+((Get-Date).ToString("yyyy_MM_dd"))+'.txt')

    • Bearbeitet kemal_84 Donnerstag, 31. März 2016 12:53
    Donnerstag, 31. März 2016 12:51