Esempio n. 1
0
		internal static bool RestartOneComputerUsingWsman(PSCmdlet cmdlet, bool isLocalhost, string computerName, object[] flags, PSCredential credential, string authentication, CancellationToken token)
		{
			bool flag;
			string str;
			string str1;
			PSCredential pSCredential;
			object obj;
			string str2;
			bool flag1 = false;
			if (isLocalhost)
			{
				str = "localhost";
			}
			else
			{
				str = computerName;
			}
			string str3 = str;
			if (isLocalhost)
			{
				str1 = null;
			}
			else
			{
				str1 = authentication;
			}
			string str4 = str1;
			if (isLocalhost)
			{
				pSCredential = null;
			}
			else
			{
				pSCredential = credential;
			}
			PSCredential pSCredential1 = pSCredential;
			Win32Native.TOKEN_PRIVILEGE tOKENPRIVILEGE = new Win32Native.TOKEN_PRIVILEGE();
			CimOperationOptions cimOperationOption = new CimOperationOptions();
			cimOperationOption.Timeout = TimeSpan.FromMilliseconds(10000);
			cimOperationOption.CancellationToken = new CancellationToken?(token);
			CimOperationOptions cimOperationOption1 = cimOperationOption;
			try
			{
				try
				{
					if ((!isLocalhost || !ComputerWMIHelper.EnableTokenPrivilege("SeShutdownPrivilege", ref tOKENPRIVILEGE)) && (isLocalhost || !ComputerWMIHelper.EnableTokenPrivilege("SeRemoteShutdownPrivilege", ref tOKENPRIVILEGE)))
					{
						string privilegeNotEnabled = ComputerResources.PrivilegeNotEnabled;
						string str5 = computerName;
						if (isLocalhost)
						{
							obj = "SeShutdownPrivilege";
						}
						else
						{
							obj = "SeRemoteShutdownPrivilege";
						}
						string str6 = StringUtil.Format(privilegeNotEnabled, str5, obj);
						ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str6), "PrivilegeNotEnabled", ErrorCategory.InvalidOperation, null);
						cmdlet.WriteError(errorRecord);
						flag = false;
						return flag;
					}
					else
					{
						CimSession cimSession = RemoteDiscoveryHelper.CreateCimSession(str3, pSCredential1, str4, token, cmdlet);
						using (cimSession)
						{
							CimMethodParametersCollection cimMethodParametersCollection = new CimMethodParametersCollection();
							cimMethodParametersCollection.Add(CimMethodParameter.Create("Flags", flags[0], Microsoft.Management.Infrastructure.CimType.SInt32, (CimFlags)((long)0)));
							cimMethodParametersCollection.Add(CimMethodParameter.Create("Reserved", flags[1], Microsoft.Management.Infrastructure.CimType.SInt32, (CimFlags)((long)0)));
							CimMethodResult cimMethodResult = cimSession.InvokeMethod("root/cimv2", "Win32_OperatingSystem", "Win32shutdown", cimMethodParametersCollection, cimOperationOption1);
							int num = Convert.ToInt32(cimMethodResult.ReturnValue.Value, CultureInfo.CurrentCulture);
							if (num == 0)
							{
								flag1 = true;
							}
							else
							{
								Win32Exception win32Exception = new Win32Exception(num);
								string str7 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, win32Exception.Message);
								ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(str7), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
								cmdlet.WriteError(errorRecord1);
							}
						}
					}
				}
				catch (CimException cimException1)
				{
					CimException cimException = cimException1;
					string str8 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, cimException.Message);
					ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(str8), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
					cmdlet.WriteError(errorRecord2);
				}
				catch (Exception exception1)
				{
					Exception exception = exception1;
					CommandProcessorBase.CheckForSevereException(exception);
					string str9 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, exception.Message);
					ErrorRecord errorRecord3 = new ErrorRecord(new InvalidOperationException(str9), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
					cmdlet.WriteError(errorRecord3);
				}
				return flag1;
			}
			finally
			{
				if (isLocalhost)
				{
					str2 = "SeShutdownPrivilege";
				}
				else
				{
					str2 = "SeRemoteShutdownPrivilege";
				}
				ComputerWMIHelper.RestoreTokenPrivilege(str2, ref tOKENPRIVILEGE);
			}
			return flag;
		}
