Exemplo n.º 1
0
        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;
            }
        }
Exemplo n.º 2
0
        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);
                }
            }
        }
Exemplo n.º 3
0
        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;
            }
        }