}//End EndProcessing()
        #endregion

        #region helper methods

        /// <summary>
        /// Build a CimSessionOptions, used to create CimSession
        /// </summary>
        /// <returns>Null means no prefer CimSessionOptions</returns>
        internal void BuildSessionOptions(out CimSessionOptions outputOptions, out CimCredential outputCredential)
        {
            DebugHelper.WriteLogEx();

            CimSessionOptions options = null;
            if (this.SessionOption != null)
            {
                // clone the sessionOption object
                if (this.SessionOption is WSManSessionOptions)
                {
                    options = new WSManSessionOptions(this.sessionOption as WSManSessionOptions);
                }
                else
                {
                    options = new DComSessionOptions(this.sessionOption as DComSessionOptions);
                }
            }
            outputOptions = null;
            outputCredential = null;
            if (options != null)
            {
                DComSessionOptions dcomOptions = (options as DComSessionOptions);
                if (dcomOptions != null)
                {
                    bool conflict = false;
                    string parameterName = string.Empty;
                    if (this.CertificateThumbprint != null)
                    {
                        conflict = true;
                        parameterName = @"CertificateThumbprint";
                    }
                    if (portSet)
                    {
                        conflict = true;
                        parameterName = @"Port";
                    }
                    if (conflict)
                    {
                        ThrowConflictParameterWasSet(@"New-CimSession", parameterName, @"DComSessionOptions");
                        return;
                    }
                }
            }
            if (portSet || (this.CertificateThumbprint != null))
            {
                WSManSessionOptions wsmanOptions = (options == null) ? new WSManSessionOptions() : options as WSManSessionOptions;
                if (portSet)
                {
                    wsmanOptions.DestinationPort = this.Port;
                    portSet = false;
                }
                if (this.CertificateThumbprint != null)
                {
                    CimCredential credentials = new CimCredential(CertificateAuthenticationMechanism.Default, this.CertificateThumbprint);
                    wsmanOptions.AddDestinationCredentials(credentials);
                }
                options = wsmanOptions;
            }
            if (this.operationTimeoutSet)
            {
                if (options != null)
                {
                    options.Timeout = TimeSpan.FromSeconds((double)this.OperationTimeoutSec);
                }
            }
            if (this.authenticationSet || (this.credential != null))
            {
                PasswordAuthenticationMechanism authentication = this.authenticationSet ? this.Authentication : PasswordAuthenticationMechanism.Default;
                if (this.authenticationSet)
                {
                    this.authenticationSet = false;
                }
                CimCredential credentials = CreateCimCredentials(this.Credential, authentication, @"New-CimSession", @"Authentication");
                if (credentials == null)
                {
                    return;
                }

                DebugHelper.WriteLog("Credentials: {0}", 1, credentials);
                outputCredential = credentials;
                if (options != null)
                {
                    DebugHelper.WriteLog("Add credentials to option: {0}", 1, options);
                    options.AddDestinationCredentials(credentials);
                }
            }
            DebugHelper.WriteLogEx("Set outputOptions: {0}", 1, outputOptions);
            outputOptions = options;
        }
Beispiel #2
0
		public DComSessionOptions(DComSessionOptions optionsToClone) : base(optionsToClone)
		{
		}
Beispiel #3
0
 /// <summary>
 /// Instantiates a deep copy of <paramref name="optionsToClone"/>
 /// </summary>
 /// <param name="optionsToClone">options to clone</param>
 /// <exception cref="ArgumentNullException">Thrown when <paramref name="optionsToClone"/> is <c>null</c></exception>
 public DComSessionOptions(DComSessionOptions optionsToClone)
     : base(optionsToClone)
 {
 }
        }//End EndProcessing()

        #endregion

        #region helper functions
        /// <summary>
        /// Create DComSessionOptions
        /// </summary>
        /// <returns></returns>
        internal DComSessionOptions CreateDComSessionOptions()
        {
            DComSessionOptions dcomoptions = new DComSessionOptions();
            if (this.impersonationSet)
            {
                dcomoptions.Impersonation = this.Impersonation;
                this.impersonationSet = false;
            }
            else
            {
                dcomoptions.Impersonation = ImpersonationType.Impersonate;
            }
            if (this.packetintegritySet)
            {
                dcomoptions.PacketIntegrity = this.packetintegrity;
                this.packetintegritySet = false;
            }
            else
            {
                dcomoptions.PacketIntegrity = true;
            }
            if (this.packetprivacySet)
            {
                dcomoptions.PacketPrivacy = this.PacketPrivacy;
                this.packetprivacySet = false;
            }
            else
            {
                dcomoptions.PacketPrivacy = true;
            }
            return dcomoptions;
        }
