protected override List <ActivityImplementationContext> GetImplementation(NativeActivityContext context)
        {
            string[]        computernames  = PSComputerName.Get(context);
            string[]        connectionUris = PSConnectionUri.Get(context);
            PSSessionOption sessionOptions = PSSessionOption.Get(context);
            List <ActivityImplementationContext> commands = new List <ActivityImplementationContext>();

            // Configure the remote connectivity options
            RemotingBehavior remotingBehavior = PSRemotingBehavior.Get(context);

            if (PSRemotingBehavior.Expression == null)
            {
                remotingBehavior = RemotingBehavior.PowerShell;
            }

            // If they've specified the 'Custom' remoting behavior, ensure the activity
            // supports it.
            if ((remotingBehavior == RemotingBehavior.Custom) && (!SupportsCustomRemoting))
            {
                throw new ArgumentException(Resources.CustomRemotingNotSupported);
            }

            if (PSCredential.Get(context) != null && PSAuthentication.Get(context) == AuthenticationMechanism.NegotiateWithImplicitCredential)
            {
                throw new ArgumentException(Resources.CredentialParameterCannotBeSpecifiedWithNegotiateWithImplicitAuthentication);
            }

            // we need connection info to be populated even for the custom remoting case.
            // This is because the ComputerName is picked up from the connection info field
            if ((remotingBehavior == RemotingBehavior.PowerShell || (IsActivityInlineScript(this) && RunWithCustomRemoting(context))) &&
                (GetIsComputerNameSpecified(context) || (connectionUris != null && connectionUris.Length > 0)))
            {
                List <WSManConnectionInfo> connectionInfo = ActivityUtils.GetConnectionInfo(
                    computernames,
                    connectionUris, PSCertificateThumbprint.Get(context), PSConfigurationName.Get(context),
                    PSUseSsl.Get(context), PSPort.Get(context), PSApplicationName.Get(context),
                    PSCredential.Get(context), PSAuthentication.Get(context).GetValueOrDefault(AuthenticationMechanism.Default),
                    PSAllowRedirection.Get(context).GetValueOrDefault(false),
                    sessionOptions);

                foreach (WSManConnectionInfo connection in connectionInfo)
                {
                    CreatePowerShellInstance(context, connection, commands);
                }
            }
            // Configure the local invocation options
            else
            {
                CreatePowerShellInstance(context, null, commands);
            }

            return(commands);
        }
