0

Two issues I experience are,

  1. It does the job but throws the error shown below.

  2. The notification doesn't fire, the condition is not met at all.

gpg2.exe : gpg: encrypted with 2048-bit RSA key, ID D9A0AD5F, created
2017-07-04
At C:\Scripts\FinanceGPGDecryptor.ps1:24 char:5
+    & <<<< $gpg_prog --batch --yes --passphrase "$gpg_p" -r "$gpg_r" -o "$out_file" -d "$in_file"
    + CategoryInfo          : NotSpecified: (gpg: encrypted ...ated 2017-07-04:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

      "key_id>"
# Variables
$gpg_prog = "C:\Program Files (x86)\GNU\GnuPG\pub\gpg2.exe"
$sFolder = "J:\Receive\"
$dFolder = "J:\Receive\Decrypted\"
$aFolder = "J:\Receive\Archived\"
$Log = "J:\Receive\decrypt.log"
$gpg_p = "****"
$gpg_r = "key_id"

$recipients = @("alias@company.com")

$date = date;
Add-Content $Log $date;
Set-Location J:\

# gpg wrapper
function gpg_me {
    $in_file = $sFolder + $args[0] + ".pgp"
    $out_file = $dFolder + $args[0] + ".txt"

    #Write-Host $in_file " | " $out_file
    & $gpg_prog --batch --yes --passphrase "$gpg_p" -r "$gpg_r" -o "$out_file" -d "$in_file"

    if (!$LastExitCode) {
        Add-Content $Log "Inbound file: $in_file | Outbound file: $out_file";

        Move-Item -Path $in_file -Destination $aFolder -force
        #echo "all good" $LastExitCode
    }
}

# Loop thru current directory.
$items = Get-ChildItem -Path $sFolder
foreach ($item in $items) {
    #if the item is NOT a directory, then process it.
    if ($item.Attributes -ne "Directory") {
        #Write-Host "file found:" $item
        if ($Output = $item -like "*.pgp") {
            #Write-Host "Decrypting new file "  $item "'" $Output
            gpg_me $item.BaseName
        } else {
            # // File does not exist
            # Write-Host $item.Name " is not a file for decrypting:"
        }
    }
}

Add-Content $Log  "`n **********`n";

Write-Host "LastExitCode:" $LastExitCode
Write-Host "Output:" $Output

if ($Output -and !$LastExitCode) {
    Send-MailMessage -From "noreply@compnay.com" -SmtpServer smtp.company.com -To "$recipients" -Subject "file  received and decrypted" -Body "Decrypted file can be found at $dFolder" -Attachment $Log;
} else{
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Rich
  • 1
  • Please reformat your post so it's clear where the error message(s) start and end. Note that some characters – like asterisks or plus signs followed by a space – will trigger bullet points in Markdown. See https://stackoverflow.com/editing-help for tips for formatting your posts. – Kay Jul 22 '18 at 13:58
  • Looks like a duplicate of these: https://stackoverflow.com/q/18380227/478656 https://stackoverflow.com/q/2095088/478656 - if your native command (e.g. an EXE) writes to `stderr`, PowerShell assumes it had an error, and throws an exception so you can fix it. But many programs write data to `stdout` and then use the only other available output stream - `stderr` - for all other information, rather than just errors, even when things are fine. – TessellatingHeckler Jul 22 '18 at 16:38

0 Answers0