Beispiel #5
0
        /// <summary>
        /// <para>
        /// Create <see cref="CimSessionOptions"/> based on the given computerName,
        /// timeout and credential
        /// </para>
        /// </summary>
        /// <param name="computerName"></param>
        /// <param name="timeout"></param>
        /// <param name="credential"></param>
        /// <returns></returns>
        internal static CimSessionOptions CreateCimSessionOption(string computerName,
            UInt32 timeout, CimCredential credential)
        {
            DebugHelper.WriteLogEx();

            CimSessionOptions option;
            if (ConstValue.IsDefaultComputerName(computerName))
            {
                DebugHelper.WriteLog("<<<<<<<<<< Use protocol DCOM  {0}", 1, computerName);
                option = new DComSessionOptions();
            }
            else
            {
                DebugHelper.WriteLog("<<<<<<<<<< Use protocol WSMAN {0}", 1, computerName);
                option = new WSManSessionOptions();
            }
            if (timeout != 0)
            {
                option.Timeout = TimeSpan.FromSeconds((double)timeout);
            }
            if (credential != null)
            {
                option.AddDestinationCredentials(credential);
            }
            DebugHelper.WriteLogEx("returned option :{0}.", 1, option);
            return option;
        }
Beispiel #6
0
		internal void BuildSessionOptions (out CimSessionOptions outputOptions, out CimCredential outputCredential)
		{
			WSManSessionOptions wSManSessionOption;
			PasswordAuthenticationMechanism authentication;
			DebugHelper.WriteLogEx ();
			CimSessionOptions dComSessionOption = null;

			/* Requires Authentication for Remote Host */
			if (this.credential == null && ComputerName != null) {
				bool requiredAuth = false;
				foreach(var c in ComputerName)
				{
					if (c != null && !c.Equals("localhost", StringComparison.OrdinalIgnoreCase))
					{
						requiredAuth = true;
						break;
					}
				}
				if (requiredAuth)
				{
					TrySetCredentials();
				}
			}

			if (this.SessionOption != null) {
				if (this.SessionOption as WSManSessionOptions == null) {
					dComSessionOption = new DComSessionOptions (this.sessionOption as DComSessionOptions);
				} else {
					dComSessionOption = new WSManSessionOptions (this.sessionOption as WSManSessionOptions);
				}
			}
			outputOptions = null;
			outputCredential = null;
			if (dComSessionOption != null) {
				DComSessionOptions dComSessionOption1 = dComSessionOption as DComSessionOptions;
				if (dComSessionOption1 != null) {
					bool flag = false;
					string empty = string.Empty;
					if (this.CertificateThumbprint != null) {
						flag = true;
						empty = "CertificateThumbprint";
					}
					if (this.portSet) {
						flag = true;
						empty = "Port";
					}
					if (flag) {
						base.ThrowConflictParameterWasSet ("New-CimSession", empty, "DComSessionOptions");
						return;
					}
				}
			}

			if (this.portSet || this.CertificateThumbprint != null) {
				if (dComSessionOption == null) {
					wSManSessionOption = new WSManSessionOptions ();
				} else {
					wSManSessionOption = dComSessionOption as WSManSessionOptions;
				}
				WSManSessionOptions port = wSManSessionOption;
				if (this.portSet) {
					port.DestinationPort = this.Port;
					this.portSet = false;
				}
				if (this.CertificateThumbprint != null) {
					CimCredential cimCredential = new CimCredential (CertificateAuthenticationMechanism.Default, this.CertificateThumbprint);
					port.AddDestinationCredentials (cimCredential);
				}
				dComSessionOption = port;
			}
			if (this.operationTimeoutSet && dComSessionOption != null) {
				dComSessionOption.Timeout = TimeSpan.FromSeconds ((double)((float)this.OperationTimeoutSec));
			}

			if (this.authenticationSet || this.credential != null) {
				if (this.authenticationSet) {
					authentication = this.Authentication;
				} else {
					authentication = PasswordAuthenticationMechanism.Default;
				}
				PasswordAuthenticationMechanism passwordAuthenticationMechanism = authentication;
				if (this.authenticationSet) {
					this.authenticationSet = false;
				}
				CimCredential cimCredential1 = base.CreateCimCredentials (this.Credential, passwordAuthenticationMechanism, "New-CimSession", "Authentication");
				if (cimCredential1 != null) {
					object[] objArray = new object[1];
					objArray [0] = cimCredential1;
					DebugHelper.WriteLog ("Credentials: {0}", 1, objArray);
					outputCredential = cimCredential1;
					if (dComSessionOption != null) {
						object[] objArray1 = new object[1];
						objArray1 [0] = dComSessionOption;
						DebugHelper.WriteLog ("Add credentials to option: {0}", 1, objArray1);
						dComSessionOption.AddDestinationCredentials (cimCredential1);
					}
				} else {
					return;
				}
			}
		
			object[] objArray2 = new object[1];
			objArray2[0] = outputOptions;
			DebugHelper.WriteLogEx("Set outputOptions: {0}", 1, objArray2);
			outputOptions = dComSessionOption;
		}
        public static void Main()
        {
            bool hasComputerNameChanged = true;
            CimSession cimSession = null;
            CimSessionOptions sessionOptions = null;
            
            string className = null;
            string computerName = @"."; //GetName("ComputerName");
            if (String.IsNullOrEmpty(computerName))
            {
                computerName = null;
            }

            string namespaceName = @"root\cimv2"; //GetName("Namespace");
            CurrentOperation currentOperation = GetCurrentOption(true);
            while (true)
            {
                if (currentOperation == CurrentOperation.OperationQuit)
                {
                    if (cimSession != null)
                    {
                        cimSession.Close();
                        cimSession = null;
                    }

                    return;
                }

                if (ClassNeeded(currentOperation))
                {
                    className = GetName("ClassName");
                }

                try
                {
                    // Create local CIM session
                    if (hasComputerNameChanged)
                    {
                        if (cimSession != null)
                        {
                            cimSession.Close();
                        }

                        sessionOptions = new DComSessionOptions();
                        sessionOptions.Timeout = new TimeSpan(
                                                            0, // Hours
                                                            2, // Minutes 
                                                            0  // Seconds
                                                            );
                        cimSession = CimSession.Create(computerName, sessionOptions);
                        
                        hasComputerNameChanged = false;
                    }

                    switch (currentOperation)
                    {
                        case CurrentOperation.EnumerateAsync:
                            SampleCimOperation.EnumerateASync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.EnumerateSync:
                            SampleCimOperation.EnumerateSync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.GetInstanceSync:
                            SampleCimOperation.GetInstanceSync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.GetInstanceAsync:
                            SampleCimOperation.GetInstanceASync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.CreateInstanceAsync:
                            SampleCimOperation.CreateInstanceASync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.CreateInstanceSync:
                            SampleCimOperation.CreateInstanceSync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.DeleteInstanceAsync:
                            SampleCimOperation.DeleteInstanceASync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.DeleteInstanceSync:
                            SampleCimOperation.DeleteInstanceSync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.ModifyInstanceAsync:
                            SampleCimOperation.ModifyInstanceASync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.ModifyInstanceSync:
                            SampleCimOperation.ModifyInstanceSync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.QueryInstanceAsync:
                            SampleCimOperation.QueryInstanceASync(cimSession, namespaceName);
                            break;
                        case CurrentOperation.QueryInstanceSync:
                            SampleCimOperation.QueryInstanceSync(cimSession, namespaceName);
                            break;
                        case CurrentOperation.QueryAssociationSync:
                            SampleCimOperation.EnumerateAssociatedInstanceSync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.QueryAssociationAsync:
                            SampleCimOperation.EnumerateAssociatedInstanceASync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.InvokeMethodSync:
                            SampleCimOperation.InvokeMethodSync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.InvokeMethodAsync:
                            SampleCimOperation.InvokeMethodASync(cimSession, namespaceName, className);
                            break;
                        case CurrentOperation.SubscribeSync:
                            SampleCimOperation.SubscribeSync(cimSession, namespaceName);
                            break;
                        case CurrentOperation.SubscribeAsync:
                            SampleCimOperation.SubscribeASync(cimSession, namespaceName);
                            break;
                        case CurrentOperation.OperationComputerName:
                            computerName = GetName("ComputerName");
                            if (String.IsNullOrEmpty(computerName))
                            {
                                computerName = null;
                            }

                            hasComputerNameChanged = true;
                            break;
                        case CurrentOperation.OperationNamespaceName:
                            namespaceName = GetName("Namespace");
                            break;
                        default:
                            break;
                    }
                }
                catch (CimException ex)
                {
                    Console.WriteLine(ex.Message);
                }

                currentOperation = GetCurrentOption(false);
            }
        }
Beispiel #8
0
		internal static CimSessionOptions CreateCimSessionOption(string computerName, uint timeout, CimCredential credential)
		{
			CimSessionOptions wSManSessionOption;
			DebugHelper.WriteLogEx();
			if (!ConstValue.IsDefaultComputerName(computerName))
			{
				object[] objArray = new object[1];
				objArray[0] = computerName;
				DebugHelper.WriteLog("<<<<<<<<<< Use protocol WSMAN {0}", 1, objArray);
				wSManSessionOption = new WSManSessionOptions();
			}
			else
			{
				object[] objArray1 = new object[1];
				objArray1[0] = computerName;
				DebugHelper.WriteLog("<<<<<<<<<< Use protocol DCOM  {0}", 1, objArray1);
				wSManSessionOption = new DComSessionOptions();
			}
			if (timeout != 0)
			{
				wSManSessionOption.Timeout = TimeSpan.FromSeconds((double)((float)timeout));
			}
			if (credential != null)
			{
				wSManSessionOption.AddDestinationCredentials(credential);
			}
			object[] objArray2 = new object[1];
			objArray2[0] = wSManSessionOption;
			DebugHelper.WriteLogEx("returned option :{0}.", 1, objArray2);
			return wSManSessionOption;
		}