コード例 #1
0
        public static void Run()
        {
            var userName = "******";
            var password = "******";

            try
            {
                // Create a connector
                var connector = new ActiveDirectoryConnector(userName, password);

                // Initialze Powershell Client
                using (var client = PowerShellClient.Create(connector))
                {
                    // Create Command
                    var geUserCommand = new PowershellCommand
                    {
                        Name              = "GetUser",
                        CommandText       = "Get-User",
                        CommandParameters = new List <PowershellCommandParameter> {
                            new PowershellCommandParameter {
                                Name = "ResultSize", Value = "unlimited"
                            },                                                                                   //"100"
                            new PowershellCommandParameter {
                                Name = "OrganizationalUnit", Value = "Marketing"
                            }
                        }
                    };

                    // Execute Command
                    var result = client.Execute <ActiveDirectory>(new List <PowershellCommand> {
                        geUserCommand
                    });

                    // Success, get the data
                    if (result.Status == StatusCode.SUCCESS)
                    {
                        var adUsers = result.Content;

                        Console.WriteLine($"{adUsers.Count} users found");
                    }
                    else // Failes show the errors
                    {
                        Console.WriteLine(result.Status);
                        Console.WriteLine(result.StatusText);
                        Console.WriteLine(result.Errors);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #2
0
        //internal static PSObject ExecuteCommadX(this Connector connector, Command command)
        //{
        //    PSObject result = null;
        //    using (Runspace psRunSpace = RunspaceFactory.CreateRunspace(connector.SessionState))
        //    {
        //        // Open runspace.
        //        psRunSpace.Open();

        //        var pipe = psRunSpace.CreatePipeline();
        //        pipe.Commands.Add(command);

        //        // Execute command and generate results and errors (if any).
        //        var results = pipe.Invoke();

        //        result = results[0];

        //        psRunSpace.Close();
        //    }

        //    return result;
        //}

        internal static Command ToPSCommand(this PowershellCommand command)
        {
            var psCommand = new Command(command.CommandText);

            if (command.CommandParameters != null)
            {
                foreach (var parm in command.CommandParameters)
                {
                    psCommand.Parameters.Add(new CommandParameter(parm.Name, parm.Value));
                }
            }

            return(psCommand);
        }