Last active
May 4, 2021 07:22
-
-
Save jtuttas/b46979a6cc976c5418c053625f755131 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$global:pw="geheim" | |
# Vor dem Start werden alte, registrierte Events geschlossen | |
Unregister-Event -SourceIdentifier * | |
# Initialisierung der Variablen | |
$global:Folder = "$PSScriptRoot\encoded" | |
$global:FolderOut = "$PSScriptRoot\decoded" | |
$global:TempFolder = "$PSScriptRoot\temp" | |
# Anlegen der Verzecihnisse wenn Sie noch nicht Existieren | |
if (-not (Test-Path $global:Folder)) { | |
mkdir $global:Folder | |
} | |
if (-not (Test-Path $global:FolderOut)) { | |
mkdir $global:FolderOut | |
} | |
if (-not (Test-Path $global:TempFolder)) { | |
mkdir $global:TempFolder | |
} | |
if (-not (Test-Path "$PSScriptRoot\log.txt")) { | |
"Script gestartet um $(Get-Date)" | Set-Content "$PSScriptRoot\log.txt" | |
} | |
else { | |
"Script gestartet um $(Get-Date)" | Add-Content "$PSScriptRoot\log.txt" | |
} | |
#$npw = Read-Host -Prompt "Geben Sie das Kennwort an, mit dem die Excel Dateien geschützt werden sollen: [$global:pw]" | |
#if ($npw -ne "") { | |
# $global:pw=$npw | |
#} | |
Write-Host "Folder decoded ($global:FolderOut) Folder encoded ($global:Folder) Kennwort=($global:pw)" | |
$Filter = "*.xls" | |
$global:files=@{}; | |
# FileSystemWatcher aufbauen und Überwachung starten | |
$FSW = new-object system.io.filesystemwatcher # FileSystemWatcher erstellen | |
$FSW.Path = $global:Folder # zu überwachendes Verzeichnis übergeben | |
$FSW.IncludeSubdirectories = $True # Unterverzeichnisse einschließen? : NEIN! | |
$FSW.Filter = $Filter # Filter für die Überwachung… auch Wildcards gehen… | |
$global:FSWdecoded = new-object system.io.filesystemwatcher # FileSystemWatcher erstellen | |
$global:FSWdecoded.Path = $global:FolderOut # zu überwachendes Verzeichnis übergeben | |
$global:FSWdecoded.IncludeSubdirectories = $True # Unterverzeichnisse einschließen? : NEIN! | |
$global:FSWdecoded.Filter = $Filter # Filter für die Überwachung… auch Wildcards gehen… | |
# Die Funktion out_Text() muss global definiert werden, weil der EventWatcher | |
# in einem eigenen Bereich läuft und sie sonst nicht finden würde. | |
# Die globale Definition gilt für die Ausführung als Script-Datei *.ps1 | |
function global:out_Text($txt) { Write-Host („{0}“ -f $txt) -BackgroundColor DarkGray } | |
function global:log($txt) { "$(get-date): $txt" | Add-Content "$PSScriptRoot\log.txt"} | |
function global:moveExcel($from,$to,$efile,$pwfrom,$pwto) { | |
#Copy-Item -Path "C:\access\ZeugnisExcelTool\encoded\notgeheim.xls" -Destination "$env:TEMP\notgeheim.xls" | |
Write-Host "Verschiebe $from\$efile nach $($global:TempFolder)\$efile" -BackgroundColor DarkGreen | |
Copy-Item -Path "$from" -Destination "$global:TempFolder" -Force | |
Write-Host "Öffne Excel" -BackgroundColor DarkGreen | |
$excel = new-object -comobject Excel.Application | |
$excel.Visible = $false | |
$excel.DisplayAlerts = $False | |
if ($pwfrom) { | |
Write-Host "Öffne $efile mit Kennwort $pwfrom" -BackgroundColor DarkGreen | |
try { | |
$wb=$excel.Workbooks.Open("$global:TempFolder\$efile",0,$false,1,$pwfrom); | |
log "encodiere $efile" | |
} | |
catch { | |
Write-Host "Fehler beim öffnen von $global:TempFolder\$efile evtl. stimmt das Kennwort nicht" -BackgroundColor DarkRed | |
log "Fehler beim oeffnen von $global:TempFolder\$efile evtl. stimmt das Kennwort nicht" | |
$excel.Quit(); | |
Write-Host "Beende Excel und lösche Datei in $global:TempFolder\$efile" -BackgroundColor DarkRed | |
Remove-Item "$global:TempFolder\$efile" | |
while( [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)){} | |
return | |
} | |
} | |
else { | |
Write-Host "Öffne $efile" -BackgroundColor DarkGreen | |
try { | |
$wb=$excel.Workbooks.Open("$global:TempFolder\$efile",0,$false,1,""); | |
log "decodiere $efile" | |
} | |
catch { | |
Write-Host $_.Exception.Message | |
Write-Host "Fehler beim öffnen von $global:TempFolder\$efile evtl. ist die Datei mit einem Kennwort geschützt" -BackgroundColor DarkRed | |
log "Fehler beim oeffnen von $global:TempFolder\$efile evtl. ist die Datei mit einem Kennwort geschuetzt" | |
$excel.Quit(); | |
Write-Host "Beende Excel und lösche Datei in $global:TempFolder\$efile" -BackgroundColor DarkRed | |
Remove-Item "$global:TempFolder\$efile" | |
while( [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)){} | |
return | |
} | |
} | |
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlExcel8 | |
if ($pwto) { | |
Write-Host "Verschlüssele $efile mit Kennwort $pwto" -BackgroundColor DarkGreen | |
try { | |
$wb.SaveAs("$global:TempFolder\$efile",$xlFixedFormat,$pwto); | |
} | |
catch { | |
$_.Exception | |
Write-Host "Exception" | |
Write-Host $_.Exception.Message | |
} | |
#Write-Host "speichere---" | |
} | |
else { | |
$wb.Password=""; | |
Write-Host "Speicher $efile ohne Kennwort" -BackgroundColor DarkGreen | |
$wb.SaveAs("$global:TempFolder\$efile",$xlFixedFormat); | |
} | |
$wb.Close(); | |
$excel.Quit(); | |
Write-Host "Verschiebe $efile nach $to" -BackgroundColor DarkGreen | |
$path=Split-Path $to | |
if (-not (Test-Path $path)) { | |
mkdir $path | |
} | |
Move-Item -Path "$global:TempFolder\$efile" -Destination "$to" -Force | |
#Write-Host "Beende Excel" -BackgroundColor DarkGreen | |
while( [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)){} | |
#Write-Host "fertig!" -BackgroundColor DarkGreen | |
} | |
function global:newExcelFile($file,$Event) { | |
$time=[long]($Event.TimeGenerated.Ticks); | |
$d = get-Date | |
#Write-Host "Differenz zum letzten Ereignis is $($d.Ticks-$global:files[$file])" | |
# Nach 8 Sec wird es als neuer Kopiervorgang betrachtet | |
if (-not $global:files[$file] -or $d.Ticks-$global:files[$file] -gt 80000000) { | |
$global:files[$file]=$d.Ticks; | |
return $true; | |
} | |
return $false; | |
} | |
$OnCreate = { | |
$o=Split-Path $eventArgs.FullPath –Leaf | |
if (newExcelFile $o $Event) { | |
$text = „!Ereignis: [{0}] in {1} : {2:d}“ –f $eventArgs.ChangeType, $eventArgs.FullPath, $Event.TimeGenerated.ToLocalTime() | |
out_Text ($text) # Funktion out_Text aufrufen und Ausgabetext übergeben | |
[String]$dest = $eventArgs.FullPath | |
$dest=$dest.Replace("encoded","decoded") | |
moveExcel $eventArgs.FullPath $dest $o $null $global:pw | |
} | |
} | |
$OnChange = { | |
$o=Split-Path $eventArgs.FullPath –Leaf | |
if (newExcelFile $o $Event) { | |
$text = „Ereignis!! : [{0}] in {1} : {2:d}“ –f $eventArgs.ChangeType, $eventArgs.FullPath, $Event.TimeGenerated.ToLocalTime() | |
out_Text ($text) # Funktion out_Text aufrufen und Ausgabetext übergeben | |
[String]$dest = $eventArgs.FullPath | |
$dest=$dest.Replace("encoded","decoded") | |
out_Text("Destination $dest") | |
moveExcel $eventArgs.FullPath $dest $o $null $global:pw | |
} | |
} | |
$OnRename = { | |
$text = „Ereignis: [{0}] in {1} : {2:d}“ –f $eventArgs.ChangeType, $eventArgs.FullPath, $Event.TimeGenerated.ToLocalTime() | |
out_Text ($text) # Funktion out_Text aufrufen und Ausgabetext übergeben | |
} | |
$OnDelete = { | |
$text = „Ereignis: [{0}] in {1} : {2:d}“ –f $eventArgs.ChangeType, $eventArgs.FullPath, $Event.TimeGenerated.ToLocalTime() | |
out_Text ($text) # Funktion out_Text aufrufen und Ausgabetext übergeben | |
$o=Split-Path $eventArgs.FullPath –Leaf | |
$text="Lösche auch Datei {0} in {1}" -f $o,$global:FolderOut | |
out_Text ($text) | |
Remove-Item -Path "$global:FolderOut/$o" | |
} | |
# Handler für decoded | |
$OnCreatedecoded = { | |
$o=Split-Path $eventArgs.FullPath –Leaf | |
if (newExcelFile $o $Event) { | |
$text = „Ereignis: [{0}] in {1} : {2:d}“ –f $Event.SourceEventArgs.ChangeType , $eventArgs.FullPath, $Event.TimeGenerated.ToLocalTime() | |
out_Text ($text) # Funktion out_Text aufrufen und Ausgabetext übergeben | |
[String]$dest = $eventArgs.FullPath | |
$dest=$dest.Replace("decoded","encoded") | |
moveExcel $eventArgs.FullPath $dest $o $global:pw $null | |
} | |
} | |
$OnChangedecoded = { | |
$o=Split-Path $eventArgs.FullPath –Leaf | |
if (newExcelFile $o $Event) { | |
$text = „Ereignis! : [{0}] in {1} : {2:d}“ –f $eventArgs.ChangeType, $eventArgs.FullPath, $Event.TimeGenerated.ToLocalTime() | |
out_Text ($text) # Funktion out_Text aufrufen und Ausgabetext übergeben | |
[String]$dest = $eventArgs.FullPath | |
$dest=$dest.Replace("decoded","encoded") | |
moveExcel $eventArgs.FullPath $dest $o $global:pw $null | |
} | |
else { | |
#out_Text("alt"); | |
} | |
} | |
$OnRenamedecoded = { | |
$text = „Ereignis: [{0}] in {1} : {2:d}“ –f $eventArgs.ChangeType, $eventArgs.FullPath, $Event.TimeGenerated.ToLocalTime() | |
out_Text ($text) # Funktion out_Text aufrufen und Ausgabetext übergeben | |
} | |
$OnDeletedecoded = { | |
$text = „Ereignis: [{0}] in {1} : {2:d}“ –f $eventArgs.ChangeType, $eventArgs.FullPath, $Event.TimeGenerated.ToLocalTime() | |
out_Text ($text) # Funktion out_Text aufrufen und Ausgabetext übergeben | |
$o=Split-Path $eventArgs.FullPath –Leaf | |
$text="Lösche auch Datei {0} in {1}" -f $o,$global:Folder | |
out_Text ($text) | |
Remove-Item -Path "$global:Folder/$o" | |
} | |
# abonniert das entsprechende Ereignis für DATEI ERSTELLT, GEÄNDERT, UMBENANNT, GELÖSCHT | |
# das „" ist ein Zeilenumbruch im Script | |
Register-ObjectEvent -InputObject $FSW -EventName Created -SourceIdentifier FileCreated -Action $OnCreate | Out-Null | |
Register-ObjectEvent -InputObject $FSW -EventName Changed -SourceIdentifier FileChanged -Action $OnChange | Out-Null | |
Register-ObjectEvent -InputObject $FSW -EventName Renamed -SourceIdentifier FileRenamed -Action $OnRename | Out-Null | |
#Register-ObjectEvent -InputObject $FSW -EventName Deleted -SourceIdentifier FileDeleted -Action $OnDelete | Out-Null | |
Register-ObjectEvent -InputObject $global:FSWdecoded -EventName Created -SourceIdentifier FileOutCreated -Action $OnCreatedecoded | Out-Null | |
Register-ObjectEvent -InputObject $global:FSWdecoded -EventName Changed -SourceIdentifier FileOutChanged -Action $OnChangedecoded | Out-Null | |
Register-ObjectEvent -InputObject $global:FSWdecoded -EventName Renamed -SourceIdentifier FileOutRenamed -Action $OnRenamedecoded | Out-Null | |
#Register-ObjectEvent -InputObject $global:FSWdecoded -EventName Deleted -SourceIdentifier FileOutDeleted -Action $OnDeletedecoded | Out-Null | |
# abonnierte Ereignisse laufen im Hintergrund und können per Befehl | |
# Unregister-Event -SourceIdentifier * wieder beendet werden (siehe ganz oben) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment