Exemplo n.º 1
0
 private void RunPSCommandForOrgOrUser(PSCommand cmd)
 {
     using (AnchorRunspaceProxy anchorRunspaceProxy = AnchorRunspaceProxy.CreateRunspaceForDatacenterAdmin(this.Context, "upgradehandlers"))
     {
         try
         {
             anchorRunspaceProxy.RunPSCommand <PSObject>(cmd);
         }
         catch (MigrationPermanentException ex)
         {
             this.Context.Logger.Log(MigrationEventType.Error, "Run cmd {0} failed due to {1}", new object[]
             {
                 cmd.ToString(),
                 ex
             });
             throw;
         }
     }
 }
Exemplo n.º 2
0
        static int Main(string[] args)
        {
            // Using reflection determine available commands
            Dictionary <Type, CaseInsensitiveList> availableCommands = ReflectionHelper.GetCommands();
            List <PSCommand> userCommands = null;

            // If no arguments are provided to the executable, show help
            if (args.Length == 0)
            {
                Console.WriteLine("== NoPowerShell v{0} ==\r\nWebsite: {1}\r\n{2}", VERSION, WEBSITE, USAGE);
                userCommands = new List <PSCommand>(1)
                {
                    new GetCommandCommand(null)
                };
            }
            // Parse pipes in commandline arguments and commands within pipes
            else
            {
                string error = null;

                try
                {
                    userCommands = PipeParser.ParseArguments(args, availableCommands);
                }
                catch (ParameterBindingException ex)
                {
                    error = ex.Message;
                }
                catch (CommandNotFoundException ex)
                {
                    error = string.Join("", new string[] { ex.Message, HELP });
                }

                if (error != null)
                {
                    WriteError(error);
                    return(-1);
                }
            }

            // Add output to console if no explicit output is provided
            Type lastCommand = userCommands[userCommands.Count - 1].GetType();
            bool justOutput  = false;

            if (lastCommand != typeof(FormatListCommand) && lastCommand != typeof(FormatTableCommand))
            {
                justOutput = true;
            }

            CommandResult result = null;

#if DEBUG
            // Execute commands in pipeline
            foreach (PSCommand command in userCommands)
            {
                result = command.Execute(result);
            }
#else
            PSCommand mostRecentCommand = null;
            try
            {
                // Execute commands in pipeline
                foreach (PSCommand command in userCommands)
                {
                    mostRecentCommand = command;
                    result            = command.Execute(result);
                }
            }
            catch (NoPowerShellException e)
            {
                WriteError(string.Format("{0} : {1}", mostRecentCommand.ToString(), e.Message));
                return(-1);
            }
            catch (Exception e)
            {
                WriteError(string.Format("{0} : {1}", mostRecentCommand.ToString(), e.ToString()));
                return(-1);
            }
#endif

            // Output to screen
            if (justOutput)
            {
                ResultPrinter.OutputResults(result);
            }

            return(0);
        }
