GetSFTPFilesCmdlet.cs
using Renci.SshNet;
using Renci.SshNet.Sftp; using System; using System.Collections.Generic; using System.Management.Automation; using System.Text; namespace SFTPSyncer { [Cmdlet(VerbsCommon.Get, "SFTPFiles")] [OutputType(typeof(IEnumerable<SftpFile>))] public class GetSFTPFilesCmdlet : Cmdlet { [Parameter(Position = 0, Mandatory = true)] [ValidateNotNullOrEmpty()] public SftpClient Connection { get; set; } [Parameter(Position = 1, Mandatory = false)] public string Directory { get; set; } static IEnumerable<SftpFile> ListDirectory(SftpClient client, String dirName) { foreach (var entry in client.ListDirectory(dirName)) { if (entry.Name != "." && entry.Name != "..") { if (entry.IsDirectory) { foreach(var item in ListDirectory(client, entry.FullName)) { yield return item; } } else { yield return entry; } } } } protected override void ProcessRecord() { base.ProcessRecord(); foreach(var item in ListDirectory(Connection, string.IsNullOrEmpty(Directory)?".": Directory)) { WriteObject(item); } } } } |