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:
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