I have a Powershell 3.0 script that runs fine from the PS prompt on Windows 2008 R2, but when I execute it through an SQL Agent job in SQL 2012 I get different behavior. I was launching it as a powershell step, but since I was using features of 3.0
that didn't work, so I started launching it as a CMD job where I started Powershell with the script as an argument. The script runs to a point, but then goes astray. The script encrypts flat files created by an SSIS package then deletes the flat
files and moves the encrypted versions into an archive folder. When run from the Powershell prompt it works fine, but when run as a step in SQL Agent, it completes much faster than from the prompt and the files disappear. I can see the encrypted
files being created, so it seems to get that far, and then all the files suddenly disappear.
The paths are set outside the function and they look fine, and the files to process are passed in as an array. When running from SQL I found I had to hard-code the file paths to make it work, which is another minor issue. I have include the function
below.
Thanks in advance for any help you may be able to offer.
Function ArchiveFiles([string[]] $files)
{
# Path to executable
$exe = "C:\Program Files (x86)\GNU\GnuPG\gpg2.exe"
try
{
foreach($file in $files)
{
# Add command line arguments to an array, then execute the app with the array
$CmdArgs = "--batch", "--yes", "--encrypt", "--recipient", "info@exacttarget.com", ($file)
& $exe $CmdArgs
# Delete the clear-text source files from the current folder
Remove-Item -path $file
$file += ".gpg"
# Move the encrypted files into the archive folderMove-Item $file $ArchivePath -Force
}
Return $True}
catch
{
Return $False}
}
# End ArchiveFiles()