Esempio n. 2
0
		private void ConnectInvokeWmi()
		{
			ManagementObject managementObject;
			object obj;
			string str;
			InvokeWmiMethod invokeWmiMethod = (InvokeWmiMethod)this.wmiObject;
			this.state = WmiState.Running;
			this.RaiseWmiOperationState(null, WmiState.Running);
			if (invokeWmiMethod.InputObject == null)
			{
				ConnectionOptions connectionOption = invokeWmiMethod.GetConnectionOption();
				ManagementPath managementPath = null;
				if (invokeWmiMethod.Path != null)
				{
					managementPath = new ManagementPath(invokeWmiMethod.Path);
					if (!string.IsNullOrEmpty(managementPath.NamespacePath))
					{
						if (invokeWmiMethod.namespaceSpecified)
						{
							InvalidOperationException invalidOperationException = new InvalidOperationException("NamespaceSpecifiedWithPath");
							this.internalException = invalidOperationException;
							this.state = WmiState.Failed;
							this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
							return;
						}
					}
					else
					{
						managementPath.NamespacePath = invokeWmiMethod.Namespace;
					}
					if (!(managementPath.Server != ".") || !invokeWmiMethod.serverNameSpecified)
					{
						if (!(managementPath.Server == ".") || !invokeWmiMethod.serverNameSpecified)
						{
							this.computerName = managementPath.Server;
						}
					}
					else
					{
						InvalidOperationException invalidOperationException1 = new InvalidOperationException("ComputerNameSpecifiedWithPath");
						this.internalException = invalidOperationException1;
						this.state = WmiState.Failed;
						this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
						return;
					}
				}
				bool flag = false;
				bool enablePrivilege = false;
				Win32Native.TOKEN_PRIVILEGE tOKENPRIVILEGE = new Win32Native.TOKEN_PRIVILEGE();
				try
				{
					try
					{
						enablePrivilege = this.NeedToEnablePrivilege(this.computerName, invokeWmiMethod.Name, ref flag);
						if (!enablePrivilege || flag && ComputerWMIHelper.EnableTokenPrivilege("SeShutdownPrivilege", ref tOKENPRIVILEGE) || !flag && ComputerWMIHelper.EnableTokenPrivilege("SeRemoteShutdownPrivilege", ref tOKENPRIVILEGE))
						{
							if (invokeWmiMethod.Path == null)
							{
								ManagementScope managementScope = new ManagementScope(WMIHelper.GetScopeString(this.computerName, invokeWmiMethod.Namespace), connectionOption);
								ManagementClass managementClass = new ManagementClass(invokeWmiMethod.Class);
								managementObject = managementClass;
								managementObject.Scope = managementScope;
							}
							else
							{
								managementPath.Server = this.computerName;
								if (!managementPath.IsClass)
								{
									ManagementObject managementObject1 = new ManagementObject(managementPath);
									managementObject = managementObject1;
								}
								else
								{
									ManagementClass managementClass1 = new ManagementClass(managementPath);
									managementObject = managementClass1;
								}
								ManagementScope managementScope1 = new ManagementScope(managementPath, connectionOption);
								managementObject.Scope = managementScope1;
							}
							ManagementBaseObject methodParameters = managementObject.GetMethodParameters(invokeWmiMethod.Name);
							if (invokeWmiMethod.ArgumentList != null)
							{
								int length = (int)invokeWmiMethod.ArgumentList.Length;
								foreach (PropertyData property in methodParameters.Properties)
								{
									if (length == 0)
									{
										break;
									}
									property.Value = invokeWmiMethod.ArgumentList[(int)invokeWmiMethod.ArgumentList.Length - length];
									length--;
								}
							}
							if (!enablePrivilege)
							{
								managementObject.InvokeMethod(this.results, invokeWmiMethod.Name, methodParameters, null);
							}
							else
							{
								ManagementBaseObject managementBaseObject = managementObject.InvokeMethod(invokeWmiMethod.Name, methodParameters, null);
								int num = Convert.ToInt32(managementBaseObject["ReturnValue"], CultureInfo.CurrentCulture);
								if (num == 0)
								{
									this.ShutdownComplete.SafeInvoke<EventArgs>(this, null);
								}
								else
								{
									Win32Exception win32Exception = new Win32Exception(num);
									this.internalException = win32Exception;
									this.state = WmiState.Failed;
									this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
								}
							}
						}
						else
						{
							string privilegeNotEnabled = ComputerResources.PrivilegeNotEnabled;
							string str1 = this.computerName;
							if (flag)
							{
								obj = "SeShutdownPrivilege";
							}
							else
							{
								obj = "SeRemoteShutdownPrivilege";
							}
							string str2 = StringUtil.Format(privilegeNotEnabled, str1, obj);
							InvalidOperationException invalidOperationException2 = new InvalidOperationException(str2);
							this.internalException = invalidOperationException2;
							this.state = WmiState.Failed;
							this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
							return;
						}
					}
					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);
					}
				}
				finally
				{
					if (enablePrivilege)
					{
						if (flag)
						{
							str = "SeShutdownPrivilege";
						}
						else
						{
							str = "SeRemoteShutdownPrivilege";
						}
						ComputerWMIHelper.RestoreTokenPrivilege(str, ref tOKENPRIVILEGE);
					}
				}
				return;
			}
			else
			{
				try
				{
					ManagementBaseObject methodParameters1 = invokeWmiMethod.InputObject.GetMethodParameters(invokeWmiMethod.Name);
					if (invokeWmiMethod.ArgumentList != null)
					{
						int length1 = (int)invokeWmiMethod.ArgumentList.Length;
						foreach (PropertyData argumentList in methodParameters1.Properties)
						{
							if (length1 == 0)
							{
								break;
							}
							argumentList.Value = invokeWmiMethod.ArgumentList[(int)invokeWmiMethod.ArgumentList.Length - length1];
							length1--;
						}
					}
					invokeWmiMethod.InputObject.InvokeMethod(this.results, invokeWmiMethod.Name, methodParameters1, null);
				}
				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;
			}
		}
