1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| ### version1 .0
#######################################
#######################################
param
(
[int] $mode ,
[string] $source ,
[string] $destination,
[string] $path
)
# DATE ARRAYS
$Date = get-date -Format yyyyMMdd
$Hour = get-date -Format T
#mode
#$mode='1'
# PATHS
#$source ='c:\folderA' #chemin du fichier source
#$destination='c:\folderB' #chemin du fichier destination
#$path='C:\TSQL\EDI\EDI_AOG\Powershell\test.bat' #chemin du fichier bat
$ScriptLogs = "c:\folderB\$Date-Script.log"
#Tester la présence du Fichier source
If ((Test-Path $source ) -eq $false )
{
write-host "dossier $source Inexistant"
Write-Output "$Hour :---> dossier $source Inexistant" | Out-File -FilePath $ScriptLogs -Append
}
#Tester la présence du Fichier source
elseIf ((Test-Path $destination ) -eq $false )
{
write-host "dossier $destination Inexistant"
Write-Output "$Hour :----> dossier $destination Inexistant" | Out-File -FilePath $ScriptLogs -Append
}
else
{
if ($mode -eq '1')
{
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.IncludeSubdirectories = $true
$watcher.Path = $source
$watcher.EnableRaisingEvents = $true
#Evenement
$action =
{
$source = $event.SourceEventArgs.FullPath
$changetype = $event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, $source"
Write-Host "$path was $changetype at $(get-date)"
Move-Item -Path $source -Destination $destination -Verbose -force
Start-Process $path
# Add-Content $ScriptLogs -value $logline
}
#Register-ObjectEvent $watcher "Created" -Action $action
Register-ObjectEvent $watcher "Created" -Action $action
Write-Output "$Hour :--->$watcher Created" | Out-File -FilePath $ScriptLogs -Append
Write-Output "$Hour :--->Supervision du dossier $source" | Out-File -FilePath $ScriptLogs -Append
}
#Annulation inscription Evenement
else
{
Get-EventSubscriber | Unregister-Event
write-host "Annulation inscription Evenement "
Write-Output "$Hour :--->Annulation inscription Evenement" | Out-File -FilePath $ScriptLogs -Append
}
Get-EventSubscriber
} |
Partager