/// <summary> /// Get the List of HotFixes installed on the Local Machine. /// </summary> protected override void BeginProcessing() { foreach (string computer in ComputerName) { bool foundRecord = false; StringBuilder QueryString = new StringBuilder(); ConnectionOptions conOptions = ComputerWMIHelper.GetConnectionOptions(AuthenticationLevel.Packet, ImpersonationLevel.Impersonate, this.Credential); ManagementScope scope = new ManagementScope(ComputerWMIHelper.GetScopeString(computer, ComputerWMIHelper.WMI_Path_CIM), conOptions); scope.Connect(); if (Id != null) { QueryString.Append("Select * from Win32_QuickFixEngineering where ("); for (int i = 0; i <= Id.Length - 1; i++) { QueryString.Append("HotFixID= '"); QueryString.Append(Id[i].ToString().Replace("'", "\\'")); QueryString.Append("'"); if (i < Id.Length - 1) { QueryString.Append(" Or "); } } QueryString.Append(")"); } else { QueryString.Append("Select * from Win32_QuickFixEngineering"); foundRecord = true; } _searchProcess = new ManagementObjectSearcher(scope, new ObjectQuery(QueryString.ToString())); foreach (ManagementObject obj in _searchProcess.Get()) { if (Description != null) { if (!FilterMatch(obj)) { continue; } } else { _inputContainsWildcard = true; } // try to translate the SID to a more friendly username // just stick with the SID if anything goes wrong string installed = (string)obj["InstalledBy"]; if (!String.IsNullOrEmpty(installed)) { try { SecurityIdentifier secObj = new SecurityIdentifier(installed); obj["InstalledBy"] = secObj.Translate(typeof(NTAccount));; } catch (IdentityNotMappedException) // thrown by SecurityIdentifier.Translate { } catch (SystemException) // thrown by SecurityIdentifier.constr { } //catch (ArgumentException) // thrown (indirectly) by SecurityIdentifier.constr (on XP only?) //{ catch not needed - this is already caught as SystemException //} //catch (PlatformNotSupportedException) // thrown (indirectly) by SecurityIdentifier.Translate (on Win95 only?) //{ catch not needed - this is already caught as SystemException //} //catch (UnauthorizedAccessException) // thrown (indirectly) by SecurityIdentifier.Translate //{ catch not needed - this is already caught as SystemException //} } WriteObject(obj); foundRecord = true; } if (!foundRecord && !_inputContainsWildcard) { Exception Ex = new ArgumentException(StringUtil.Format(HotFixResources.NoEntriesFound, computer)); WriteError(new ErrorRecord(Ex, "GetHotFixNoEntriesFound", ErrorCategory.ObjectNotFound, null)); } if (_searchProcess != null) { this.Dispose(); } } }//end of BeginProcessing method
protected override void ProcessRecord() { string hostName; ConnectionOptions connection = ComputerWMIHelper.GetConnection(this.Authentication, this.Impersonation, this.Credential); object[] objArray = new object[2]; objArray[0] = 1; objArray[1] = 0; object[] objArray1 = objArray; if (this._force.IsPresent) { objArray1[0] = 5; } if (!this._asjob.IsPresent) { string empty = string.Empty; string[] strArrays = this._computername; for (int i = 0; i < (int)strArrays.Length; i++) { string str = strArrays[i]; if (str.Equals("localhost", StringComparison.CurrentCultureIgnoreCase) || str.Equals(".", StringComparison.OrdinalIgnoreCase)) { hostName = Dns.GetHostName(); empty = "localhost"; } else { hostName = str; } if (base.ShouldProcess(StringUtil.Format(ComputerResources.DoubleComputerName, empty, hostName))) { try { ManagementScope managementScope = new ManagementScope(ComputerWMIHelper.GetScopeString(str, "\\root\\cimv2"), connection); EnumerationOptions enumerationOption = new EnumerationOptions(); enumerationOption.UseAmendedQualifiers = true; enumerationOption.DirectRead = true; ObjectQuery objectQuery = new ObjectQuery("select * from Win32_OperatingSystem"); this.searcher = new ManagementObjectSearcher(managementScope, objectQuery, enumerationOption); foreach (ManagementObject managementObject in this.searcher.Get()) { object obj = managementObject.InvokeMethod("Win32shutdown", objArray1); int num = Convert.ToInt32(obj.ToString(), CultureInfo.CurrentCulture); if (num == 0) { continue; } ComputerWMIHelper.WriteNonTerminatingError(num, this, hostName); } } catch (ManagementException managementException1) { ManagementException managementException = managementException1; ErrorRecord errorRecord = new ErrorRecord(managementException, "StopComputerException", ErrorCategory.InvalidOperation, hostName); base.WriteError(errorRecord); } catch (COMException cOMException1) { COMException cOMException = cOMException1; ErrorRecord errorRecord1 = new ErrorRecord(cOMException, "StopComputerException", ErrorCategory.InvalidOperation, hostName); base.WriteError(errorRecord1); } } } return; } else { string machineNames = ComputerWMIHelper.GetMachineNames(this.ComputerName); if (base.ShouldProcess(machineNames)) { InvokeWmiMethod invokeWmiMethod = new InvokeWmiMethod(); invokeWmiMethod.Path = "Win32_OperatingSystem=@"; invokeWmiMethod.ComputerName = this._computername; invokeWmiMethod.Authentication = this._authentication; invokeWmiMethod.Impersonation = this._impersonation; invokeWmiMethod.Credential = this._credential; invokeWmiMethod.ThrottleLimit = this._throttlelimit; invokeWmiMethod.Name = "Win32Shutdown"; invokeWmiMethod.EnableAllPrivileges = SwitchParameter.Present; invokeWmiMethod.ArgumentList = objArray1; PSWmiJob pSWmiJob = new PSWmiJob(invokeWmiMethod, this._computername, this._throttlelimit, Job.GetCommandTextFromInvocationInfo(base.MyInvocation)); base.JobRepository.Add(pSWmiJob); base.WriteObject(pSWmiJob); return; } else { return; } } }
/// <summary> /// Get the List of HotFixes installed on the Local Machine. /// </summary> protected override void ProcessRecord() { foreach (string computer in ComputerName) { bool foundRecord = false; StringBuilder queryString = new StringBuilder(); ManagementScope scope = new ManagementScope(ComputerWMIHelper.GetScopeString(computer, ComputerWMIHelper.WMI_Path_CIM), _connectionOptions); scope.Connect(); if (Id != null) { queryString.Append("Select * from Win32_QuickFixEngineering where ("); for (int i = 0; i <= Id.Length - 1; i++) { queryString.Append("HotFixID= '"); queryString.Append(Id[i].Replace("'", "\\'")); queryString.Append("'"); if (i < Id.Length - 1) { queryString.Append(" Or "); } } queryString.Append(")"); } else { queryString.Append("Select * from Win32_QuickFixEngineering"); foundRecord = true; } _searchProcess = new ManagementObjectSearcher(scope, new ObjectQuery(queryString.ToString())); foreach (ManagementObject obj in _searchProcess.Get()) { if (Description != null) { if (!FilterMatch(obj)) { continue; } } else { _inputContainsWildcard = true; } // try to translate the SID to a more friendly username // just stick with the SID if anything goes wrong string installed = (string)obj["InstalledBy"]; if (!string.IsNullOrEmpty(installed)) { try { SecurityIdentifier secObj = new SecurityIdentifier(installed); obj["InstalledBy"] = secObj.Translate(typeof(NTAccount)); } catch (IdentityNotMappedException) { // thrown by SecurityIdentifier.Translate } catch (SystemException) { // thrown by SecurityIdentifier.constr } } WriteObject(obj); foundRecord = true; } if (!foundRecord && !_inputContainsWildcard) { Exception ex = new ArgumentException(StringUtil.Format(HotFixResources.NoEntriesFound, computer)); WriteError(new ErrorRecord(ex, "GetHotFixNoEntriesFound", ErrorCategory.ObjectNotFound, null)); } if (_searchProcess != null) { this.Dispose(); } } }
private void DoAddComputerAction(string computer, string newName, bool isLocalhost, ConnectionOptions options, EnumerationOptions enumOptions, ObjectQuery computerSystemQuery) { string str; string userName; string stringFromSecureString; string userName1; string stringFromSecureString1; int num = 0; bool flag = false; if (isLocalhost) { str = this._shortLocalMachineName; } else { str = computer; } string str1 = str; if (base.ParameterSetName != "Domain") { string str2 = StringUtil.Format(ComputerResources.AddComputerActionWorkgroup, this._workgroupName); if (!base.ShouldProcess(str1, str2)) { return; } } else { string str3 = StringUtil.Format(ComputerResources.AddComputerActionDomain, this._domainName); if (!base.ShouldProcess(str1, str3)) { return; } } if (newName != null && newName.Length > 15) { string str4 = newName.Substring(0, 15); string str5 = StringUtil.Format(ComputerResources.TruncateNetBIOSName, str4); string truncateNetBIOSNameCaption = ComputerResources.TruncateNetBIOSNameCaption; if (!this.Force && !base.ShouldContinue(str5, truncateNetBIOSNameCaption)) { return; } } if (this.LocalCredential != null) { //options.SecurePassword = this.LocalCredential.Password; options.Username = ComputerWMIHelper.GetLocalAdminUserName(str1, this.LocalCredential); } if (isLocalhost) { options.Username = null; //options.SecurePassword = null; } ManagementObjectSearcher managementObjectSearcher = null; ManagementScope managementScope = new ManagementScope(ComputerWMIHelper.GetScopeString(computer, "\\root\\cimv2"), options); using (managementObjectSearcher) { try { managementObjectSearcher = new ManagementObjectSearcher(managementScope, computerSystemQuery, enumOptions); foreach (ManagementObject managementObject in managementObjectSearcher.Get()) { string item = (string)managementObject["DNSHostName"]; if (newName == null && item.Length > 15) { string str6 = item.Substring(0, 15); string str7 = StringUtil.Format(ComputerResources.TruncateNetBIOSName, str6); string truncateNetBIOSNameCaption1 = ComputerResources.TruncateNetBIOSNameCaption; if (!this.Force && !base.ShouldContinue(str7, truncateNetBIOSNameCaption1)) { continue; } } if (newName == null || !item.Equals(newName, StringComparison.OrdinalIgnoreCase)) { if (base.ParameterSetName != "Domain") { if (!(bool)managementObject["PartOfDomain"]) { string str8 = (string)LanguagePrimitives.ConvertTo(managementObject["Domain"], typeof(string), CultureInfo.InvariantCulture); if (!str8.Equals(this._workgroupName, StringComparison.OrdinalIgnoreCase)) { num = this.JoinWorkgroup(managementObject, str1, null); if (num == 0 && newName != null) { num = this.RenameComputer(managementObject, str1, newName); } flag = num == 0; } else { object[] objArray = new object[2]; objArray[0] = str1; objArray[1] = this._workgroupName; this.WriteErrorHelper(ComputerResources.AddComputerToSameWorkgroup, "AddComputerToSameWorkgroup", str1, ErrorCategory.InvalidOperation, false, objArray); continue; } } else { string removeComputerConfirm = ComputerResources.RemoveComputerConfirm; if (!this.Force && !base.ShouldContinue(removeComputerConfirm, null)) { continue; } string str9 = (string)LanguagePrimitives.ConvertTo(managementObject["Domain"], typeof(string), CultureInfo.InvariantCulture); if (this.Credential != null) { userName = this.Credential.UserName; } else { userName = null; } string str10 = userName; if (this.Credential != null) { stringFromSecureString = Utils.GetStringFromSecureString(this.Credential.Password); } else { stringFromSecureString = null; } string str11 = stringFromSecureString; num = this.UnjoinDomain(managementObject, str1, str9, str10, str11); if (num == 0) { num = this.JoinWorkgroup(managementObject, str1, str9); if (num == 0 && newName != null) { num = this.RenameComputer(managementObject, str1, newName); } } flag = num == 0; } } else { if (!(bool)managementObject["PartOfDomain"]) { string str12 = (string)LanguagePrimitives.ConvertTo(managementObject["Domain"], typeof(string), CultureInfo.InvariantCulture); num = this.JoinDomain(managementObject, str1, null, str12); if (num == 0 && newName != null) { num = this.RenameComputer(managementObject, str1, newName); } flag = num == 0; } else { string str13 = (string)LanguagePrimitives.ConvertTo(managementObject["Domain"], typeof(string), CultureInfo.InvariantCulture); string str14 = ""; if (str13.Contains(".")) { int num1 = str13.IndexOf(".", StringComparison.OrdinalIgnoreCase); str14 = str13.Substring(0, num1); } if (str13.Equals(this._domainName, StringComparison.OrdinalIgnoreCase) || str14.Equals(this._domainName, StringComparison.OrdinalIgnoreCase)) { object[] objArray1 = new object[2]; objArray1[0] = str1; objArray1[1] = this._domainName; this.WriteErrorHelper(ComputerResources.AddComputerToSameDomain, "AddComputerToSameDomain", str1, ErrorCategory.InvalidOperation, false, objArray1); continue; } else { PSCredential unjoinDomainCredential = this.UnjoinDomainCredential; PSCredential credential = unjoinDomainCredential; if (unjoinDomainCredential == null) { credential = this.Credential; } PSCredential pSCredential = credential; if (pSCredential != null) { userName1 = pSCredential.UserName; } else { userName1 = null; } string str15 = userName1; if (pSCredential != null) { stringFromSecureString1 = Utils.GetStringFromSecureString(pSCredential.Password); } else { stringFromSecureString1 = null; } string str16 = stringFromSecureString1; num = this.UnjoinDomain(managementObject, str1, str13, str15, str16); if (num == 0) { num = this.JoinDomain(managementObject, str1, str13, null); if (num == 0 && newName != null) { num = this.RenameComputer(managementObject, str1, newName); } } flag = num == 0; } } } if (!this._passThru) { continue; } base.WriteObject(ComputerWMIHelper.GetComputerStatusObject(num, str1)); } else { object[] objArray2 = new object[2]; objArray2[0] = str1; objArray2[1] = newName; this.WriteErrorHelper(ComputerResources.NewNameIsOldName, "NewNameIsOldName", newName, ErrorCategory.InvalidArgument, false, objArray2); } } if (flag && this._restart) { object[] objArray3 = new object[2]; objArray3[0] = 6; objArray3[1] = 0; object[] objArray4 = objArray3; RestartComputerCommand.RestartOneComputerUsingDcom(this, isLocalhost, str1, objArray4, options); } if (flag && !this._restart) { base.WriteWarning(StringUtil.Format(ComputerResources.RestartNeeded, null, str1)); } } catch (ManagementException managementException1) { ManagementException managementException = managementException1; object[] message = new object[2]; message[0] = str1; message[1] = managementException.Message; this.WriteErrorHelper(ComputerResources.FailToConnectToComputer, "AddComputerException", str1, ErrorCategory.OperationStopped, false, message); } catch (COMException cOMException1) { COMException cOMException = cOMException1; object[] message1 = new object[2]; message1[0] = str1; message1[1] = cOMException.Message; this.WriteErrorHelper(ComputerResources.FailToConnectToComputer, "AddComputerException", str1, ErrorCategory.OperationStopped, false, message1); } catch (UnauthorizedAccessException unauthorizedAccessException1) { UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1; object[] message2 = new object[2]; message2[0] = str1; message2[1] = unauthorizedAccessException.Message; this.WriteErrorHelper(ComputerResources.FailToConnectToComputer, "AddComputerException", str1, ErrorCategory.OperationStopped, false, message2); } } }
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); } } }
protected override void BeginProcessing() { bool flag = false; string[] strArrays = this._computername; for (int i = 0; i < (int)strArrays.Length; i++) { string str = strArrays[i]; StringBuilder stringBuilder = new StringBuilder(); ConnectionOptions connection = ComputerWMIHelper.GetConnection(AuthenticationLevel.Packet, ImpersonationLevel.Impersonate, this.Credential); ManagementScope managementScope = new ManagementScope(ComputerWMIHelper.GetScopeString(str, "\\root\\cimv2"), connection); managementScope.Connect(); if (this._id == null) { stringBuilder.Append("Select * from Win32_QuickFixEngineering"); flag = true; } else { stringBuilder.Append("Select * from Win32_QuickFixEngineering where ("); for (int j = 0; j <= (int)this._id.Length - 1; j++) { stringBuilder.Append("HotFixID= '"); stringBuilder.Append(this._id[j].ToString().Replace("'", "\\'")); stringBuilder.Append("'"); if (j < (int)this._id.Length - 1) { stringBuilder.Append(" Or "); } } stringBuilder.Append(")"); } this.searchProcess = new ManagementObjectSearcher(managementScope, new ObjectQuery(stringBuilder.ToString())); foreach (ManagementObject managementObject in this.searchProcess.Get()) { if (this._description == null) { this.inputContainsWildcard = true; } else { if (!this.FilterMatch(managementObject)) { continue; } } string item = (string)managementObject["InstalledBy"]; if (!string.IsNullOrEmpty(item)) { try { SecurityIdentifier securityIdentifier = new SecurityIdentifier(item); managementObject["InstalledBy"] = securityIdentifier.Translate(typeof(NTAccount)); } catch (IdentityNotMappedException identityNotMappedException) { } catch (SystemException systemException1) { SystemException systemException = systemException1; CommandsCommon.CheckForSevereException(this, systemException); } } base.WriteObject(managementObject); flag = true; } if (!flag && !this.inputContainsWildcard) { Exception argumentException = new ArgumentException(StringUtil.Format(HotFixResources.NoEntriesFound, str)); base.WriteError(new ErrorRecord(argumentException, "GetHotFixNoEntriesFound", ErrorCategory.ObjectNotFound, null)); } if (this.searchProcess != null) { this.Dispose(); } } }
private void DoRemoveComputerAction(string computer, bool isLocalhost, ConnectionOptions options, EnumerationOptions enumOptions, ObjectQuery computerSystemQuery) { string str; string userName; string stringFromSecureString; bool flag = false; if (isLocalhost) { str = this._shortLocalMachineName; } else { str = computer; } string str1 = str; if (base.ShouldProcess(str1)) { if (this.LocalCredential != null) { //options.SecurePassword = this.LocalCredential.Password; options.Username = ComputerWMIHelper.GetLocalAdminUserName(str1, this.LocalCredential); } if (isLocalhost) { options.Username = null; //options.SecurePassword = null; } ManagementObjectSearcher managementObjectSearcher = null; ManagementScope managementScope = new ManagementScope(ComputerWMIHelper.GetScopeString(computer, "\\root\\cimv2"), options); using (managementObjectSearcher) { try { managementObjectSearcher = new ManagementObjectSearcher(managementScope, computerSystemQuery, enumOptions); foreach (ManagementObject managementObject in managementObjectSearcher.Get()) { if ((bool)managementObject["PartOfDomain"]) { string removeComputerConfirm = ComputerResources.RemoveComputerConfirm; if (!this.Force && !base.ShouldContinue(removeComputerConfirm, null)) { continue; } string str2 = (string)LanguagePrimitives.ConvertTo(managementObject["Domain"], typeof(string), CultureInfo.InvariantCulture); if (this.UnjoinDomainCredential != null) { userName = this.UnjoinDomainCredential.UserName; } else { userName = null; } string str3 = userName; if (this.UnjoinDomainCredential != null) { stringFromSecureString = Utils.GetStringFromSecureString(this.UnjoinDomainCredential.Password); } else { stringFromSecureString = null; } string str4 = stringFromSecureString; ManagementBaseObject methodParameters = managementObject.GetMethodParameters("UnjoinDomainOrWorkgroup"); methodParameters.SetPropertyValue("UserName", str3); methodParameters.SetPropertyValue("Password", str4); methodParameters.SetPropertyValue("FUnjoinOptions", 4); ManagementBaseObject managementBaseObject = managementObject.InvokeMethod("UnjoinDomainOrWorkgroup", methodParameters, null); int num = Convert.ToInt32(managementBaseObject["ReturnValue"], CultureInfo.CurrentCulture); if ((num == 0x54b || num == 53) && this.Force) { methodParameters.SetPropertyValue("FUnjoinOptions", 0); managementBaseObject = managementObject.InvokeMethod("UnjoinDomainOrWorkgroup", methodParameters, null); num = Convert.ToInt32(managementBaseObject["ReturnValue"], CultureInfo.CurrentCulture); } if (num == 0) { flag = true; if (this._workGroup != null) { ManagementBaseObject methodParameters1 = managementObject.GetMethodParameters("JoinDomainOrWorkgroup"); methodParameters1.SetPropertyValue("Name", this._workGroup); methodParameters1.SetPropertyValue("Password", null); methodParameters1.SetPropertyValue("UserName", null); methodParameters1.SetPropertyValue("FJoinOptions", 0); managementBaseObject = managementObject.InvokeMethod("JoinDomainOrWorkgroup", methodParameters1, null); num = Convert.ToInt32(managementBaseObject["ReturnValue"], CultureInfo.CurrentCulture); if (num != 0) { Win32Exception win32Exception = new Win32Exception(num); object[] message = new object[4]; message[0] = str1; message[1] = str2; message[2] = this._workGroup; message[3] = win32Exception.Message; string str5 = StringUtil.Format(ComputerResources.FailToSwitchFromDomainToWorkgroup, message); ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str5), "FailToJoinWorkGroup", ErrorCategory.OperationStopped, str1); base.WriteError(errorRecord); } } } else { Win32Exception win32Exception1 = new Win32Exception(num); object[] objArray = new object[3]; objArray[0] = str1; objArray[1] = str2; objArray[2] = win32Exception1.Message; string str6 = StringUtil.Format(ComputerResources.FailToUnjoinDomain, objArray); ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(str6), "FailToUnjoinDomain", ErrorCategory.OperationStopped, str1); base.WriteError(errorRecord1); } if (!this._passThru) { continue; } base.WriteObject(ComputerWMIHelper.GetComputerStatusObject(num, str1)); } else { string str7 = StringUtil.Format(ComputerResources.ComputerNotInDomain, str1); ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(str7), "ComputerNotInDomain", ErrorCategory.InvalidOperation, str1); base.WriteError(errorRecord2); } } if (flag && this._restart) { object[] objArray1 = new object[2]; objArray1[0] = 6; objArray1[1] = 0; object[] objArray2 = objArray1; RestartComputerCommand.RestartOneComputerUsingDcom(this, isLocalhost, str1, objArray2, options); } if (flag && !this._restart) { base.WriteWarning(StringUtil.Format(ComputerResources.RestartNeeded, null, str1)); } } catch (ManagementException managementException1) { ManagementException managementException = managementException1; string str8 = StringUtil.Format(ComputerResources.FailToConnectToComputer, str1, managementException.Message); ErrorRecord errorRecord3 = new ErrorRecord(new InvalidOperationException(str8), "RemoveComputerException", ErrorCategory.OperationStopped, str1); base.WriteError(errorRecord3); } catch (COMException cOMException1) { COMException cOMException = cOMException1; string str9 = StringUtil.Format(ComputerResources.FailToConnectToComputer, str1, cOMException.Message); ErrorRecord errorRecord4 = new ErrorRecord(new InvalidOperationException(str9), "RemoveComputerException", ErrorCategory.OperationStopped, str1); base.WriteError(errorRecord4); } catch (UnauthorizedAccessException unauthorizedAccessException1) { UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1; string str10 = StringUtil.Format(ComputerResources.FailToConnectToComputer, str1, unauthorizedAccessException.Message); ErrorRecord errorRecord5 = new ErrorRecord(new InvalidOperationException(str10), "RemoveComputerException", ErrorCategory.OperationStopped, str1); base.WriteError(errorRecord5); } } return; } else { return; } }
private void DoRenameComputerAction(string computer, string newName, bool isLocalhost) { string str; string userName; string stringFromSecureString; EnumerationOptions enumerationOption = new EnumerationOptions(); enumerationOption.UseAmendedQualifiers = true; enumerationOption.DirectRead = true; EnumerationOptions enumerationOption1 = enumerationOption; ObjectQuery objectQuery = new ObjectQuery("select * from Win32_ComputerSystem"); bool flag = false; if (isLocalhost) { str = this._shortLocalMachineName; } else { str = computer; } string str1 = str; if (base.ShouldProcess(str1)) { if (newName != null && newName.Length > 15) { string str2 = newName.Substring(0, 15); string str3 = StringUtil.Format(ComputerResources.TruncateNetBIOSName, str2); string truncateNetBIOSNameCaption = ComputerResources.TruncateNetBIOSNameCaption; if (!this.Force && !base.ShouldContinue(str3, truncateNetBIOSNameCaption)) { return; } } ConnectionOptions connectionOption = new ConnectionOptions(); connectionOption.Authentication = AuthenticationLevel.PacketPrivacy; connectionOption.Impersonation = ImpersonationLevel.Impersonate; connectionOption.EnablePrivileges = true; ConnectionOptions password = connectionOption; if (!isLocalhost) { if (this.LocalCredential == null) { if (this.DomainCredential != null) { //password.SecurePassword = this.DomainCredential.Password; password.Username = this.DomainCredential.UserName; } } else { //password.SecurePassword = this.LocalCredential.Password; password.Username = ComputerWMIHelper.GetLocalAdminUserName(str1, this.LocalCredential); } } else { password.Username = null; //password.SecurePassword = null; } ManagementScope managementScope = new ManagementScope(ComputerWMIHelper.GetScopeString(computer, "\\root\\cimv2"), password); try { try { this._searcher = new ManagementObjectSearcher(managementScope, objectQuery, enumerationOption1); foreach (ManagementObject managementObject in this._searcher.Get()) { string item = (string)managementObject["DNSHostName"]; if (!item.Equals(newName, StringComparison.OrdinalIgnoreCase)) { string str4 = null; string str5 = null; if ((bool)managementObject["PartOfDomain"]) { if (this.DomainCredential != null) { userName = this.DomainCredential.UserName; } else { userName = null; } str4 = userName; if (this.DomainCredential != null) { stringFromSecureString = Utils.GetStringFromSecureString(this.DomainCredential.Password); } else { stringFromSecureString = null; } str5 = stringFromSecureString; } ManagementBaseObject methodParameters = managementObject.GetMethodParameters("Rename"); methodParameters.SetPropertyValue("Name", newName); methodParameters.SetPropertyValue("UserName", str4); methodParameters.SetPropertyValue("Password", str5); ManagementBaseObject managementBaseObject = managementObject.InvokeMethod("Rename", methodParameters, null); int num = Convert.ToInt32(managementBaseObject["ReturnValue"], CultureInfo.CurrentCulture); if (num == 0) { flag = true; } else { Win32Exception win32Exception = new Win32Exception(num); object[] message = new object[3]; message[0] = str1; message[1] = newName; message[2] = win32Exception.Message; string str6 = StringUtil.Format(ComputerResources.FailToRename, message); ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str6), "FailToRenameComputer", ErrorCategory.OperationStopped, str1); base.WriteError(errorRecord); } if (!this._passThru) { continue; } base.WriteObject(ComputerWMIHelper.GetRenameComputerStatusObject(num, newName, str1)); } else { string str7 = StringUtil.Format(ComputerResources.NewNameIsOldName, str1, newName); ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(str7), "NewNameIsOldName", ErrorCategory.InvalidArgument, newName); base.WriteError(errorRecord1); } } if (flag && this._restart) { object[] objArray = new object[2]; objArray[0] = 6; objArray[1] = 0; object[] objArray1 = objArray; RestartComputerCommand.RestartOneComputerUsingDcom(this, isLocalhost, str1, objArray1, password); } if (flag && !this._restart) { base.WriteWarning(StringUtil.Format(ComputerResources.RestartNeeded, null, str1)); } } catch (ManagementException managementException1) { ManagementException managementException = managementException1; string str8 = StringUtil.Format(ComputerResources.FailToConnectToComputer, str1, managementException.Message); ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(str8), "RenameComputerException", ErrorCategory.OperationStopped, str1); base.WriteError(errorRecord2); } catch (COMException cOMException1) { COMException cOMException = cOMException1; string str9 = StringUtil.Format(ComputerResources.FailToConnectToComputer, str1, cOMException.Message); ErrorRecord errorRecord3 = new ErrorRecord(new InvalidOperationException(str9), "RenameComputerException", ErrorCategory.OperationStopped, str1); base.WriteError(errorRecord3); } catch (UnauthorizedAccessException unauthorizedAccessException1) { UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1; string str10 = StringUtil.Format(ComputerResources.FailToConnectToComputer, str1, unauthorizedAccessException.Message); ErrorRecord errorRecord4 = new ErrorRecord(new InvalidOperationException(str10), "RenameComputerException", ErrorCategory.OperationStopped, str1); base.WriteError(errorRecord4); } } finally { this._searcher.Dispose(); } return; } else { return; } }