HP Switch Backup config

Hi everbody,

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:

Function copyrunningConf{
Param([String] $ip)
New-SSHSession -ComputerName $ip -Credential (Get-Credential) -AcceptKey</p>
$session = Get-SSHSession -ComputerName $ip
$stream = New-SSHShellStream -Index $session.SessionID
$stream.WriteLine("Conf T")
$stream.WriteLine("Copy running-config tftp NOACTUALIP outtaNoida.txt pc")
$result = $stream.Read()
Write-Output $result
Remove-SSHSession -Index 0
}
$ip = "10.66.12.119"
copyrunningConf $ip

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,

Arnold

Hello Arnold,
Welcome to PowerShell.org
Please read “Read Me Before Posting! You’ll be Glad You Did” and use proper formatting for your code.

Have you tried Invoke-SSHCommand<!–more–>

Hope that helps.

Hi everbody,

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,

 

Arnold