问题描述
好吧,我创建的PS脚本似乎在其中的某个位置中断,需要帮助了解为什么它会跳过功能。我是脚本的新手,也不知道如何调试,因此任何帮助都将非常有用。
脚本功能:
- 将PGP加密文件从外部服务器拉到特定文件夹
- 解密带有.csv扩展名的PGP加密文件和输出
- 将原始文件移动到特定的存档文件夹
- 将解密的文件复制到SharePoint Online库
- 将解密的文件移动到特定的存档文件夹
脚本运行时,它要么跳过解密过程,要么不对所有文件解密。如果我自己运行代码,它将正确解密文件。如果我手动运行执行第2-5步的代码部分,它将无法正常工作。
我的目标是确保在不进行干预的情况下执行第1-5步,并以某种方式将验证放入脚本中,我尚未学习如何做。
验证目标:
有人可以帮助我或指导我如何修复脚本,调试和/或如何在脚本中进行验证吗?我将不胜感激!
$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"
}
}
解决方法
此发布代码中实际上没有任何功能。
切勿在未事先检查自己的情况下运行破坏性代码(创建,新建,更新,修改,移动,删除...)。 This is why the -WhatIf parameter exists.
无论如何,请尝试使用此重构代码。
$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"}
}
,
因此它可以正确解密文件,但是后一部分失败。
我相信这是由于行$outfile = $file.BaseName
和脚本的后半部分正在基于文件扩展名搜索文件的事实。 BaseName
返回文件名,减去扩展名,因此,如果您的文件名为banana.csv
$file.BaseName
将返回banana