- This topic has 2 replies, 2 voices, and was last updated 1 month, 4 weeks ago by
July 24, 2020 at 5:46 am #244478ParticipantTopics: 1Replies: 1Points: 17Rank: Member
I am trying to write a script to backup the config from several HP switches and copy it to a ftp server.
My problem is if I execute every command of the script one by one in the powershell it works and I get a txt file on the ftp server with the config in it. But if I run the whole script at once as a ps1 file the script runs and finishes but it does not execute the commands in the shell stream. I think I would have to send a return (execute) command in the shell stream after the command but I do not know how to do this in powershell. Here is my script:
New-SSHSession -ComputerName $ip -Credential (Get-Credential) -AcceptKey</p>
$session = Get-SSHSession -ComputerName $ip
$stream = New-SSHShellStream -Index $session.SessionID
$stream.WriteLine("Copy running-config tftp NOACTUALIP outtaNoida.txt pc")
$result = $stream.Read()
Remove-SSHSession -Index 0
$ip = "10.66.12.119"
So I think after the commands like conf t and Copy Running I would need to send a Return. Does anyone know how to do this and can help? I tried $stream.flush and $stream.writeLine(“\n”) and $stream.WriteLine(“`n”) but those commands did not work.
With kind regards,
July 29, 2020 at 12:49 am #245490
July 29, 2020 at 2:18 am #245520ParticipantTopics: 1Replies: 1Points: 17Rank: Member
my mistake was that the script executes the commands too fast and does not wait till the previous command is finished.
So between $stream.WriteLine("Copy running-config tftp NOACTUALIP outtaNoida.txt pc")
and Remove-SSHSession -Index 0 I needed a Start-Sleep -s 5
otherwise it kills the ssh-Session before the tftp copy command is finished. The sleep command gives the previous command the time it needs.
With kind regards,
- You must be logged in to reply to this topic.