Beispiel #1
0
        protected override List <ActivityImplementationContext> GetImplementation(NativeActivityContext context)
        {
            string empty;
            string str;
            string empty1;
            string str1;

            typeof(GenericCimCmdletActivity).IsAssignableFrom(base.GetType());
            string[]     strArrays       = this.PSComputerName.Get(context);
            CimSession[] cimSessionArray = this.CimSession.Get(context);
            Uri          uri             = null;

            if (this.ResourceUri != null)
            {
                uri = this.ResourceUri.Get(context);
            }
            List <ActivityImplementationContext> activityImplementationContexts = new List <ActivityImplementationContext>();

            if (strArrays == null || (int)strArrays.Length <= 0)
            {
                ActivityImplementationContext    powerShell = this.GetPowerShell(context);
                CimActivityImplementationContext cimActivityImplementationContext = new CimActivityImplementationContext(powerShell, null, null, null, new AuthenticationMechanism?(AuthenticationMechanism.Default), false, 0, null, null, null, this.ModuleDefinition, uri);
                activityImplementationContexts.Add(cimActivityImplementationContext);
            }
            else
            {
                WSManSessionOptions wSManSessionOption = new WSManSessionOptions();
                uint?nullable = base.PSActionRunningTimeoutSec.Get(context);
                if (nullable.HasValue)
                {
                    wSManSessionOption.Timeout = TimeSpan.FromSeconds((double)((float)nullable.Value));
                }
                bool?nullable1 = this.PSUseSsl.Get(context);
                bool value     = false;
                if (nullable1.HasValue)
                {
                    wSManSessionOption.UseSsl = nullable1.Value;
                    value = nullable1.Value;
                }
                uint?nullable2 = this.PSPort.Get(context);
                uint num       = 0;
                if (nullable2.HasValue)
                {
                    wSManSessionOption.DestinationPort = nullable2.Value;
                    num = nullable2.Value;
                }
                PSSessionOption pSSessionOption = this.PSSessionOption.Get(context);
                if (pSSessionOption != null)
                {
                    wSManSessionOption.NoEncryption        = pSSessionOption.NoEncryption;
                    wSManSessionOption.CertCACheck         = pSSessionOption.SkipCACheck;
                    wSManSessionOption.CertCNCheck         = pSSessionOption.SkipCNCheck;
                    wSManSessionOption.CertRevocationCheck = pSSessionOption.SkipRevocationCheck;
                    if (pSSessionOption.UseUTF16)
                    {
                        wSManSessionOption.PacketEncoding = PacketEncoding.Utf16;
                    }
                    if (pSSessionOption.Culture != null)
                    {
                        wSManSessionOption.Culture = pSSessionOption.Culture;
                    }
                    if (pSSessionOption.UICulture != null)
                    {
                        wSManSessionOption.UICulture = pSSessionOption.UICulture;
                    }
                    if (pSSessionOption.ProxyCredential != null)
                    {
                        char[] chrArray = new char[1];
                        chrArray[0] = '\\';
                        string[] strArrays1 = pSSessionOption.ProxyCredential.UserName.Split(chrArray);
                        if ((int)strArrays1.Length >= 2)
                        {
                            empty = strArrays1[0];
                            str   = strArrays1[1];
                        }
                        else
                        {
                            empty = string.Empty;
                            str   = strArrays1[0];
                        }
                        wSManSessionOption.AddProxyCredentials(new CimCredential(PSGeneratedCIMActivity.ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(pSSessionOption.ProxyAuthentication), empty, str, pSSessionOption.ProxyCredential.Password));
                    }
                    ProxyAccessType proxyAccessType = pSSessionOption.ProxyAccessType;
                    if (proxyAccessType == ProxyAccessType.IEConfig)
                    {
                        wSManSessionOption.ProxyType = ProxyType.InternetExplorer;
                        goto Label0;
                    }
                    else if (proxyAccessType == ProxyAccessType.WinHttpConfig)
                    {
                        wSManSessionOption.ProxyType = ProxyType.WinHttp;
                        goto Label0;
                    }
                    else if (proxyAccessType == (ProxyAccessType.IEConfig | ProxyAccessType.WinHttpConfig))
                    {
                        goto Label0;
                    }
                    else if (proxyAccessType == ProxyAccessType.AutoDetect)
                    {
                        wSManSessionOption.ProxyType = ProxyType.Auto;
                        goto Label0;
                    }
                }
Label0:
                PSCredential pSCredential = this.PSCredential.Get(context);
                string str2 = this.PSCertificateThumbprint.Get(context);
                if (pSCredential == null || str2 == null)
                {
                    PasswordAuthenticationMechanism cimPasswordAuthenticationMechanism = PasswordAuthenticationMechanism.Default;
                    AuthenticationMechanism?        nullable3 = this.PSAuthentication.Get(context);
                    if (nullable3.HasValue)
                    {
                        cimPasswordAuthenticationMechanism = PSGeneratedCIMActivity.ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(nullable3.Value);
                    }
                    if (str2 != null)
                    {
                        wSManSessionOption.AddDestinationCredentials(new CimCredential(CertificateAuthenticationMechanism.Default, str2));
                    }
                    if (pSCredential != null)
                    {
                        char[] chrArray1 = new char[1];
                        chrArray1[0] = '\\';
                        string[] strArrays2 = pSCredential.UserName.Split(chrArray1);
                        if ((int)strArrays2.Length >= 2)
                        {
                            empty1 = strArrays2[0];
                            str1   = strArrays2[1];
                        }
                        else
                        {
                            empty1 = string.Empty;
                            str1   = strArrays2[0];
                        }
                        wSManSessionOption.AddDestinationCredentials(new CimCredential(cimPasswordAuthenticationMechanism, empty1, str1, pSCredential.Password));
                    }
                    if (cimSessionArray == null || (int)cimSessionArray.Length <= 0)
                    {
                        string[] strArrays3 = strArrays;
                        for (int i = 0; i < (int)strArrays3.Length; i++)
                        {
                            string str3 = strArrays3[i];
                            ActivityImplementationContext    activityImplementationContext     = this.GetPowerShell(context);
                            CimActivityImplementationContext cimActivityImplementationContext1 = new CimActivityImplementationContext(activityImplementationContext, str3, pSCredential, str2, nullable3, value, num, pSSessionOption, null, wSManSessionOption, this.ModuleDefinition, uri);
                            activityImplementationContexts.Add(cimActivityImplementationContext1);
                        }
                    }
                    else
                    {
                        CimSession[] cimSessionArray1 = cimSessionArray;
                        for (int j = 0; j < (int)cimSessionArray1.Length; j++)
                        {
                            CimSession cimSession = cimSessionArray1[j];
                            ActivityImplementationContext    powerShell1 = this.GetPowerShell(context);
                            CimActivityImplementationContext cimActivityImplementationContext2 = new CimActivityImplementationContext(powerShell1, cimSession.ComputerName, pSCredential, str2, nullable3, value, num, pSSessionOption, cimSession, wSManSessionOption, this.ModuleDefinition, uri);
                            activityImplementationContexts.Add(cimActivityImplementationContext2);
                        }
                    }
                }
                else
                {
                    throw new ArgumentException(Resources.CredentialParameterCannotBeSpecifiedWithPSCertificateThumbPrint);
                }
            }
            return(activityImplementationContexts);
        }
