1

Been trying to connect to an sftp using posh-ssh. Works fine when i run it, when i try running it through windows task scheduler it does not work.

I have tried switching around the users that are to run the script such as Admin, myself, system, etc.

I have tried using keys to save the password and decrypt it later on.

$Password = ConvertTo-SecureString 'sftpPW' -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($SFTPUser, $Password) 
$sftpIP = 'hostIP'
$sftpSess = New-SFTPSession -ComputerName $SftpIp -Credential $Credential

And then other method VVVVVVVVVV this part is run not in the script but is to create a password

Get-Random -Count 32 -InputObject (0..255) | Out-File -FilePath encrypt
ConvertTo-SecureString 'sftpPW' -AsPlainText -Force | convertFrom-secureString -key (get-content -path encrypt) | Out-File pw

^^^^^^^^^^

$SFTPUser="user"
$password = (cat pw | ConvertTo-SecureString -key (Get-Content -path encrypt))
$Credential = New-Object System.Management.Automation.PSCredential($SFTPUser, $Password) 
$sftpIP = 'hostIP'
$sftpSess = New-SFTPSession -ComputerName $SftpIp -Credential $Credential

edit*

Import-Module Posh-SSH
$datestuff = Get-Date
echo "start $datestuff" > C:\sftptestlog
$SFTPUser="user"
echo $sftpuser >> C:\sftptestlog
$Password = ConvertTo-SecureString 'sftpPW' -AsPlainText -Force
echo $password >> C:\sftptestlog
$Credential = New-Object System.Management.Automation.PSCredential($SFTPUser, $Password) 
$sftpIP = 'hostip'

echo $Credential >> C:\sftptestlog
echo $sftpip >> C:\sftptestlog
$sftpSess = New-SFTPSession -ComputerName $SftpIp -Credential $Credential
echo $sftpSess >> C:\sftptestlog

expected output from sftptestlog

start 09/10/2019 16:31:19

user

System.Security.SecureString

UserName Password

-------- --------

user System.Security.SecureString

hostIP

SessionId Host Connected

--------- ---- ---------
2 hostIP True

above output only happens when the script is run from the command line. When using scheduler the output is the same only the last lines (starting from sessionID) are not printed out.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Adlis
  • 95
  • 1
  • 11
  • Sorry for the delayed response. When i try the line $sftpSess = New-SFTPSession -ComputerName $SftpIp -Credential $Credential nothing happens. when i print out said line it will usually give me the session details ID etc. When i print it out in the script using the scheduler nothing prints. – Adlis Sep 03 '19 at 16:17
  • ```Import-Module Posh-SSH $datestuff = Get-Date echo "start $datestuff" > C:\sftptestlog $SFTPUser="user" echo $sftpuser >> C:\sftptestlog $Password = ConvertTo-SecureString 'sftpPW' -AsPlainText -Force echo $password >> C:\sftptestlog $Credential = New-Object System.Management.Automation.PSCredential($SFTPUser, $Password) echo $Credential >> C:\sftptestlog $sftpIP = 'hostIP' echo $sftpip >> C:\sftptestlog $sftpSess = New-SFTPSession -ComputerName $SftpIp -Credential $Credential echo $sftpSess >> C:\sftptestlog``` – Adlis Sep 03 '19 at 21:01
  • got this exception when i put the try catch around ```New-SFTPSession``` the exception is ```The term 'New-SFTPSession' is not recognized as the name of a cmdlet, function, script file, or operable program. Check t he spelling of the name, or if a path was included, verify that the path is correct and try again.``` Could this just be from the system not knowing where the module is when i have it run and use Posh-SSH versus when i just run it from my machine via command line? – Adlis Sep 11 '19 at 16:42
  • Should probably start this is a new server. Its been a while but i believe i installed it using ```Install-Module -Name Posh-SSH -RequiredVersion 2.1```. I have started to search around more and posted out what the ```$ENV:PSModulePath``` would look like and it seems to point to the correct paths ```C:\Users\markki\Documents\WindowsPowerShell\Modules``` when run through the scheduler. – Adlis Sep 11 '19 at 17:10
  • Yes currently i have it running under my account with the "run with highest privileges" mark checked – Adlis Sep 11 '19 at 17:14
  • Hello Martin, thanks for leading me the correct way. I have found out what was wrong. It seems that the module wasn't in the $ENV:PSModulePath like i thought it was. I directly added the path ```Import-Module "C:\Program Files\WindowsPowerShell\Modules\Posh-SSH"``` and now it is working. – Adlis Sep 11 '19 at 17:22