Пример #2
0
        protected override List <ActivityImplementationContext> GetImplementation(NativeActivityContext context)
        {
            bool needRunspace = !typeof(GenericCimCmdletActivity).IsAssignableFrom(this.GetType());

            string[]     computernames = PSComputerName.Get(context);
            CimSession[] sessions      = this.CimSession.Get(context);
            Uri          resourceUri   = null;

            if (ResourceUri != null)
            {
                resourceUri = ResourceUri.Get(context);
            }

            List <ActivityImplementationContext> commands = new List <ActivityImplementationContext>();

            // Configure the remote connectivity options...
            if (computernames != null && computernames.Length > 0)
            {
                WSManSessionOptions sessionOptions = new WSManSessionOptions();

                // Set a timeout on the connection...
                uint?timeout = PSActionRunningTimeoutSec.Get(context);
                if (timeout.HasValue)
                {
                    sessionOptions.Timeout = TimeSpan.FromSeconds((double)(timeout.Value));
                }

                // See if we should use SSL or not...
                bool?useSsl = PSUseSsl.Get(context);
                bool sessionOptionUseSsl = false;

                if (useSsl.HasValue)
                {
                    sessionOptions.UseSsl = useSsl.Value;
                    sessionOptionUseSsl   = useSsl.Value;
                }

                // Set the port to use
                uint?port = PSPort.Get(context);
                uint sessionOptionPort = 0;

                if (port.HasValue)
                {
                    sessionOptions.DestinationPort = port.Value;
                    sessionOptionPort = port.Value;
                }

                // Map over options from PSSessionConfig to WSManSessionOptions
                PSSessionOption pso = PSSessionOption.Get(context);
                if (pso != null)
                {
                    sessionOptions.NoEncryption        = pso.NoEncryption;
                    sessionOptions.CertCACheck         = pso.SkipCACheck;
                    sessionOptions.CertCNCheck         = pso.SkipCNCheck;
                    sessionOptions.CertRevocationCheck = pso.SkipRevocationCheck;

                    if (pso.UseUTF16)
                    {
                        sessionOptions.PacketEncoding = PacketEncoding.Utf16;
                    }

                    if (pso.Culture != null)
                    {
                        sessionOptions.Culture = pso.Culture;
                    }

                    if (pso.UICulture != null)
                    {
                        sessionOptions.UICulture = pso.UICulture;
                    }

                    if (pso.ProxyCredential != null)
                    {
                        string[] parts = pso.ProxyCredential.UserName.Split('\\');
                        string   domain, userid;
                        if (parts.Length < 2)
                        {
                            domain = string.Empty;
                            userid = parts[0];
                        }
                        else
                        {
                            domain = parts[0];
                            userid = parts[1];
                        }

                        sessionOptions.AddProxyCredentials(
                            new CimCredential(ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(pso.ProxyAuthentication),
                                              domain, userid, pso.ProxyCredential.Password));
                    }

                    switch (pso.ProxyAccessType)
                    {
                    case ProxyAccessType.WinHttpConfig:
                        sessionOptions.ProxyType = ProxyType.WinHttp;
                        break;

                    case ProxyAccessType.AutoDetect:
                        sessionOptions.ProxyType = ProxyType.Auto;
                        break;

                    case ProxyAccessType.IEConfig:
                        sessionOptions.ProxyType = ProxyType.InternetExplorer;
                        break;
                    }
                }

                PSCredential pscreds = PSCredential.Get(context);
                string       certificateThumbprint = PSCertificateThumbprint.Get(context);

                if (pscreds != null && certificateThumbprint != null)
                {
                    throw new ArgumentException(Resources.CredentialParameterCannotBeSpecifiedWithPSCertificateThumbPrint);
                }

                PasswordAuthenticationMechanism passwordAuthenticationMechanism = PasswordAuthenticationMechanism.Default;
                AuthenticationMechanism?        authenticationMechanism         = PSAuthentication.Get(context);

                if (authenticationMechanism.HasValue)
                {
                    passwordAuthenticationMechanism = ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(authenticationMechanism.Value);
                }


                if (certificateThumbprint != null)
                {
                    sessionOptions.AddDestinationCredentials(new CimCredential(CertificateAuthenticationMechanism.Default, certificateThumbprint));
                }

                if (pscreds != null)
                {
                    string[] parts = pscreds.UserName.Split('\\');
                    string   domain, userid;
                    if (parts.Length < 2)
                    {
                        domain = string.Empty;
                        userid = parts[0];
                    }
                    else
                    {
                        domain = parts[0];
                        userid = parts[1];
                    }

                    sessionOptions.AddDestinationCredentials(new CimCredential(passwordAuthenticationMechanism, domain, userid, pscreds.Password));
                }

                // Create the PowerShell instance, and add the script to it.
                if (sessions != null && sessions.Length > 0)
                {
                    foreach (CimSession session in sessions)
                    {
                        ActivityImplementationContext configuredCommand = GetPowerShell(context);

                        CimActivityImplementationContext activityImplementationContext =
                            new CimActivityImplementationContext(
                                configuredCommand,
                                session.ComputerName,
                                pscreds,
                                certificateThumbprint,
                                authenticationMechanism,
                                sessionOptionUseSsl,
                                sessionOptionPort,
                                pso,
                                session,
                                sessionOptions,
                                ModuleDefinition,
                                resourceUri);

                        commands.Add(activityImplementationContext);
                        //if (needRunspace)
                        //    GetRunspaceForCimCmdlet(context, activityImplementationContext);
                    }
                }
                else if (this.PSCommandName.Equals("CimCmdlets\\New-CimSession", StringComparison.OrdinalIgnoreCase))
                {
                    // NewCimSession activity is a special one as it creates the required sessions based on number of computers specified in one go.

                    ActivityImplementationContext baseContext = GetPowerShell(context);

                    CimActivityImplementationContext activityImplementationContext =
                        new CimActivityImplementationContext(baseContext,
                                                             null,     // ComputerName
                                                             pscreds,
                                                             certificateThumbprint,
                                                             authenticationMechanism,
                                                             sessionOptionUseSsl,
                                                             sessionOptionPort,
                                                             pso,
                                                             null,    // session
                                                             sessionOptions,
                                                             ModuleDefinition,
                                                             resourceUri);

                    commands.Add(activityImplementationContext);
                }
                else
                {
                    foreach (string computer in computernames)
                    {
                        ActivityImplementationContext baseContext = GetPowerShell(context);

                        CimActivityImplementationContext activityImplementationContext =
                            new CimActivityImplementationContext(baseContext,
                                                                 computer,
                                                                 pscreds,
                                                                 certificateThumbprint,
                                                                 authenticationMechanism,
                                                                 sessionOptionUseSsl,
                                                                 sessionOptionPort,
                                                                 pso,
                                                                 null, // session
                                                                 sessionOptions,
                                                                 ModuleDefinition,
                                                                 resourceUri);

                        commands.Add(activityImplementationContext);
                    }
                }
            }
            // Configure the local invocation options
            else
            {
                // Create the PowerShell instance, and add the script to it.
                ActivityImplementationContext    baseContext = GetPowerShell(context);
                CimActivityImplementationContext activityImplementationContext =
                    new CimActivityImplementationContext(baseContext,
                                                         null,  // ComputerName
                                                         null,  // Credential
                                                         null,  // CertificateThumbprint
                                                         AuthenticationMechanism.Default,
                                                         false, // UseSsl
                                                         0,     // Port
                                                         null,  // PSSessionOption
                                                         null,  // Session
                                                         null,  // CimSessionOptions
                                                         ModuleDefinition,
                                                         resourceUri);

                commands.Add(activityImplementationContext);
            }

            return(commands);
        }