Exemplo n.º 3
0
        internal override bool OnApprove(MessageItem message)
        {
            AutoGroupApplication.diag.TraceFunction((long)this.GetHashCode(), "AutoGroupApplication.OnApprove");
            message.Load(new PropertyDefinition[]
            {
                MessageItemSchema.ReceivedBy,
                MessageItemSchema.ApprovalDecisionMaker,
                MessageItemSchema.ApprovalRequestor,
                MessageItemSchema.ApprovalAllowedDecisionMakers,
                MessageItemSchema.ApprovalApplicationData
            });
            CultureInfo cultureInfo    = null;
            CultureInfo messageCulture = null;
            string      arg;
            SmtpAddress smtpAddress;
            SmtpAddress smtpAddress2;

            SmtpAddress[] array;
            if (!AutoGroupApplication.GetItemRecipients(message, out arg, out smtpAddress, out smtpAddress2, out array))
            {
                AutoGroupApplication.diag.TraceError <string, SmtpAddress, SmtpAddress>((long)this.GetHashCode(), "'{0}' message has invalid approver '{1}', requester '{2}', or empty moderators list.", arg, smtpAddress, smtpAddress2);
                return(false);
            }
            SmtpAddress emailAddressFromMailboxItem = AutoGroupApplication.GetEmailAddressFromMailboxItem(message);

            if (emailAddressFromMailboxItem == SmtpAddress.Empty)
            {
                AutoGroupApplication.diag.TraceError <string>((long)this.GetHashCode(), "'{0}' cannot get arbitration mailbox SMTP address from message.", arg);
                return(false);
            }
            string     valueOrDefault = message.GetValueOrDefault <string>(MessageItemSchema.ApprovalApplicationData);
            string     text;
            ADObjectId adobjectId;

            if (string.IsNullOrEmpty(valueOrDefault) || !AutoGroupApplication.ParseApprovalData(valueOrDefault, out text, out adobjectId))
            {
                AutoGroupApplication.diag.TraceError <string, string>((long)this.GetHashCode(), "'{0}' applicationData '{1}' cannot be parsed", arg, valueOrDefault);
                return(false);
            }
            IRecipientSession recipientSession = ApprovalProcessor.CreateRecipientSessionFromSmtpAddress(smtpAddress);
            ADRawEntry        adrawEntry       = null;
            bool flag = true;

            try
            {
                adrawEntry = recipientSession.FindByProxyAddress(new SmtpProxyAddress(smtpAddress.ToString(), true), new PropertyDefinition[]
                {
                    ADObjectSchema.Id,
                    ADUserSchema.Languages
                });
            }
            catch (NonUniqueRecipientException)
            {
                flag = false;
            }
            ADRawEntry adrawEntry2 = null;
            bool       flag2       = true;

            try
            {
                adrawEntry2 = recipientSession.FindByProxyAddress(new SmtpProxyAddress(smtpAddress2.ToString(), true), new PropertyDefinition[]
                {
                    ADObjectSchema.Id,
                    ADRecipientSchema.PrimarySmtpAddress,
                    ADUserSchema.Languages
                });
            }
            catch (NonUniqueRecipientException)
            {
                flag2 = false;
            }
            MiniRecipient miniRecipient = recipientSession.ReadMiniRecipient(adobjectId, null);

            if (miniRecipient == null)
            {
                AutoGroupApplication.diag.TraceError <string, ADObjectId>((long)this.GetHashCode(), "'{0}' group '{1}' not found in AD.", arg, adobjectId);
                return(false);
            }
            string group;

            if (string.IsNullOrEmpty(miniRecipient.DisplayName))
            {
                group = miniRecipient.Name;
            }
            else
            {
                group = miniRecipient.DisplayName;
            }
            if (adrawEntry == null)
            {
                if (flag)
                {
                    AutoGroupApplication.diag.TraceError <string, SmtpAddress>((long)this.GetHashCode(), "'{0}' approver '{1}' not found in AD.", arg, smtpAddress);
                }
                else
                {
                    AutoGroupApplication.diag.TraceError <string, SmtpAddress>((long)this.GetHashCode(), "'{0}' approver '{1}' not unique in AD.", arg, smtpAddress);
                }
                ApprovalProcessor.SendNotification(emailAddressFromMailboxItem, message.Id.ObjectId, new SmtpAddress[]
                {
                    smtpAddress
                }, Strings.AutoGroupRequestFailedSubject(group), Strings.AutoGroupRequestFailedHeader(group), Strings.AutoGroupRequestFailedBodyBadApprover(group, ApprovalProcessor.GetDisplayNameFromSmtpAddress(smtpAddress2.ToString()), ApprovalProcessor.GetDisplayNameFromSmtpAddress(smtpAddress.ToString())), LocalizedString.Empty, null);
                return(false);
            }
            MultiValuedProperty <CultureInfo> multiValuedProperty = (MultiValuedProperty <CultureInfo>)adrawEntry[ADUserSchema.Languages];

            if (multiValuedProperty.Count > 0)
            {
                cultureInfo = multiValuedProperty[0];
            }
            if (adrawEntry2 == null)
            {
                if (flag2)
                {
                    AutoGroupApplication.diag.TraceError <string, SmtpAddress>((long)this.GetHashCode(), "'{0}' requester '{1}' not found in AD.", arg, smtpAddress2);
                }
                else
                {
                    AutoGroupApplication.diag.TraceError <string, SmtpAddress>((long)this.GetHashCode(), "'{0}' requester '{1}' not unique in AD.", arg, smtpAddress2);
                }
                ApprovalProcessor.SendNotification(emailAddressFromMailboxItem, message.Id.ObjectId, new SmtpAddress[]
                {
                    smtpAddress
                }, Strings.AutoGroupRequestFailedSubject(group), Strings.AutoGroupRequestFailedHeader(group), Strings.AutoGroupRequestFailedBodyBadRequester(group, ApprovalProcessor.GetDisplayNameFromSmtpAddress(smtpAddress2.ToString())), LocalizedString.Empty, cultureInfo);
                return(false);
            }
            MultiValuedProperty <CultureInfo> multiValuedProperty2 = (MultiValuedProperty <CultureInfo>)adrawEntry2[ADUserSchema.Languages];

            if (multiValuedProperty2.Count > 0)
            {
                messageCulture = multiValuedProperty2[0];
            }
            bool   flag3 = false;
            string a;

            if ((a = text) != null)
            {
                if (!(a == "Add-DistributionGroupMember"))
                {
                    if (!(a == "Remove-DistributionGroupMember"))
                    {
                        goto IL_393;
                    }
                    flag3 = true;
                }
                PSCommand pscommand = new PSCommand().AddCommand(text);
                pscommand.AddParameter("Identity", adobjectId);
                pscommand.AddParameter("Member", adrawEntry2.Id);
                if (flag3)
                {
                    pscommand.AddParameter("Confirm", new SwitchParameter(false));
                }
                AutoGroupApplication.diag.Information <string, string>((long)this.GetHashCode(), "'{0}' executing command '{1}'.", arg, pscommand.ToString());
                string text2;
                string text3;
                ApprovalApplication.ExecuteCommandsInRunspace(smtpAddress, pscommand, cultureInfo, out text2, out text3);
                if (!text2.Equals(string.Empty))
                {
                    AutoGroupApplication.diag.TraceError <string, string, string>((long)this.GetHashCode(), "'{0}' command '{1}' failed with error {2}.", arg, pscommand.ToString(), text2);
                    ApprovalProcessor.SendNotification(emailAddressFromMailboxItem, message.Id.ObjectId, new SmtpAddress[]
                    {
                        smtpAddress
                    }, Strings.AutoGroupRequestFailedSubject(group), Strings.AutoGroupRequestFailedHeader(group), Strings.AutoGroupRequestFailedBodyTaskError(text2), LocalizedString.Empty, cultureInfo);
                    return(false);
                }
                AutoGroupApplication.diag.Information <string, string>((long)this.GetHashCode(), "'{0}' command '{1}' completed successfully.", arg, pscommand.ToString());
                ApprovalProcessor.SendApproveNotification(emailAddressFromMailboxItem, message.Id.ObjectId, new SmtpAddress[]
                {
                    (SmtpAddress)adrawEntry2[ADRecipientSchema.PrimarySmtpAddress]
                }, Strings.AutoGroupRequestApprovedSubject(group), Strings.AutoGroupRequestApprovedHeader(ApprovalProcessor.GetDisplayNameFromSmtpAddress(smtpAddress.ToString()), group), Strings.AutoGroupRequestApprovedBody, LocalizedString.Empty, messageCulture);
                return(true);
            }
IL_393:
            AutoGroupApplication.diag.TraceError <string, string>((long)this.GetHashCode(), "'{0}' command '{1}' not recognized.", arg, text);
            return(false);
        }