public override CommandResult Execute(CommandResult pipeIn) { string[] attributes = _arguments.Get <StringArgument>("Property").Value.Split(','); foreach (ResultRecord result in pipeIn) { ResultRecord newResult = new ResultRecord(); foreach (string attr in attributes) { if (result.ContainsKey(attr)) { newResult.Add(attr, result[attr]); } else { newResult.Add(attr, null); } } _results.Add(newResult); } return(_results); }
public override CommandResult Execute(CommandResult pipeIn) { string[] attributes = _arguments.Get <StringArgument>("Property").Value.Split(','); int first = _arguments.Get <IntegerArgument>("First").Value; if (pipeIn == null) { return(null); } bool wildcardSelect = attributes[0] == "*"; bool firstSet = first > 0; int counter = 0; foreach (ResultRecord result in pipeIn) { // Obey -First [int32] parameter and break if number is reached if (firstSet && counter == first) { break; } // If all attributes need to be taken if (wildcardSelect) { _results.Add(result); } // If specific attributes are selected else { ResultRecord newResult = new ResultRecord(); foreach (string attr in attributes) { if (result.ContainsKey(attr)) { newResult.Add(attr, result[attr]); } else { newResult.Add(attr, null); } } _results.Add(newResult); } counter++; } return(_results); }
public override CommandResult Execute(CommandResult pipeIn) { string[] properties = _arguments.Get <StringArgument>("Property").Value.Split(','); _results.Output = CommandResult.OutputType.List; if (pipeIn == null) { return(null); } foreach (ResultRecord result in pipeIn) { // Show all columns if (properties[0] == string.Empty) { _results.Add(result); } // Show only specific columns else { ResultRecord newResult = new ResultRecord(); foreach (string attr in properties) { if (result.ContainsKey(attr)) { newResult.Add(attr, result[attr]); } else { newResult.Add(attr, null); } } _results.Add(newResult); } } ResultPrinter.OutputResults(_results); return(_results); }
public static CommandResult BrowseFilesystem(string path, bool recurse, int depth, bool includeHidden, string searchPattern) { CommandResult results = new CommandResult(); DirectoryInfo gciDir = new DirectoryInfo(path); if (!gciDir.Exists) { throw new ItemNotFoundException(path); } // TODO: Follow symlinks. Skipping them for now if ((gciDir.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) { return(results); } DirectoryInfo[] directories; try { directories = gciDir.GetDirectories(recurse ? "*" : searchPattern); } catch (UnauthorizedAccessException) { Console.WriteLine("Unauthorized to access \"{0}\"", path); return(results); } FileInfo[] files = gciDir.GetFiles(searchPattern); // Enumerate directories foreach (DirectoryInfo dir in directories) { if (!includeHidden && ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)) { continue; } // Don't show directories if -Recurse and an -Include filter is set if (recurse && !string.IsNullOrEmpty(searchPattern)) { continue; } ResultRecord currentDir = new ResultRecord() { { "Mode", GetModeFlags(dir) }, { "LastWriteTime", dir.LastWriteTime.ToString() }, { "Length", string.Empty }, { "Name", dir.Name } }; // If recursive, also the directory name is needed if (recurse) { currentDir.Add("Directory", dir.FullName); } results.Add(currentDir); } // Enumerate files foreach (FileInfo file in files) { if (!includeHidden && ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)) { continue; } ResultRecord currentFile = new ResultRecord() { { "Mode", GetModeFlags(file) }, { "LastWriteTime", file.LastWriteTime.ToString() }, { "Length", file.Length.ToString() }, { "Name", file.Name } }; // If recursive, also the directory name is needed if (recurse) { currentFile.Add("Directory", file.Directory.FullName); } results.Add(currentFile); } // After adding folders and files in current directory, go depth first if (recurse && depth > 0) { foreach (DirectoryInfo subDir in directories) { // Skip hidden directories in case -Force parameter is not provided if ((subDir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden && !includeHidden) { continue; } CommandResult currentDir = BrowseFilesystem(subDir.FullName, recurse, depth - 1, includeHidden, searchPattern); results.AddRange(currentDir); } } return(results); }
public override CommandResult Execute(CommandResult pipeIn) { string[] attributes = _arguments.Get <StringArgument>("Property").Value.Split(','); int first = _arguments.Get <IntegerArgument>("First").Value; if (pipeIn == null) { return(null); } bool wildcardSelect = attributes[0] == "*"; bool firstSet = first > 0; // Simply return pipeIn if empty if (pipeIn.Count == 0) { return(pipeIn); } // Ignore casing List <string> correctAttributes = new List <string>(); foreach (string inputAttr in attributes) { bool found = false; // Locate case-insensitive match foreach (string key in pipeIn[0].Keys) { if (inputAttr.Equals(key, System.StringComparison.InvariantCultureIgnoreCase)) { correctAttributes.Add(key); found = true; break; } } // Add original non-existent column if no matching column found if (!found) { correctAttributes.Add(inputAttr); } } attributes = correctAttributes.ToArray(); int counter = 0; foreach (ResultRecord result in pipeIn) { // Obey -First [int32] parameter and break if number is reached if (firstSet && counter == first) { break; } // If all attributes need to be taken if (wildcardSelect) { _results.Add(result); } // If specific attributes are selected else { ResultRecord newResult = new ResultRecord(); foreach (string attr in attributes) { if (result.ContainsKey(attr)) { newResult.Add(attr, result[attr]); } else { newResult.Add(attr, null); } } _results.Add(newResult); } counter++; } return(_results); }
private static CommandResult PerformTraceroute(string ip, string computerName, int count, int timeout, int maxHops) { CommandResult results = new CommandResult(count); Ping ping = new Ping(); List <string> IPs = new List <string>(maxHops); // Last hop details string remoteAddress = string.Empty; bool succeeded = false; int rtt = -1; for (int ttl = 1; ttl <= maxHops; ttl++) { PingOptions options = new PingOptions(ttl, true); PingReply reply = null; try { reply = ping.Send(ip, timeout, alphabet, options); } catch (PingException) { break; } if (reply.Status == IPStatus.TtlExpired) { IPs.Add(reply.Address.ToString()); } else if (reply.Status == IPStatus.TimedOut) { IPs.Add("*"); } else if (reply.Status == IPStatus.Success) { IPs.Add(reply.Address.ToString()); remoteAddress = reply.Address.ToString(); succeeded = true; rtt = (int)reply.RoundtripTime; break; } } ResultRecord record = new ResultRecord() { { "ComputerName", computerName }, { "RemoteAddress", remoteAddress }, { "PingSucceeded", succeeded ? "True" : "False" } }; if (succeeded) { record.Add("PingReplyDetails (RTT)", rtt.ToString()); record.Add("TraceRoute", string.Join(", ", IPs.ToArray())); } results.Add(record); return(results); }