Comunidad de usuarios
de fibra, móvil y ADSL

Una de Powershell


Esroy vigilando una carpeta por pdf's nuevos, y los mando por mail en cuanto aparezcan.

Ea executepolicy la puse en remotesigned ... y el script se ejecuta (tanto en modo batch como interactivo)...

El problema es que si lo ejecuto desde la ISE (el editor), sí que captura el evento ... pero si lo ejecuto desde la shell , no le da la gana capturar el evento :S ..

¿alguna idea?

#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

$folder = "C:\patata" # Enter the root path you want to monitor.
$filter = "*.pdf" # 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

  $outlook = New-Object -comObject  Outlook.Application
    $mail = $outlook.CreateItem(0)
    $mail.Subject = $name

    # For HTML encoded emails
    # $mail.HTMLBody = "TextBOLD Color Text"

 # To send an attachment
 $mail.Attachments.Add($folder + "\" + $name)

 Write-Host "Email sent!"

 #Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
 #Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"}

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