首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell脚本问题-文件不会自动用PGP解密

PowerShell脚本问题-文件不会自动用PGP解密
EN

Stack Overflow用户
提问于 2020-11-06 18:38:02
回答 2查看 1.2K关注 0票数 0

好的,我创建了一个PS脚本,它似乎在其中某个地方中断,需要帮助理解它为什么跳过一个函数。我对脚本很陌生,我不知道如何调试,所以任何帮助都是很好的。

脚本功能:

  1. 将PGP加密文件从外部服务器拉到特定文件夹
  2. 用.csv扩展名解密PGP加密文件和输出
  3. 将原始文件移动到特定的归档文件夹。
  4. 将解密文件复制到SharePoint联机库
  5. 将解密文件移动到特定的归档文件夹

当脚本运行时,它要么跳过解密进程,要么不解密所有文件。如果我单独运行代码,它将正确地解密文件。如果我手动运行执行步骤2-5的代码部分,它将无法工作。

我的目标是确保它将在没有干预的情况下处理步骤1-5,并以某种方式将验证放入脚本中,这一点我还没有学会如何做。

验证目标:

  1. 确认文件已被取出
  2. 对于每个文件,确认已经创建了一个解密文件。
  3. 一旦复制到SPO,确认所有解密的文件都在SPO中。

有人能帮助我或指点我如何修复我的脚本、调试和/或如何在脚本中执行验证吗?我将不胜感激!!

代码语言:javascript
复制
    $files = Get-ChildItem -Path "\\WebDrive\clientSFTP\From_client\*.*" -File
foreach ($file in $files)
{
    #Write-Output "Retrieving File: $file"
    Write-Output "$file  TO \\servername\records\client\Received\" 
    
    try
    {
            Move-Item -Path "$file" "\\servername\records\client\Received\" -Force 
}
catch
{
    Write-Output "Error pulling down the files. ($file)"
}

}

#Pause script for 10 seconds
Start-Sleep 10
#If I run the below all together it still fails.
#-----------------------    
$DestPath = "\\servername\records\client\Received"
$files = Get-ChildItem $DestPath -File
ForEach($file in $files)
{
   # try
    #{
        $outfile = $file.BaseName
        Write-Output "PGP Procesing: Input=$DestPath\$file, Output=$DestPath\$outfile"
        Start-Process -FilePath "C:\Program Files (x86)\GnuPG\bin\gpg.exe" -WorkingDirectory "$DestPath" -ArgumentList "--pinentry-mode=loopback --passphrase passwordhere -o $DestPath\$outfile $DestPath\$file"

    #}
    #catch
    #{
     #   Write-Output "Error during PGP decryption of: $file"
    #}
}
#---------------
#If I run the lines of code between the #------ it will decrypt all the files correctly.

$files = Get-ChildItem "$DestPath\*.csv" -File
ForEach($file in $files)
{
    try {
        Copy-Item -Path "$file" -Destination "\\WebDrive\SpoProfile\Shared Documents\client\02.From_client\" -Force
        Move-Item -Path "$file" -Destination "\\servername\records\client\Received\Archive\Decrypted\" -Force

    }
    catch
    {
        Write-Output "Error Moving file: $file"
    }

}

$files = Get-ChildItem "$DestPath\*.pgp" -File
ForEach($file in $files)
{
    try 
    {
        Move-Item -Path "$file" -Destination "\\servername\records\client\Received\Archive\Original\" -Force
    }
    catch
    {
        Write-Output "Error Moving file: $file"
    }

}
EN

回答 2

Stack Overflow用户

发布于 2020-11-06 21:37:18

实际上,在这个发布的代码中没有任何功能。

永远不要运行破坏性代码(创建、新建、更新、修改、移动、删除.)不用先检查一下自己。这就是为什么存在-WhatIf参数。

无论如何,试试这个重构的代码。

代码语言:javascript
复制
$DestPath = "\\$ServerName\records\client\Received"
$WebDrive = 'PathToSource'

Get-ChildItem -Path "\\$WebDrive\clientSFTP\From_client\*.*" -File |
ForEach-Object {
    Write-Output "$PSItem  TO \\$ServerName\records\client\Received\" 
    
    Try
    {
        $moveItemSplat  = @{
            Destination = "\\$ServerName\records\client\Received\"
            Path        = $PSItem
            Force       = $true
            ErrorAction = 'Stop'
        }
        Move-Item @moveItemSplat -WhatIf
    }
    Catch {Write-Warning -Message "Error pulling down the files. $PSItem"}
}

Start-Sleep 10


Get-ChildItem $DestPath -File | 
ForEach-Object {
    Try
    {
        $CurrentErrorActionPreference = $ErrorActionPreference
        $ErrorActionPreference        = 'Stop'

        "PGP Procesing: Input=$DestPath\$PSItem, Output=$DestPath\$($PSItem.FullName)"
        $startProcessSplat   = @{
            FilePath         = 'C:\Program Files (x86)\GnuPG\bin\gpg.exe'
            ArgumentList     = "--pinentry-mode=loopback --passphrase passwordhere -o $DestPath\$($PSItem.FullName) $DestPath\$PSItem"
            WorkingDirectory = $DestPath
        }
        Start-Process @startProcessSplat
    }
    Catch {Write-Warning -Message "Error during PGP decryption of: $PSitem"}
    Finally {$ErrorActionPreference = $CurrentErrorActionPreference}
}

Get-ChildItem "$DestPath\*.csv" -File | 
ForEach-Object {
    Try
    {
        $CurrentErrorActionPreference = $ErrorActionPreference
        $ErrorActionPreference        = 'Stop'

        $copyItemSplat  = @{
            Destination = "\\$WebDrive\SpoProfile\Shared Documents\client\02.From_client\"
            Path        = $PSItem.FullName
            Force       = $true
        }
        Copy-Item @copyItemSplat

        $moveItemSplat  = @{
            Destination = "\\$ServerName\records\client\Received\Archive\Decrypted\"
            Path        = $PSItem.FullName
            Force       = $true
        }
        Move-Item @moveItemSplat -WhatIf

    }
    Catch {Write-Warning -Messate "Error Moving file: $file"}
    Finally {$ErrorActionPreference = $CurrentErrorActionPreference}
}

Get-ChildItem "$DestPath\*.pgp" -File | 
ForEach-Object {
    Try
    {
        $moveItemSplat  = @{
            Destination = "\\$ServerName\records\client\Received\Archive\Original\"
            Path        = $PSItem.FullName
            Force       = $true
            ErrorAction = 'Stop'
        }
        Move-Item @moveItemSplat -WhatIf
    }
    Catch {Write-Warning -Messate "Error Moving file: $file"}
}
票数 0
EN

Stack Overflow用户

发布于 2020-11-06 21:37:33

所以它正确地解密了文件,但是后一部分失败了。

我相信这是由于行$outfile = $file.BaseName以及脚本的后半部分是根据文件扩展名搜索文件的事实。BaseName返回文件名,减去扩展名,因此如果文件名为banana.csv $file.BaseName,则返回banana

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64719985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档