Abjt G
Abjt G

Reputation: 39

Using PowerShell script to Encrypt files

I want to automate the encryption files placed in a folder using GPG on Windows Server 2019. I tried calling gpg.exe from the script but it doesn't create any output files. I also tried using debug & verbose to check values that are being set. There is no exception after gpg.exe is called, but no output files are generated.

[CmdletBinding()]
param()

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

$key      = "[email protected]"
$GpgPath  = "C:\Program Files (x86)\GnuPG\bin\gpg.exe"
$localuser = "[email protected]"
$outputdir = "D:\ToBeEncrypted\Output\"
$archivedir = "D:\ToBeEncrypted\Archive\"
$passPhrase = "password"
$sourcePath = "D:\ToBeEncrypted\*.dat"

$filearray  = Get-ChildItem -Path $sourcePath

foreach ($file in $filearray) {

$fileName = $file.Name

$encryptedFileName = "$outputdir" + $fileName + ".txt"

Write-Verbose "Encrypted name: $encryptFileName"

try
    {
    Write-Verbose "GPG Execution Started $file to $encryptedFileName"
    Start-Process -FilePath $GpgPath -ArgumentList "--pinentry-mode=loopback --passphrase $passPhrase --compress-algo 1 --cipher-algo cast5 --armor --recipient $key --local-user $localuser --output $encryptedFileName -se $file"
}
 catch [Exception]
{
    Write-Verbose $_.Exception.Message
    exit 1
}
Write-Verbose "GPG Execution Completed"

}

What am I doing wrong in this script?

Upvotes: 0

Views: 1967

Answers (0)

Related Questions