Powershell - bulk decryption of files

%3CLINGO-SUB%20id%3D%22lingo-sub-2780007%22%20slang%3D%22en-US%22%3EPowershell%20-%20bulk%20decryption%20of%20files%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2780007%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20someone%20with%20some%20scripting%20skills%20can%20help%20here.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20using%20openssl%20to%20encrypt%20and%20decrypt%20files.%20I%20can%20decrypt%20single%20files%20via%20command%20line%20but%20the%20problem%20comes%20when%20I'm%20trying%20to%20bulk%20decrypt%20using%20a%20wildcard%20like%20*.crypt%20in%20the%20command%20line%20doesn't%20work.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3Eopenssl.exe%20cms%20-decrypt%20-inkey%20C%3A%2Fkey.pem%20-recip%20C%3A%2Fcert.pem%20-inform%20DER%20-in%20%22E%3A%2F*.crypt%22%20-out%22%20E%3A%2F*.xml%22%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EI'm%20unable%20to%20find%20a%20parameter%20to%20do%20a%20bulk%20decrypt%20s%3C%2FSPAN%3Eo%20I%20thought%20hey%20I'll%20use%20powershell%20to%20decrypt%20each%20file%2C%20maybe%20something%20like%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24file%20%3D%20*.crypt%3C%2FP%3E%3CP%3Efor%20each%20file%20in%20C%3A%5Cfolder%5C*.crypt%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%7B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3CRUN%20command%3D%22%22%3E%20%24file%3C%2FRUN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ecan%20anyone%20help%20with%20how%20I%20can%20script%20this%20in%20PS%20or%20point%20me%20in%20the%20right%20direction%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20I%20need%20some%20help%20to%20flesh%20it%20out%20as%20I'm%20hopeless%20with%20scripting%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2780007%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2905315%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20-%20bulk%20decryption%20of%20files%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2905315%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F870165%22%20target%3D%22_blank%22%3E%40Chi_L%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%20Chi.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20a%20bit%20late%20but%20may%20help%20someone%20with%20a%20similar%20question.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20simple%20one-liner%20(spread%20out%20a%20bit%20for%20readability)%20would%20likely%20suffice.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E(Get-ChildItem%20-Path%20%22D%3A%5CData%5C*.crypt%22).FullName%20%7C%20%25%20%7B%0A%20%20%20%20openssl.exe%20cms%20-decrypt%20-inkey%20C%3A%5Ckey.pem%20-recip%20C%3A%5Ccert.pem%20-inform%20DER%20-in%20%22%24_%22%20-out%20%22%24(%24_%20-replace%20'%5C.crypt%24'%2C%20'.xml')%22%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

Hi all,

 

Hope someone with some scripting skills can help here.

 

I'm using openssl to encrypt and decrypt files. I can decrypt single files via command line but the problem comes when I'm trying to bulk decrypt using a wildcard like *.crypt in the command line doesn't work. 

 

openssl.exe cms -decrypt -inkey C:/key.pem -recip C:/cert.pem -inform DER -in "E:/*.crypt" -out" E:/*.xml"

 

I'm unable to find a parameter in openssl to do a bulk decrypt so I thought hey I'll use powershell to decrypt each file, maybe something like

 

$file = *.crypt

for each file in C:\folder\*.crypt

     { 

      <run command> $file

      }

 

can anyone help with how I can script this in PS or point me in the right direction. Or even tell me if this is even possible.

 

Thanks

 

1 Reply

@Chi_L 

 

Hi, Chi.

 

This is a bit late but may help someone with a similar question.

 

This simple one-liner (spread out a bit for readability) would likely suffice.

 

(Get-ChildItem -Path "D:\Data\*.crypt").FullName | % {
    openssl.exe cms -decrypt -inkey C:\key.pem -recip C:\cert.pem -inform DER -in "$_" -out "$($_ -replace '\.crypt$', '.xml')";
}