I am trying to use Powershell within a SQL Server Agent job to download a zip file. The script uses PuTTY (PSCP.exe) to download a zip file from a SFTP site.
The issue I am having is that when the job runs it connects to the SFTP site and PuTTY sends a prompt back about storing the server's host key in the registry. I don't want to do this so I am trying to pipe the echo n
command to PuTTY. This doesn't seem to be working though.
$SrcPath = "/somedirectory/somewhere/files/"
$DstPath = "D:\Download\"
$currentDate = (Get-Date).ToString('yyyyMMdd')
$FileName = "$currentDate.zip"
$ArchivePath = "D:\Archive\"
$File = "$SrcPath$FileName"
Set-Location $DstPath
echo n | C:\"Program Files (x86)"\PuTTY\pscp -P 99999 -pw password username@{IP_ADDRESS}:$File $DstPath
# Check the file is there
If (Test-Path "$DstPath$FileName")
{
# Unzip the contents
C:\"Program Files"\7-Zip\7z.exe e "$DstPath$FileName"
#Move the zip file to the archive directory
Move-Item $DstPath$FileName $ArchivePath -force
}
If I open up a command window and do this manually it works fine.
e.g. Typing this in the command window
C:\"Program Files (x86)"\PuTTY\pscp -P 99999 -pw password username@{IP_ADDRESS}:/somedirectory/somewhere/files/20121025.zip D:\Download\
and then pressing n
when the prompt appears, downloads the files correctly.
When the job runs via SQL Agent, it errors initially complaining about the host key and then states that the file does not exist.
Can anyone point me in the right direction?