Beispiel #2
0
        internal WSManSessionOptions CreateWSMANSessionOptions()
        {
            WSManSessionOptions wSManSessionOption = new WSManSessionOptions();

            if (!this.noEncryptionSet)
            {
                wSManSessionOption.NoEncryption = false;
            }
            else
            {
                wSManSessionOption.NoEncryption = true;
                this.noEncryptionSet            = false;
            }
            if (!this.skipCACheckSet)
            {
                wSManSessionOption.CertCACheck = true;
            }
            else
            {
                wSManSessionOption.CertCACheck = false;
                this.skipCACheckSet            = false;
            }
            if (!this.skipCNCheckSet)
            {
                wSManSessionOption.CertCNCheck = true;
            }
            else
            {
                wSManSessionOption.CertCNCheck = false;
                this.skipCNCheckSet            = false;
            }
            if (!this.skipRevocationCheckSet)
            {
                wSManSessionOption.CertRevocationCheck = true;
            }
            else
            {
                wSManSessionOption.CertRevocationCheck = false;
                this.skipRevocationCheckSet            = false;
            }
            if (!this.encodeportinserviceprincipalnameSet)
            {
                wSManSessionOption.EncodePortInServicePrincipalName = false;
            }
            else
            {
                wSManSessionOption.EncodePortInServicePrincipalName = this.EncodePortInServicePrincipalName;
                this.encodeportinserviceprincipalnameSet            = false;
            }
            if (!this.encodingSet)
            {
                wSManSessionOption.PacketEncoding = PacketEncoding.Utf8;
            }
            else
            {
                wSManSessionOption.PacketEncoding = this.Encoding;
            }
            if (this.HttpPrefix != null)
            {
                wSManSessionOption.HttpUrlPrefix = this.HttpPrefix;
            }
            if (!this.maxenvelopesizekbSet)
            {
                wSManSessionOption.MaxEnvelopeSize = 0;
            }
            else
            {
                wSManSessionOption.MaxEnvelopeSize = this.MaxEnvelopeSizeKB;
            }
            if (!string.IsNullOrWhiteSpace(this.ProxyCertificateThumbprint))
            {
                CimCredential cimCredential = new CimCredential(CertificateAuthenticationMechanism.Default, this.ProxyCertificateThumbprint);
                wSManSessionOption.AddProxyCredentials(cimCredential);
            }
            if (this.proxyauthenticationSet)
            {
                this.proxyauthenticationSet = false;
                DebugHelper.WriteLogEx("create credential", 1);
                CimCredential cimCredential1 = base.CreateCimCredentials(this.ProxyCredential, this.ProxyAuthentication, "New-CimSessionOption", "ProxyAuthentication");
                if (cimCredential1 != null)
                {
                    try
                    {
                        DebugHelper.WriteLogEx("Add proxy credential", 1);
                        wSManSessionOption.AddProxyCredentials(cimCredential1);
                    }
                    catch (Exception exception1)
                    {
                        Exception exception = exception1;
                        DebugHelper.WriteLogEx(exception.ToString(), 1);
                        throw exception;
                    }
                }
            }
            if (!this.proxytypeSet)
            {
                wSManSessionOption.ProxyType = ProxyType.WinHttp;
            }
            else
            {
                wSManSessionOption.ProxyType = this.ProxyType;
                this.proxytypeSet            = false;
            }
            if (!this.usesslSet)
            {
                wSManSessionOption.UseSsl = false;
            }
            else
            {
                wSManSessionOption.UseSsl = this.UseSsl;
                this.usesslSet            = false;
            }
            wSManSessionOption.DestinationPort = 0;
            return(wSManSessionOption);
        }
Beispiel #3
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);
        }