0

I was able to add the function and run it, however, it only encrypted a single file out of 3 when I expected it to do the entire folder. From reading the function it looks like it should do the entire folder. Below is the function and my script.

Add-Encryption Function

[CmdletBinding()]
[OutputType([System.IO.FileInfo])]
param
(
    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [ValidateScript({Test-Path -Path $_ -PathType Container})]
    [string]$FolderPath,

    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string]$Password,

    [Parameter()]
    [ValidateNotNullOrEmpty()]
    [string]$GpgPath = 'C:\Program Files (x86)\GnuPG\bin\gpg.exe'

)
process {
    try
    {
        Get-ChildItem -Path $FolderPath | foreach {
            Write-Verbose -Message "Encrypting [$($_.FullName)]"
            Start-Process -FilePath $GpgPath -ArgumentList "--batch --passphrase $Password -c $($_.FullName)" -Wait -NoNewWindow
        }
        Get-ChildItem -Path $FolderPath -Filter '*.gpg'
    }
    catch
    {
        Write-Error $_.Exception.Message
    }
}

Calling the Function and Output

$EncryptionTarget = "C:\Users\myself\Documents\Files-ToEncrypt"
$Passphrase = "Password1"

Add-Encryption $EncryptionTarget -Password $Passphrase

Output

Mode                LastWriteTime         Length Name                                                                                                                                 
----                -------------         ------ ----                                                                                                                                 
-a----       10/24/2019   4:43 PM             79 test.docx.gpg     

I expected all 3 test documents to come out.

I have 3 documents within

$EncryptionTarget = "C:\Users\myself\Documents\Files-ToEncrypt"

I only get the 1st document encrypted.

DrixlRey
  • 205
  • 3
  • 4
  • 13
  • As an aside: if you want to run a console application such as `gpg.exe` synchronously, invoke it directly - don't use `Start-Process` - see https://stackoverflow.com/a/51334633/45375 – mklement0 Oct 25 '19 at 02:38
  • You mention 3 test documents, but they're not part of your question; please try to provide a [mcve]. – mklement0 Oct 25 '19 at 02:41
  • @mklement0 When I called the function at `EncryptionTarget = "C:\Users\myself\Documents\Files-ToEncrypt"` There are 3 files in there. The script encrypts file #1 in that folder instead of all of them which is what the function is trying to do. – DrixlRey Oct 25 '19 at 15:26
  • I understand that that's the _intent_ of your code, but my point was that unless you provide code that reproduces the problem, you're unlikely to find help. As it stands, someone who wanted to help you would have to install `gpg.exe`, create sample input files, ... - and I'm not surprised that no one has volunteered. – mklement0 Oct 28 '19 at 22:21

0 Answers0