Powershell fails at pgp decryption

I am trying to decrpyt the automated pgp message with powershell however it drops an error message: The full error message is: gpg: can’t open `MyEncryptedFile.pgp’: No such file or directory
gpg: decrypt_message failed: No such file or directory
The bottleneck of this task is that I cannot install anything (such as GnuPG)

The sign and encryption part was done with command line commands:

<span class="pln">$path </span><span class="pun">=</span> <span class="str">"\\networkdrive\folder1\message"</span><span class="pln">
$step2files </span><span class="pun">=</span> <span class="typ">Get</span><span class="pun">-</span><span class="typ">ChildItem</span> <span class="pun">-</span><span class="typ">Recurse</span> <span class="pun">-</span><span class="typ">Path</span><span class="pln"> $path </span><span class="pun">|</span> <span class="typ">Where</span><span class="pun">-</span><span class="typ">Object</span><span class="pun">{</span><span class="pln">$_</span><span class="pun">.</span><span class="typ">Extension</span> <span class="pun">-</span><span class="pln">eq </span><span class="str">".asc"</span><span class="pun">}</span><span class="pln">
$z</span><span class="pun">=</span><span class="lit">1</span><span class="pln">
foreach </span><span class="pun">(</span><span class="pln">$files </span><span class="kwd">in</span><span class="pln"> $step2files</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
    $outputname </span><span class="pun">=</span> <span class="str">"$files.pgp"</span><span class="pln">
    $input </span><span class="pun">=</span><span class="pln"> $files
    $z 

    $options </span><span class="pun">=</span> <span class="str">" --output $outputname –-encrypt --recipient XYKey $files"</span><span class="pln">
    $options

    gpg  </span><span class="pun">--</span><span class="pln">output $outputname </span><span class="pun">--</span><span class="pln">encrypt </span><span class="pun">--</span><span class="pln">recipient </span><span class="typ">XYKey</span><span class="pln"> $input
    $z</span><span class="pun">++</span>
<span class="pun">}</span>

I need the response decrypted in a similar way and even though powershell finds all the respective files, the aforementioned error message displays.

The codepart is:

<span class="pln">$extracted </span><span class="pun">=</span> <span class="str">"\\networkdrive\folder1\extracted"</span><span class="pln">
$step5files </span><span class="pun">=</span> <span class="typ">Get</span><span class="pun">-</span><span class="typ">ChildItem</span> <span class="pun">-</span><span class="typ">Recurse</span> <span class="pun">-</span><span class="typ">Path</span><span class="pln"> $extracted </span><span class="pun">|</span> <span class="typ">Where</span><span class="pun">-</span><span class="typ">Object</span><span class="pun">{</span><span class="pln">$_</span><span class="pun">.</span><span class="typ">Extension</span> <span class="pun">-</span><span class="pln">eq </span><span class="str">".pgp"</span><span class="pun">}</span><span class="pln">
$z</span><span class="pun">=</span><span class="lit">1</span><span class="pln">
foreach </span><span class="pun">(</span><span class="pln">$files </span><span class="kwd">in</span><span class="pln"> $step5files</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
    $outputname </span><span class="pun">=</span> <span class="str">"$files.xml"</span><span class="pln">
    $input </span><span class="pun">=</span><span class="pln"> $files
    $z 

    $options </span><span class="pun">=</span> <span class="str">" -u KeyID --batch --yes --passphrase password --output $outputname --decrypt $files"</span><span class="pln">
    $options

    gpg  </span><span class="pun">-</span><span class="pln">u </span><span class="typ">KeyID</span> <span class="pun">--</span><span class="pln">batch </span><span class="pun">--</span><span class="pln">yes </span><span class="pun">--</span><span class="pln">passphrase password </span><span class="pun">--</span><span class="pln">output $outputname </span><span class="pun">--</span><span class="pln">decrypt $input
    $z</span><span class="pun">++</span>
<span class="pun">}</span>

Any idea?

It is not PS that is doing any encrypt/decrypt stuff, that’s all pgp.
You are also not using the full path name, which is critical in a recurse, to the file in your code. You are only hitting the name.

Meaning, this give the full path to the file.

...
foreach ($files in $step2files) {
    $outputname = "$($files.FullName).pgp"
    $input = $files.FullName
...
}

Thank you for the advice. Running the script with the suggested modification drops the following error(s) :

1
-u PGPpubKEY --batch --yes --passphrase password --output --decrypt file_19.pgp
gpg: can’t open `\\networkdrive\folder1\file_1.pgp’: No such file or directory
gpg: decrypt_message failed: No such file or directory
2
-u PGPpubKEY --batch --yes --passphrase password --output --decrypt file_19.pgp
gpg: can’t open `\\networkdrive\folder1\file_10.pgp’: No such file or directory
gpg: decrypt_message failed: No such file or directory
3
-u PGPpubKEY --batch --yes --passphrase password --output --decrypt file_19.pgp
gpg: can’t open `\\networkdrive\folder1\file_11.pgp’: No such file or directory
gpg: decrypt_message failed: No such file or directory

I don’t understand where it takes the extra backslashes from (in the location) and even though it finds all the files in the error messages, it tries to decrypt only the ‘file_19’ - there are 37 files in the given folder and I get this error message 37 times

I would suggest you start by looking at how the data looks in the $step5files variable.
Sounds like the input you expect doesn’t match what is in the variable.

That .fullname thing applies to both parts of your code post.

Try doing it as shown below, ensuring that you are including the $path and $extracted inline, since you are not specifying a work directory at all for the files as either code block. Well, you are not showing that you are.

$path = "\\networkdrive\folder1\message"

# as a check - use variable squeezing to see what is be returned, but still assign to the variable
($step2files = (Get-ChildItem -Path "$path" -Filter '*.asc' -Recurse).FullName)

$z=1

# What is the working directory here?
# Is it the same path as what is in $path?

foreach ($file in $step2files) 
{
    $outputname = "$path\$($file.pgp)"
    $input = $file
    $z 

    $options = " --output $outputname –-encrypt --recipient XYKey $file"
    $options

    gpg  --output $outputname --encrypt --recipient XYKey $input
    $z++
}


$extracted = "\\networkdrive\folder1\extracted"

# as a check - use variable squeezing to see what is be returned, but still assign to the variable
($step5files = (Get-ChildItem -Path "$extracted" -Recurse -Filter '*.pgp').FullName)

$z=1

foreach ($file in $step5files) 
{
    $outputname = "$extracted\$($file.xml)"
    $input = $file
    $z 

    $options = " -u KeyID --batch --yes --passphrase password --output $outputname --decrypt $file"
    $options

    gpg  -u KeyID --batch --yes --passphrase password --output $outputname --decrypt $input
    $z++
}

Those extra slashes are there because part of your call feels they need to be terminated. I don’t believe PS is causing this, but PGP might be.