Esempio n. 3
0
		internal static bool RestartOneComputerUsingDcom(PSCmdlet cmdlet, bool isLocalhost, string computerName, object[] flags, ConnectionOptions options)
		{
			bool flag;
			object obj;
			string str;
			string str1;
			bool flag1 = false;
			ManagementObjectSearcher managementObjectSearcher = null;
			Win32Native.TOKEN_PRIVILEGE tOKENPRIVILEGE = new Win32Native.TOKEN_PRIVILEGE();
			try
			{
				try
				{
					if ((!isLocalhost || !ComputerWMIHelper.EnableTokenPrivilege("SeShutdownPrivilege", ref tOKENPRIVILEGE)) && (isLocalhost || !ComputerWMIHelper.EnableTokenPrivilege("SeRemoteShutdownPrivilege", ref tOKENPRIVILEGE)))
					{
						string privilegeNotEnabled = ComputerResources.PrivilegeNotEnabled;
						string str2 = computerName;
						if (isLocalhost)
						{
							obj = "SeShutdownPrivilege";
						}
						else
						{
							obj = "SeRemoteShutdownPrivilege";
						}
						string str3 = StringUtil.Format(privilegeNotEnabled, str2, obj);
						ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str3), "PrivilegeNotEnabled", ErrorCategory.InvalidOperation, null);
						cmdlet.WriteError(errorRecord);
						flag = false;
						return flag;
					}
					else
					{
						if (isLocalhost)
						{
							str = "localhost";
						}
						else
						{
							str = computerName;
						}
						ManagementScope managementScope = new ManagementScope(ComputerWMIHelper.GetScopeString(str, "\\root\\cimv2"), options);
						EnumerationOptions enumerationOption = new EnumerationOptions();
						enumerationOption.UseAmendedQualifiers = true;
						enumerationOption.DirectRead = true;
						EnumerationOptions enumerationOption1 = enumerationOption;
						ObjectQuery objectQuery = new ObjectQuery("select * from Win32_OperatingSystem");
						managementObjectSearcher = new ManagementObjectSearcher(managementScope, objectQuery, enumerationOption1);
						foreach (ManagementObject managementObject in managementObjectSearcher.Get())
						{
							object obj1 = managementObject.InvokeMethod("Win32shutdown", flags);
							int num = Convert.ToInt32(obj1.ToString(), CultureInfo.CurrentCulture);
							if (num == 0)
							{
								flag1 = true;
							}
							else
							{
								Win32Exception win32Exception = new Win32Exception(num);
								string str4 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, win32Exception.Message);
								ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(str4), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
								cmdlet.WriteError(errorRecord1);
							}
						}
					}
				}
				catch (ManagementException managementException1)
				{
					ManagementException managementException = managementException1;
					string str5 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, managementException.Message);
					ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(str5), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
					cmdlet.WriteError(errorRecord2);
				}
				catch (COMException cOMException1)
				{
					COMException cOMException = cOMException1;
					string str6 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, cOMException.Message);
					ErrorRecord errorRecord3 = new ErrorRecord(new InvalidOperationException(str6), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
					cmdlet.WriteError(errorRecord3);
				}
				catch (UnauthorizedAccessException unauthorizedAccessException1)
				{
					UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1;
					string str7 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, unauthorizedAccessException.Message);
					ErrorRecord errorRecord4 = new ErrorRecord(new InvalidOperationException(str7), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
					cmdlet.WriteError(errorRecord4);
				}
				return flag1;
			}
			finally
			{
				if (isLocalhost)
				{
					str1 = "SeShutdownPrivilege";
				}
				else
				{
					str1 = "SeRemoteShutdownPrivilege";
				}
				ComputerWMIHelper.RestoreTokenPrivilege(str1, ref tOKENPRIVILEGE);
				if (managementObjectSearcher != null)
				{
					managementObjectSearcher.Dispose();
				}
			}
			return flag;
		}