private string GetWmiQueryString()
        {
            GetWmiObjectCommand getWmiObjectCommand = (GetWmiObjectCommand)this.wmiObject;
            StringBuilder       stringBuilder       = new StringBuilder("select ");

            stringBuilder.Append(string.Join(", ", getWmiObjectCommand.Property));
            stringBuilder.Append(" from ");
            stringBuilder.Append(getWmiObjectCommand.Class);
            if (!string.IsNullOrEmpty(getWmiObjectCommand.Filter))
            {
                stringBuilder.Append(" where ");
                stringBuilder.Append(getWmiObjectCommand.Filter);
            }
            return(stringBuilder.ToString());
        }
示例#2
0
        private void processDCOMProtocolForTestConnection()
        {
            ConnectionOptions options = ComputerWMIHelper.GetConnectionOptions(DcomAuthentication, this.Impersonation, this.Credential);
            if (AsJob)
            {
                string filter = QueryString(ComputerName, true, false);
                GetWmiObjectCommand WMICmd = new GetWmiObjectCommand();
                WMICmd.Filter = filter.ToString();
                WMICmd.Class = ComputerWMIHelper.WMI_Class_PingStatus;
                WMICmd.ComputerName = Source;
                WMICmd.Authentication = DcomAuthentication;
                WMICmd.Impersonation = Impersonation;
                WMICmd.ThrottleLimit = throttlelimit;
                PSWmiJob wmiJob = new PSWmiJob(WMICmd, Source, throttlelimit, this.MyInvocation.MyCommand.Name, Count);
                this.JobRepository.Add(wmiJob);
                WriteObject(wmiJob);
            }
            else
            {
                int sourceCount = 0;
                foreach (string fromcomp in Source)
                {
                    try
                    {
                        sourceCount++;
                        EnumerationOptions enumOptions = new EnumerationOptions();
                        enumOptions.UseAmendedQualifiers = true;
                        enumOptions.DirectRead = true;

                        int destCount = 0;
                        foreach (var tocomp in ComputerName)
                        {
                            destCount++;
                            string querystring = QueryString(new string[] { tocomp }, true, true);
                            ObjectQuery query = new ObjectQuery(querystring);
                            ManagementScope scope = new ManagementScope(ComputerWMIHelper.GetScopeString(fromcomp, ComputerWMIHelper.WMI_Path_CIM), options);
                            scope.Options.EnablePrivileges = true;
                            scope.Connect();

                            using (searcher = new ManagementObjectSearcher(scope, query, enumOptions))
                            {
                                for (int j = 0; j <= Count - 1; j++)
                                {
                                    using (ManagementObjectCollection mobj = searcher.Get())
                                    {
                                        int mobjCount = 0;
                                        foreach (ManagementBaseObject obj in mobj)
                                        {
                                            using (obj)
                                            {
                                                mobjCount++;

                                                ProcessPingStatus(obj);

                                                // to delay the request, if case to avoid the delay for the last pingrequest
                                                if (mobjCount < mobj.Count || j < Count - 1 || sourceCount < Source.Length || destCount < ComputerName.Length)
                                                    Thread.Sleep(Delay * 1000);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        searcher = null;
                    }
                    catch (ManagementException e)
                    {
                        ErrorRecord errorRecord = new ErrorRecord(e, "TestConnectionException", ErrorCategory.InvalidOperation, null);
                        WriteError(errorRecord);
                        continue;
                    }
                    catch (System.Runtime.InteropServices.COMException e)
                    {
                        ErrorRecord errorRecord = new ErrorRecord(e, "TestConnectionException", ErrorCategory.InvalidOperation, null);
                        WriteError(errorRecord);
                        continue;
                    }
                }
            }

            if (quiet)
            {
                foreach (string destinationAddress in this.ComputerName)
                {
                    bool destinationResult = false;
                    this.quietResults.TryGetValue(destinationAddress, out destinationResult);
                    WriteObject(destinationResult);
                }
            }
        }
        protected override void ProcessRecord()
        {
            ConnectionOptions connection = ComputerWMIHelper.GetConnection(this.Authentication, this.Impersonation, this.Credential);

            if (!this.asjob)
            {
                int      num       = 0;
                string[] strArrays = this.source;
                for (int i = 0; i < (int)strArrays.Length; i++)
                {
                    string str = strArrays[i];
                    try
                    {
                        num++;
                        string          str1            = this.QueryString(this.destination, true, true);
                        ObjectQuery     objectQuery     = new ObjectQuery(str1);
                        ManagementScope managementScope = new ManagementScope(ComputerWMIHelper.GetScopeString(str, "\\root\\cimv2"), connection);
                        managementScope.Options.EnablePrivileges = true;
                        managementScope.Connect();
                        EnumerationOptions enumerationOption = new EnumerationOptions();
                        enumerationOption.UseAmendedQualifiers = true;
                        enumerationOption.DirectRead           = true;
                        this.searcher = new ManagementObjectSearcher(managementScope, objectQuery, enumerationOption);
                        for (int j = 0; j <= this.count - 1; j++)
                        {
                            ManagementObjectCollection managementObjectCollections = this.searcher.Get();
                            int num1 = 0;
                            foreach (ManagementBaseObject managementBaseObject in managementObjectCollections)
                            {
                                num1++;
                                this.ProcessPingStatus(managementBaseObject);
                                if (num1 >= managementObjectCollections.Count && j >= this.count - 1 && num >= (int)this.Source.Length)
                                {
                                    continue;
                                }
                                Thread.Sleep(this.delay * 0x3e8);
                            }
                        }
                    }
                    catch (ManagementException managementException1)
                    {
                        ManagementException managementException = managementException1;
                        ErrorRecord         errorRecord         = new ErrorRecord(managementException, "TestConnectionException", ErrorCategory.InvalidOperation, null);
                        base.WriteError(errorRecord);
                    }
                    catch (COMException cOMException1)
                    {
                        COMException cOMException = cOMException1;
                        ErrorRecord  errorRecord1 = new ErrorRecord(cOMException, "TestConnectionException", ErrorCategory.InvalidOperation, null);
                        base.WriteError(errorRecord1);
                    }
                }
            }
            else
            {
                string str2 = this.QueryString(this.destination, true, false);
                GetWmiObjectCommand getWmiObjectCommand = new GetWmiObjectCommand();
                getWmiObjectCommand.Filter         = str2.ToString();
                getWmiObjectCommand.Class          = "Win32_PingStatus";
                getWmiObjectCommand.ComputerName   = this.source;
                getWmiObjectCommand.Authentication = this.Authentication;
                getWmiObjectCommand.Impersonation  = this.Impersonation;
                getWmiObjectCommand.ThrottleLimit  = this.throttlelimit;
                PSWmiJob pSWmiJob = new PSWmiJob(getWmiObjectCommand, this.source, this.throttlelimit, base.MyInvocation.MyCommand.Name, this.count);
                base.JobRepository.Add(pSWmiJob);
                base.WriteObject(pSWmiJob);
            }
            if (this.quiet)
            {
                string[] strArrays1 = this.destination;
                for (int k = 0; k < (int)strArrays1.Length; k++)
                {
                    string str3 = strArrays1[k];
                    bool   flag = false;
                    this.quietResults.TryGetValue(str3, out flag);
                    base.WriteObject(flag);
                }
            }
        }
        private void ConnectGetWMI()
        {
            string wmiQueryString;
            GetWmiObjectCommand getWmiObjectCommand = (GetWmiObjectCommand)this.wmiObject;

            this.state = WmiState.Running;
            this.RaiseWmiOperationState(null, WmiState.Running);
            ConnectionOptions connectionOption = getWmiObjectCommand.GetConnectionOption();
            SwitchParameter   list             = getWmiObjectCommand.List;

            if (!list.IsPresent)
            {
                if (string.IsNullOrEmpty(getWmiObjectCommand.Query))
                {
                    wmiQueryString = this.GetWmiQueryString();
                }
                else
                {
                    wmiQueryString = getWmiObjectCommand.Query;
                }
                string      str         = wmiQueryString;
                ObjectQuery objectQuery = new ObjectQuery(str.ToString());
                try
                {
                    ManagementScope    managementScope   = new ManagementScope(WMIHelper.GetScopeString(this.computerName, getWmiObjectCommand.Namespace), connectionOption);
                    EnumerationOptions enumerationOption = new EnumerationOptions();
                    enumerationOption.UseAmendedQualifiers = getWmiObjectCommand.Amended;
                    enumerationOption.DirectRead           = getWmiObjectCommand.DirectRead;
                    ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(managementScope, objectQuery, enumerationOption);
                    for (int i = 0; i < this.cmdCount; i++)
                    {
                        managementObjectSearcher.Get(this.results);
                    }
                }
                catch (ManagementException managementException1)
                {
                    ManagementException managementException = managementException1;
                    this.internalException = managementException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (COMException cOMException1)
                {
                    COMException cOMException = cOMException1;
                    this.internalException = cOMException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (UnauthorizedAccessException unauthorizedAccessException1)
                {
                    UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1;
                    this.internalException = unauthorizedAccessException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                return;
            }
            else
            {
                if (getWmiObjectCommand.ValidateClassFormat())
                {
                    try
                    {
                        SwitchParameter recurse = getWmiObjectCommand.Recurse;
                        if (!recurse.IsPresent)
                        {
                            ManagementScope managementScope1 = new ManagementScope(WMIHelper.GetScopeString(this.computerName, getWmiObjectCommand.Namespace), connectionOption);
                            managementScope1.Connect();
                            ManagementObjectSearcher objectList = getWmiObjectCommand.GetObjectList(managementScope1);
                            if (objectList != null)
                            {
                                objectList.Get(this.results);
                            }
                            else
                            {
                                throw new ManagementException();
                            }
                        }
                        else
                        {
                            ArrayList arrayLists  = new ArrayList();
                            ArrayList arrayLists1 = new ArrayList();
                            ArrayList arrayLists2 = new ArrayList();
                            int       num         = 0;
                            arrayLists.Add(getWmiObjectCommand.Namespace);
                            bool flag = true;
                            while (num < arrayLists.Count)
                            {
                                string          item             = (string)arrayLists[num];
                                ManagementScope managementScope2 = new ManagementScope(WMIHelper.GetScopeString(this.computerName, item), connectionOption);
                                managementScope2.Connect();
                                ManagementClass managementClass = new ManagementClass(managementScope2, new ManagementPath("__Namespace"), new ObjectGetOptions());
                                foreach (ManagementBaseObject instance in managementClass.GetInstances())
                                {
                                    if (getWmiObjectCommand.IsLocalizedNamespace((string)instance["Name"]))
                                    {
                                        continue;
                                    }
                                    arrayLists.Add(string.Concat(item, "\\", instance["Name"]));
                                }
                                if (!flag)
                                {
                                    arrayLists1.Add(this.Job.GetNewSink());
                                }
                                else
                                {
                                    flag = false;
                                    arrayLists1.Add(this.results);
                                }
                                arrayLists2.Add(managementScope2);
                                num++;
                            }
                            if (arrayLists1.Count != arrayLists.Count || arrayLists2.Count != arrayLists.Count)
                            {
                                this.internalException = new InvalidOperationException();
                                this.state             = WmiState.Failed;
                                this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                                return;
                            }
                            else
                            {
                                num = 0;
                                while (num < arrayLists.Count)
                                {
                                    ManagementObjectSearcher objectList1 = getWmiObjectCommand.GetObjectList((ManagementScope)arrayLists2[num]);
                                    if (objectList1 != null)
                                    {
                                        if (!flag)
                                        {
                                            objectList1.Get((ManagementOperationObserver)arrayLists1[num]);
                                        }
                                        else
                                        {
                                            flag = false;
                                            objectList1.Get(this.results);
                                        }
                                        num++;
                                    }
                                    else
                                    {
                                        num++;
                                    }
                                }
                            }
                        }
                    }
                    catch (ManagementException managementException3)
                    {
                        ManagementException managementException2 = managementException3;
                        this.internalException = managementException2;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                    catch (COMException cOMException3)
                    {
                        COMException cOMException2 = cOMException3;
                        this.internalException = cOMException2;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                    catch (UnauthorizedAccessException unauthorizedAccessException3)
                    {
                        UnauthorizedAccessException unauthorizedAccessException2 = unauthorizedAccessException3;
                        this.internalException = unauthorizedAccessException2;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                    return;
                }
                else
                {
                    object[] @class = new object[1];
                    @class[0] = getWmiObjectCommand.Class;
                    ArgumentException argumentException = new ArgumentException(string.Format(Thread.CurrentThread.CurrentCulture, "Class", @class));
                    this.internalException = argumentException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    return;
                }
            }
        }
示例#5
0
		private static void RunDirectExecutionActivity(System.Management.Automation.PowerShell commandToRun, PSDataCollection<PSObject> input, PSDataCollection<PSObject> output, PSActivityContext psActivityContext, ActivityImplementationContext implementationContext)
		{
			PSDataCollection<PSObject> pSObjects;
			Type type;
			CimSessionOptions sessionOptions;
			Command item = commandToRun.Commands.Commands[0];
			string commandText = item.CommandText;
			Cmdlet invokeWmiMethod = null;
			bool flag = false;
			if (!string.Equals(commandText, "Get-WMIObject", StringComparison.OrdinalIgnoreCase))
			{
				if (string.Equals(commandText, "Invoke-WMIMethod", StringComparison.OrdinalIgnoreCase))
				{
					invokeWmiMethod = new InvokeWmiMethod();
					flag = true;
				}
			}
			else
			{
				invokeWmiMethod = new GetWmiObjectCommand();
			}
			if (!PSActivity.CheckForCancel(psActivityContext))
			{
				if (output != null)
				{
					pSObjects = output;
				}
				else
				{
					pSObjects = new PSDataCollection<PSObject>();
				}
				ActivityImplementationContext activityImplementationContext = implementationContext;
				if (invokeWmiMethod != null)
				{
					type = invokeWmiMethod.GetType();
				}
				else
				{
					type = psActivityContext.TypeImplementingCmdlet;
				}
				DirectExecutionActivitiesCommandRuntime directExecutionActivitiesCommandRuntime = new DirectExecutionActivitiesCommandRuntime(pSObjects, activityImplementationContext, type);
				directExecutionActivitiesCommandRuntime.Error = commandToRun.Streams.Error;
				directExecutionActivitiesCommandRuntime.Warning = commandToRun.Streams.Warning;
				directExecutionActivitiesCommandRuntime.Progress = commandToRun.Streams.Progress;
				directExecutionActivitiesCommandRuntime.Verbose = commandToRun.Streams.Verbose;
				directExecutionActivitiesCommandRuntime.Debug = commandToRun.Streams.Debug;
				if (invokeWmiMethod == null)
				{
					CimActivityImplementationContext cimActivityImplementationContext = implementationContext as CimActivityImplementationContext;
					if (cimActivityImplementationContext != null)
					{
						sessionOptions = cimActivityImplementationContext.SessionOptions;
					}
					else
					{
						sessionOptions = null;
					}
					CimSessionOptions cimSessionOption = sessionOptions;
					if (psActivityContext.TypeImplementingCmdlet != null)
					{
						if (input == null || input.Count <= 0 && !input.IsOpen)
						{
							CimBaseCommand cimBaseCommand = (CimBaseCommand)Activator.CreateInstance(psActivityContext.TypeImplementingCmdlet);
							using (cimBaseCommand)
							{
								cimBaseCommand.CommandRuntime = directExecutionActivitiesCommandRuntime;
								PSObject pSObject = PSObject.AsPSObject(cimBaseCommand);
								PSActivity.InitializeCmdletInstanceParameters(item, pSObject, true, psActivityContext, cimSessionOption, implementationContext);
								cimBaseCommand.Invoke().GetEnumerator().MoveNext();
							}
						}
						else
						{
							if (psActivityContext.TypeImplementingCmdlet.GetProperty("InputObject") != null)
							{
								foreach (PSObject pSObject1 in input)
								{
									try
									{
										CimBaseCommand cimBaseCommand1 = (CimBaseCommand)Activator.CreateInstance(psActivityContext.TypeImplementingCmdlet);
										using (cimBaseCommand1)
										{
											cimBaseCommand1.CommandRuntime = directExecutionActivitiesCommandRuntime;
											CimInstance cimInstance = LanguagePrimitives.ConvertTo<CimInstance>(pSObject1);
											PSObject pSObject2 = PSObject.AsPSObject(cimBaseCommand1);
											PSActivity.InitializeCmdletInstanceParameters(item, pSObject2, true, psActivityContext, cimSessionOption, implementationContext);
											PSPropertyInfo pSPropertyInfo = pSObject2.Properties["InputObject"];
											pSPropertyInfo.Value = cimInstance;
											cimBaseCommand1.Invoke().GetEnumerator().MoveNext();
										}
									}
									catch (PSInvalidCastException pSInvalidCastException1)
									{
										PSInvalidCastException pSInvalidCastException = pSInvalidCastException1;
										if (pSInvalidCastException.ErrorRecord != null)
										{
											directExecutionActivitiesCommandRuntime.Error.Add(pSInvalidCastException.ErrorRecord);
										}
									}
									if (!PSActivity.CheckForCancel(psActivityContext))
									{
										continue;
									}
									return;
								}
							}
							else
							{
								object[] objArray = new object[1];
								objArray[0] = commandText;
								throw new NotImplementedException(string.Format(CultureInfo.CurrentCulture, Resources.CmdletDoesNotImplementInputObjectProperty, objArray));
							}
						}
					}
					else
					{
						throw new InvalidOperationException(commandText);
					}
				}
				else
				{
					invokeWmiMethod.CommandRuntime = directExecutionActivitiesCommandRuntime;
					PSObject pSObject3 = PSObject.AsPSObject(invokeWmiMethod);
					PSActivity.InitializeCmdletInstanceParameters(item, pSObject3, false, psActivityContext, null, implementationContext);
					if (!flag || input == null || input.Count <= 0 && !input.IsOpen)
					{
						invokeWmiMethod.Invoke().GetEnumerator().MoveNext();
						return;
					}
					else
					{
						InvokeWmiMethod invokeWmiMethod1 = invokeWmiMethod as InvokeWmiMethod;
						foreach (PSObject pSObject4 in input)
						{
							try
							{
								ManagementObject managementObject = LanguagePrimitives.ConvertTo<ManagementObject>(pSObject4);
								invokeWmiMethod1.InputObject = managementObject;
								invokeWmiMethod1.Invoke().GetEnumerator().MoveNext();
							}
							catch (PSInvalidCastException pSInvalidCastException3)
							{
								PSInvalidCastException pSInvalidCastException2 = pSInvalidCastException3;
								if (pSInvalidCastException2.ErrorRecord != null)
								{
									directExecutionActivitiesCommandRuntime.Error.Add(pSInvalidCastException2.ErrorRecord);
								}
							}
							if (!PSActivity.CheckForCancel(psActivityContext))
							{
								continue;
							}
							return;
						}
					}
				}
				return;
			}
			else
			{
				return;
			}
		}
示例#6
0
		protected override void ProcessRecord()
		{
			ConnectionOptions connection = ComputerWMIHelper.GetConnection(this.Authentication, this.Impersonation, this.Credential);
			if (!this.asjob)
			{
				int num = 0;
				string[] strArrays = this.source;
				for (int i = 0; i < (int)strArrays.Length; i++)
				{
					string str = strArrays[i];
					try
					{
						num++;
						string str1 = this.QueryString(this.destination, true, true);
						ObjectQuery objectQuery = new ObjectQuery(str1);
						ManagementScope managementScope = new ManagementScope(ComputerWMIHelper.GetScopeString(str, "\\root\\cimv2"), connection);
						managementScope.Options.EnablePrivileges = true;
						managementScope.Connect();
						EnumerationOptions enumerationOption = new EnumerationOptions();
						enumerationOption.UseAmendedQualifiers = true;
						enumerationOption.DirectRead = true;
						this.searcher = new ManagementObjectSearcher(managementScope, objectQuery, enumerationOption);
						for (int j = 0; j <= this.count - 1; j++)
						{
							ManagementObjectCollection managementObjectCollections = this.searcher.Get();
							int num1 = 0;
							foreach (ManagementBaseObject managementBaseObject in managementObjectCollections)
							{
								num1++;
								this.ProcessPingStatus(managementBaseObject);
								if (num1 >= managementObjectCollections.Count && j >= this.count - 1 && num >= (int)this.Source.Length)
								{
									continue;
								}
								Thread.Sleep(this.delay * 0x3e8);
							}
						}
					}
					catch (ManagementException managementException1)
					{
						ManagementException managementException = managementException1;
						ErrorRecord errorRecord = new ErrorRecord(managementException, "TestConnectionException", ErrorCategory.InvalidOperation, null);
						base.WriteError(errorRecord);
					}
					catch (COMException cOMException1)
					{
						COMException cOMException = cOMException1;
						ErrorRecord errorRecord1 = new ErrorRecord(cOMException, "TestConnectionException", ErrorCategory.InvalidOperation, null);
						base.WriteError(errorRecord1);
					}
				}
			}
			else
			{
				string str2 = this.QueryString(this.destination, true, false);
				GetWmiObjectCommand getWmiObjectCommand = new GetWmiObjectCommand();
				getWmiObjectCommand.Filter = str2.ToString();
				getWmiObjectCommand.Class = "Win32_PingStatus";
				getWmiObjectCommand.ComputerName = this.source;
				getWmiObjectCommand.Authentication = this.Authentication;
				getWmiObjectCommand.Impersonation = this.Impersonation;
				getWmiObjectCommand.ThrottleLimit = this.throttlelimit;
				PSWmiJob pSWmiJob = new PSWmiJob(getWmiObjectCommand, this.source, this.throttlelimit, base.MyInvocation.MyCommand.Name, this.count);
				base.JobRepository.Add(pSWmiJob);
				base.WriteObject(pSWmiJob);
			}
			if (this.quiet)
			{
				string[] strArrays1 = this.destination;
				for (int k = 0; k < (int)strArrays1.Length; k++)
				{
					string str3 = strArrays1[k];
					bool flag = false;
					this.quietResults.TryGetValue(str3, out flag);
					base.WriteObject(flag);
				}
			}
		}