darkoperator / Posh-SSH

PowerShell Module for automating tasks on remote systems using SSH
BSD 3-Clause "New" or "Revised" License
980 stars 227 forks source link

Invoke-SSHStreamShellCommand is hanging. #594

Open SunsparcSolaris opened 4 days ago

SunsparcSolaris commented 4 days ago

Using this module to capture and back up running configs on various HP/Aruba network switches. It runs fine under my account, but when I change over to using the service account that is running it as a scheduled task, the Invoke step hangs indefinitely.

$formattedDate = get-date -format 'yyyyMMdd'
$backupPath = "tftp <IP-ADDRESS>"
$configFile = $switch + ".txt"
$logFile = "C:\TFTP\Logs\"+$switch+"_"+$formattedDate+".log" 
$connection = New-SSHSession -ComputerName $switch -Credential $Cred -AcceptKey:$True -ErrorAction Stop -ConnectionTimeout 120
$session = Get-SSHSession -ComputerName $switch
$stream = New-SSHShellStream -Index $session.sessionid -TerminalName "dumb"
$sshCommand = @"
A
en
<OPERATORUSERNAME>
<OPERATORPASSWORD>
copy startup-config $backupPath $configFile 
Logout
Y
Y
"@
$streamoutput = Invoke-SSHStreamShellCommand -ShellStream $stream -Command $sshCommand -Timeout 10
((($streamoutput -replace '\x1b\[[0-9;]*[a-z]', '').replace("[?25h","")).replace("[?6l[?7h","")).replace("[?25l","") | out-string | Out-File $logFile

Both accounts have the same permissions on the computer running the script and on the switch being backed up. I don't get any sort of output, just hangs.

darkoperator commented 4 days ago

When running as a schedule task I would proba use an iStore for the fingerprints since there is not user folder. But you say it hangs on execution .. in that case to be honest no clue.