4 Answers4

1

I'd highly recommend you put some logging into your script so that you can diagnose this sort of thing as the task scheduler event log won't help very much at all. An easy way is to use Start-Transcript

# Put this line at the very start of your script
Start-Transcript -Path 'c:\temp\sftplog.txt'

Write-Host 'Setting up user credentials'
$Password = ConvertTo-SecureString 'sftppassword' -AsPlainText -Force
$SFTPUser="username"
$Credential = New-Object System.Management.Automation.PSCredential($SFTPUser, $Password) 

# Destination host
$sftpIP = 'hostip'
Write-Host 'Destination host is $sftpIP, testing availability'
Try {
    Test-Connection -ComputerName $sftpIP -ErrorAction Stop
    Write-Host 'Host is contactable'
}
Catch {
    $_.Exception
}


# Attempt connection
Try {
    Write-Host 'Trying to connect via SFTP to $SftpIP'
    $sftpSess = New-SFTPSession -ComputerName $SftpIp -Credential $Credential -ErrorAction Stop
    Write-Host 'Done'
}
Catch {
    $_
}

# Put this line at the very end of your script
Stop-Transcript

You can remove the Transcript and/or all the Write-Host commands when you've diagnosed the issue.

Scepticalist
  • 3,737
  • 1
  • 13
  • 30
  • I have tried logging in my test script such as ```$Credential = New-Object System.Management.Automation.PSCredential($SFTPUser, $Password) echo $Credential >> C:\sftptestlog $sftpIP = 'testftp' echo $sftpip >> C:\sftptestlog $sftpSess = New-SFTPSession -ComputerName $SftpIp -Credential $Credential echo $sftpSess >> C:\sftptestlog``` however what i want in the $sftpSess does not print out anything, meaning that the session wasn't created (at least thats what i believe) Also recently I have put the Import-module POSH-SSH in and the connection still didn't happen – Adlis Sep 03 '19 at 16:21
  • It would help you immensely to stop treating the Powershell scripts like command line. If you're getting no output at all then it's likely that the scheduled task is not configured properly. – Scepticalist Sep 03 '19 at 21:24
  • When i do run the scheduled task though it performs everything else that is in the script that is around the SFTP such as writing to files. – Adlis Sep 03 '19 at 22:21
  • Then put some proper error trapping and logging in around the SFTP process.Until you do that you're guessing. – Scepticalist Sep 04 '19 at 07:16
  • Sorry for the late reply, I am currently running powershell version 4.0 so when I tried running the script with the Start-Transcript i got an error Start-Transcript : This host does not support transcription. – Adlis Sep 10 '19 at 23:14
1

Found out that the ENV:PSModulePath didn't have the path to where the module was installed when using task scheduler. When importing the module I linked directly to where the module was. Import-Module "C:\Program Files\WindowsPowerShell\Modules\Posh-SSH"

Adlis
  • 95
  • 1
  • 11
  • You don't need to explicitly import a module in PowerShell to use it. Just make sure it's installed using `-Scope AllUsers` to make it globally available (unless you are using PoSH v2). – Dennis Jan 25 '22 at 21:19
1

Figured this out after lots of head bashing. The issue is that the SSH connection waits for you to accept the SSL certificate as valid. So it pauses at the New-SFTPSession waiting for your input. If you run it as yourself, it only asks once so you probably clicked OK and never had to again.

No amount of error checking or importing the module is going to fix that.

Include the flags -AcceptKey -Force and it works like a champ.

New-SFTPSession -ComputerName $server -Credential $creds -AcceptKey -Force
Jesse B
  • 11
  • 1
0

To avoid issues with the path to the posh-ssh module, you can install it with:

install-module posh-ssh -Scope AllUsers