好的,我创建了一个PS脚本,它似乎在其中某个地方中断,需要帮助理解它为什么跳过一个函数。我对脚本很陌生,我不知道如何调试,所以任何帮助都是很好的。
脚本功能:
当脚本运行时,它要么跳过解密进程,要么不解密所有文件。如果我单独运行代码,它将正确地解密文件。如果我手动运行执行步骤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"
}
}发布于 2020-11-06 21:37:18
实际上,在这个发布的代码中没有任何功能。
永远不要运行破坏性代码(创建、新建、更新、修改、移动、删除.)不用先检查一下自己。这就是为什么存在-WhatIf参数。
无论如何,试试这个重构的代码。
$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"}
}发布于 2020-11-06 21:37:33
所以它正确地解密了文件,但是后一部分失败了。
我相信这是由于行$outfile = $file.BaseName以及脚本的后半部分是根据文件扩展名搜索文件的事实。BaseName返回文件名,减去扩展名,因此如果文件名为banana.csv $file.BaseName,则返回banana
https://stackoverflow.com/questions/64719985
复制相似问题