Windows服务器上用PowerShell定时运行解密Jar脚本求助
Got it, let's put together a solid PowerShell solution that runs your PGP decrypt Jar, validates the result properly, and can be set up for regular execution on your Windows server. Here's a step-by-step breakdown:
1. Write a PowerShell Decryption Script
First, we'll create a script that defines your parameters clearly, runs the Jar command, and verifies both the output message and the resulting file. This makes maintenance easier and ensures you catch potential issues early.
Save this as PGPDecrypt.ps1 (adjust paths to match your server's setup):
# Define all decrypt parameters (update these paths/passcode as needed) $encryptedFilePath = "C:\Users\anirudgu\Desktop\abcd\TestFile.pgp" $privateKeyFilePath = "C:\Users\anirudgu\Desktop\abcd\anirudguprivatekey.asc" $outputFilePath = "C:\Users\anirudgu\Desktop\abcd\outputfile.txt" $passcode = "Passcode" $jarPath = "C:\Users\anirudgu\Desktop\abcd\PGPDecrypt.jar" # Use absolute path if Jar isn't in script's folder # Run the decrypt command and capture its output try { $decryptResult = java -cp $jarPath pgpDecryptPackage.PGPDecrypt $encryptedFilePath $privateKeyFilePath $outputFilePath $passcode Write-Host "Jar Execution Output:`n$decryptResult" -ForegroundColor Cyan } catch { Write-Host "Failed to execute the Jar command: $_" -ForegroundColor Red exit 1 } # Validate the decryption outcome $successFlag = "File decrypted successfully and placed at location : $outputFilePath" if ($decryptResult -match [regex]::Escape($successFlag)) { Write-Host "✅ Decryption completed successfully!" -ForegroundColor Green # Add extra validation to confirm the output file is valid if (Test-Path $outputFilePath -PathType Leaf) { $fileSize = (Get-Item $outputFilePath).Length if ($fileSize -gt 0) { Write-Host "✅ Output file exists and has content (size: $($fileSize / 1KB) KB)" -ForegroundColor Green } else { Write-Warning "⚠️ Output file exists but is empty - double-check the decrypt process" } } else { Write-Warning "⚠️ Decryption reported success, but output file was not found at the specified path" } } else { Write-Host "❌ Decryption failed! Jar output:`n$decryptResult" -ForegroundColor Red # Log the error for troubleshooting (adjust log path as needed) $errorLogPath = "C:\Users\anirudgu\Desktop\abcd\decrypt_errors.log" "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Decryption failed. Output: $decryptResult" | Out-File -Path $errorLogPath -Append Write-Host "Error logged to $errorLogPath" -ForegroundColor Yellow }
2. Test the Script Manually
Before setting up a scheduled task, run the script in PowerShell to confirm it works:
- Open PowerShell as Administrator
- Navigate to the folder where you saved
PGPDecrypt.ps1 - Run:
.\PGPDecrypt.ps1 - Check the output for green success messages, and verify the output file is created correctly.
3. Set Up Scheduled Execution (Windows Task Scheduler)
To run this script automatically on a schedule:
- Open Task Scheduler and click Create Task
- General tab:
- Name your task (e.g., "Auto PGP Decrypt")
- Check "Run whether user is logged on or not" (for unattended server use)
- Check "Run with highest privileges"
- Triggers tab:
- Click New to set your desired schedule (e.g., daily at 2 AM)
- Actions tab:
- Click New, select "Start a program"
- Program/script:
powershell.exe - Add arguments:
-ExecutionPolicy Bypass -File "C:\Path\To\Your\PGPDecrypt.ps1"(replace with your actual script path)
- Settings tab:
- Optional: Check "If the task fails, restart every [X] minutes" for reliability
- Click OK and enter your Windows credentials when prompted
Key Notes
- Ensure Java is installed and the
javacommand works in PowerShell (test withjava -version) - For sensitive passcodes, avoid storing plaintext in the script: use PowerShell's secure string feature to encrypt and store the passcode in a file, then decrypt it in the script
- If your Jar is in a different folder than the script, always use the absolute path for
$jarPathto avoid errors
内容的提问来源于stack exchange,提问作者Anirudh Gupta
相关产品推荐
相关产品推荐

