The following will create a folder in the users home called TestFolder, upload a file to that folder, then runs a command deleting teh folder:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | param( [parameter(Mandatory = $true )] [string] $serverName , [parameter(Mandatory = $true )] [string] $userName , [parameter(Mandatory = $true )] [string] $password ) [Void][Reflection.Assembly]::LoadFrom( ".\Renci.SshNet.dll" ) $sourceFile = "c:\Scripts\test.txt" $destFile = "test.txt" $destFolder = "/home/$userName/TestFolder" $sshclient = New-Object Renci.SshNet.SshClient( $serverName , $userName , $password ) $sshclient .Connect() if ( $sshclient .IsConnected){ $sftp = New-Object Renci.SshNet.SftpClient( $sshclient .ConnectionInfo) $sftp .Connect() if ( $sftp .IsConnected){ if (! $sftp .Exists( $destFolder )){ $sftp .CreateDirectory( $destFolder ) } $sftp .ChangeDirectory( $destFolder ) $sftp .UploadFile([System.IO.File]::OpenRead( $sourceFile ), $destFile ) write-host "File Exists " $([bool](( $sftp .ListDirectory( "." ) | select Name) -match "test.txt" )) $sftp .Disconnect() } $o = $sshclient .RunCommand( "rm /home/$userName/TestFolder -R" ) $o .Result $sshclient .Disconnect() } |
No comments:
Post a Comment