Hello,
I need to make a powershell script that changes a depose file into a pgp encrypted file. The problem is that for it to be self-sufficient, I need to be able to pass the o/N validation when executing the script.
Here’s my script, which currently works, but it requires a validation that I don’t want. I’d like it to validate silently.
# Chemin du dossier à surveiller
$cheminDossier = "C:\Users\AD14140\Desktop\Test GPG"
# Création d'un objet pour surveiller les changements dans le dossier
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $cheminDossier
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
# Action à effectuer lorsqu'un fichier est créé dans le dossier surveillé
$actionCréationFichier = {
$cheminFichier = $Event.SourceEventArgs.FullPath
$nomFichier = $Event.SourceEventArgs.Name
if ($nomFichier -like "*.xml") {
Write-Host "Nouveau fichier détecté : $nomFichier"
#importation de la cle publique
#gpg --import "PGP Production C64B93CD.asc"
#Preparation de la variable pour le fichier crypter
$cheminFichierGPG = $cheminFichier.Substring(0, $cheminFichier.Length - 3) + "asc"
$arguments = "--output `"$cheminFichierGPG`" --encrypt --recipient customer_support@kyriba.com `"$cheminFichier`""
# Lancement de la commande gpg pour crypter le fichier
Start-Process "gpg" -ArgumentList $arguments -Wait
# Suppression du fichier non crypté
Remove-Item $cheminFichier
#Write-Host "Fichier crypté créé et fichier original supprimé."
}
}
# Associer l'action de création de fichier au watcher
Register-ObjectEvent $watcher "Created" -Action $actionCréationFichier
# Attendre que le script soit terminé
try {
while ($true) {
# Boucle infinie pour maintenir le script en cours d'exécution
Start-Sleep -Seconds 1
}
} finally {
# Une fois la boucle terminée, supprimer l'observateur
Unregister-Event -SourceIdentifier FileSystemWatcher.Created
$watcher.Dispose()
}