Beispiel #1
0
        /// <summary>
        /// The execute.
        /// </summary>
        /// <exception cref="UnauthorizedAccessException">
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public void Execute()
        {
            ShowHeader();

            string[] args = Environment.GetCommandLineArgs().Skip(1).ToArray();
            if (!args.Any())
            {
                ShowHelp();
                return;
            }

            var commandLine = new CommandLine(args);
            if (commandLine.GetWantsHelp())
            {
                ShowHelp();
                return;
            }

            // todo: add support for job monitoring

            // Check config file
            ApplicationSettings settings = LoadSettings();

            // Connect to server
            IUpdateServer server = Server.Connect(settings.Server);

            // Check user has admin access
            UpdateServerUserRole role = server.GetCurrentUserRole();
            if (role != UpdateServerUserRole.Administrator)
            {
                throw new UnauthorizedAccessException("You don't have administrator access on the WSUS server.");
            }

            // Get the default rule
            bool isTest = commandLine.GetIsTest();
            RunSetCollection runSets = settings.RunSets;

            switch (commandLine.GetUseRunSet())
            {
                case TriState.Yes:
                    if (runSets == null || runSets.Count == 0)
                    {
                        throw new InvalidOperationException(
                            "/runset specified on the command line.  But there are no runsets defined in the config file.");
                    }

                    // we have run sets
                    // this means our approval logic is more complex
                    DoRunSets(server, runSets, commandLine, isTest);

                    break;

                case TriState.No:
                    DoNoRunSets(settings, server, isTest);

                    break;

                default:
                    if (runSets != null && runSets.Count > 0)
                    {
                        throw new InvalidOperationException(
                            "RunSets exist in the application config. /norunset or /runset must be specified on the command line.");
                    }

                    break;
            }
        }