Exemple #1
0
        protected override void ProcessRecord()
        {
            string    hostName  = Dns.GetHostName();
            string    str       = null;
            Exception exception = null;

            if (base.ShouldProcess(hostName))
            {
                try
                {
                    ManagementObject managementObject = new ManagementObject(string.Concat("Win32_ComputerSystem.Name=\"", hostName, "\""));
                    if (!(bool)managementObject["PartOfDomain"])
                    {
                        string      resetComputerNotInDomain = ComputerResources.ResetComputerNotInDomain;
                        ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(resetComputerNotInDomain), "ComputerNotInDomain", ErrorCategory.InvalidOperation, hostName);
                        base.ThrowTerminatingError(errorRecord);
                    }
                    str = (string)LanguagePrimitives.ConvertTo(managementObject["Domain"], typeof(string), CultureInfo.InvariantCulture);
                }
                catch (ManagementException managementException1)
                {
                    ManagementException managementException = managementException1;
                    exception = managementException;
                }
                catch (COMException cOMException1)
                {
                    COMException cOMException = cOMException1;
                    exception = cOMException;
                }
                catch (UnauthorizedAccessException unauthorizedAccessException1)
                {
                    UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1;
                    exception = unauthorizedAccessException;
                }
                if (exception != null)
                {
                    string      str1         = StringUtil.Format(ComputerResources.FailToGetDomainInformation, exception.Message);
                    ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(str1), "FailToGetDomainInformation", ErrorCategory.OperationStopped, hostName);
                    base.ThrowTerminatingError(errorRecord1);
                }
                ResetComputerMachinePasswordCommand.ResetMachineAccountPassword(str, hostName, this.Server, this.Credential, this);
                return;
            }
            else
            {
                return;
            }
        }
Exemple #2
0
        internal static void ResetMachineAccountPassword(string domain, string localMachineName, string server, PSCredential credential, PSCmdlet cmdlet)
        {
            SAMAPI.LSA_UNICODE_STRING structure;
            string            userName;
            string            stringFromSecureString;
            string            cannotFindMachineAccountFromServer;
            DirectoryEntry    directoryEntry    = null;
            DirectoryEntry    directoryEntry1   = null;
            DirectorySearcher directorySearcher = null;
            string            randomPassword    = null;
            string            str  = server;
            string            str1 = str;

            if (str == null)
            {
                str1 = domain;
            }
            string str2 = str1;

            try
            {
                try
                {
                    if (credential != null)
                    {
                        userName = credential.UserName;
                    }
                    else
                    {
                        userName = null;
                    }
                    string str3 = userName;
                    if (credential != null)
                    {
                        stringFromSecureString = Utils.GetStringFromSecureString(credential.Password);
                    }
                    else
                    {
                        stringFromSecureString = null;
                    }
                    string str4 = stringFromSecureString;
                    directoryEntry    = new DirectoryEntry(string.Concat("LDAP://", str2), str3, str4, AuthenticationTypes.Secure);
                    directorySearcher = new DirectorySearcher(directoryEntry);
                    string[] strArrays = new string[5];
                    strArrays[0]             = "(&(objectClass=computer)(|(cn=";
                    strArrays[1]             = localMachineName;
                    strArrays[2]             = ")(dn=";
                    strArrays[3]             = localMachineName;
                    strArrays[4]             = ")))";
                    directorySearcher.Filter = string.Concat(strArrays);
                    SearchResult searchResult = directorySearcher.FindOne();
                    if (searchResult != null)
                    {
                        directoryEntry1 = searchResult.GetDirectoryEntry();
                        randomPassword  = ComputerWMIHelper.GetRandomPassword(120);
                        object[] objArray = new object[1];
                        objArray[0] = randomPassword;
                        directoryEntry1.Invoke("SetPassword", objArray);
                        directoryEntry1.Properties["LockOutTime"].Value = 0;
                    }
                    else
                    {
                        if (server != null)
                        {
                            cannotFindMachineAccountFromServer = ComputerResources.CannotFindMachineAccountFromServer;
                        }
                        else
                        {
                            cannotFindMachineAccountFromServer = ComputerResources.CannotFindMachineAccountFromDomain;
                        }
                        string      str5        = cannotFindMachineAccountFromServer;
                        string      str6        = StringUtil.Format(str5, str2);
                        ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str6), "CannotFindMachineAccount", ErrorCategory.OperationStopped, localMachineName);
                        cmdlet.ThrowTerminatingError(errorRecord);
                    }
                }
#if !MONO
                catch (DirectoryServicesCOMException directoryServicesCOMException1)
                {
                    DirectoryServicesCOMException directoryServicesCOMException = directoryServicesCOMException1;
                    string      str7         = StringUtil.Format(ComputerResources.FailToResetPasswordOnDomain, directoryServicesCOMException.Message);
                    ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(str7), "FailToResetPasswordOnDomain", ErrorCategory.OperationStopped, localMachineName);
                    cmdlet.ThrowTerminatingError(errorRecord1);
                }
#endif
                catch (TargetInvocationException targetInvocationException1)
                {
                    TargetInvocationException targetInvocationException = targetInvocationException1;
                    string      str8         = StringUtil.Format(ComputerResources.FailToResetPasswordOnDomain, targetInvocationException.InnerException.Message);
                    ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(str8), "FailToResetPasswordOnDomain", ErrorCategory.OperationStopped, localMachineName);
                    cmdlet.ThrowTerminatingError(errorRecord2);
                }
                catch (COMException cOMException1)
                {
                    COMException cOMException = cOMException1;
                    string       str9         = StringUtil.Format(ComputerResources.FailToResetPasswordOnDomain, cOMException.Message);
                    ErrorRecord  errorRecord3 = new ErrorRecord(new InvalidOperationException(str9), "FailToResetPasswordOnDomain", ErrorCategory.OperationStopped, localMachineName);
                    cmdlet.ThrowTerminatingError(errorRecord3);
                }
            }
            finally
            {
                if (directoryEntry != null)
                {
                    directoryEntry.Close();
                    directoryEntry.Dispose();
                }
                if (directorySearcher != null)
                {
                    directorySearcher.Dispose();
                }
                if (directoryEntry1 != null)
                {
                    directoryEntry1.Close();
                    directoryEntry1.Dispose();
                }
            }
            SAMAPI.LSA_OBJECT_ATTRIBUTES zero = new SAMAPI.LSA_OBJECT_ATTRIBUTES();
            zero.RootDirectory            = IntPtr.Zero;
            zero.ObjectName               = IntPtr.Zero;
            zero.Attributes               = 0;
            zero.SecurityDescriptor       = IntPtr.Zero;
            zero.SecurityQualityOfService = IntPtr.Zero;
            zero.Length = Marshal.SizeOf(typeof(SAMAPI.LSA_OBJECT_ATTRIBUTES));
            IntPtr intPtr  = IntPtr.Zero;
            IntPtr zero1   = IntPtr.Zero;
            IntPtr intPtr1 = IntPtr.Zero;
            SAMAPI.LSA_UNICODE_STRING lSAUNICODESTRING = new SAMAPI.LSA_UNICODE_STRING();
            lSAUNICODESTRING.Buffer = IntPtr.Zero;
            SAMAPI.LSA_UNICODE_STRING lSAUNICODESTRING1 = lSAUNICODESTRING;
            SAMAPI.LSA_UNICODE_STRING zero2             = new SAMAPI.LSA_UNICODE_STRING();
            zero2.Buffer = IntPtr.Zero;
            SAMAPI.LSA_UNICODE_STRING lSAUNICODESTRING2 = zero2;
            SAMAPI.LSA_UNICODE_STRING zero3             = new SAMAPI.LSA_UNICODE_STRING();
            zero3.Buffer        = IntPtr.Zero;
            zero3.Length        = 0;
            zero3.MaximumLength = 0;
            try
            {
                uint num = SAMAPI.LsaOpenPolicy(ref zero3, ref zero, 0xf0fff, out intPtr);
                if (num == -1073741790)
                {
                    string      needAdminPrivilegeToResetPassword = ComputerResources.NeedAdminPrivilegeToResetPassword;
                    ErrorRecord errorRecord4 = new ErrorRecord(new InvalidOperationException(needAdminPrivilegeToResetPassword), "UnauthorizedAccessException", ErrorCategory.InvalidOperation, localMachineName);
                    cmdlet.ThrowTerminatingError(errorRecord4);
                }
                if (num != 0)
                {
                    ResetComputerMachinePasswordCommand.ThrowOutLsaError(num, cmdlet);
                }
                SAMAPI.InitLsaString("$MACHINE.ACC", ref lSAUNICODESTRING1);
                SAMAPI.InitLsaString(randomPassword, ref lSAUNICODESTRING2);
                bool flag = false;
                num = SAMAPI.LsaOpenSecret(intPtr, ref lSAUNICODESTRING1, 3, out zero1);
                if (num == -1073741772)
                {
                    num  = SAMAPI.LsaCreateSecret(intPtr, ref lSAUNICODESTRING1, 1, out zero1);
                    flag = true;
                }
                if (num != 0)
                {
                    ResetComputerMachinePasswordCommand.ThrowOutLsaError(num, cmdlet);
                }
                if (!flag)
                {
                    num = SAMAPI.LsaQuerySecret(zero1, out intPtr1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                    if (num != 0)
                    {
                        ResetComputerMachinePasswordCommand.ThrowOutLsaError(num, cmdlet);
                    }
                    structure = (SAMAPI.LSA_UNICODE_STRING)Marshal.PtrToStructure(intPtr1, typeof(SAMAPI.LSA_UNICODE_STRING));
                }
                else
                {
                    structure = lSAUNICODESTRING2;
                }
                num = SAMAPI.LsaSetSecret(zero1, ref lSAUNICODESTRING2, ref structure);
                if (num != 0)
                {
                    ResetComputerMachinePasswordCommand.ThrowOutLsaError(num, cmdlet);
                }
            }
            finally
            {
                if (intPtr1 != IntPtr.Zero)
                {
                    SAMAPI.LsaFreeMemory(intPtr1);
                }
                if (intPtr != IntPtr.Zero)
                {
                    SAMAPI.LsaClose(intPtr);
                }
                if (zero1 != IntPtr.Zero)
                {
                    SAMAPI.LsaClose(zero1);
                }
                SAMAPI.FreeLsaString(ref lSAUNICODESTRING1);
                SAMAPI.FreeLsaString(ref lSAUNICODESTRING2);
            }
        }
Exemple #3
0
        protected override void ProcessRecord()
        {
            bool      flag;
            string    str;
            string    str1;
            string    str2;
            string    hostName  = Dns.GetHostName();
            string    str3      = null;
            Exception exception = null;

            if (base.ShouldProcess(hostName))
            {
                try
                {
                    ManagementObject managementObject = new ManagementObject(string.Concat("Win32_ComputerSystem.Name=\"", hostName, "\""));
                    if (!(bool)managementObject["PartOfDomain"])
                    {
                        string      testComputerNotInDomain = ComputerResources.TestComputerNotInDomain;
                        ErrorRecord errorRecord             = new ErrorRecord(new InvalidOperationException(testComputerNotInDomain), "ComputerNotInDomain", ErrorCategory.InvalidOperation, hostName);
                        base.ThrowTerminatingError(errorRecord);
                    }
                    str3 = (string)LanguagePrimitives.ConvertTo(managementObject["Domain"], typeof(string), CultureInfo.InvariantCulture);
                }
                catch (ManagementException managementException1)
                {
                    ManagementException managementException = managementException1;
                    exception = managementException;
                }
                catch (COMException cOMException1)
                {
                    COMException cOMException = cOMException1;
                    exception = cOMException;
                }
                catch (UnauthorizedAccessException unauthorizedAccessException1)
                {
                    UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1;
                    exception = unauthorizedAccessException;
                }
                if (exception != null)
                {
                    string      str4         = StringUtil.Format(ComputerResources.FailToGetDomainInformation, exception.Message);
                    ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(str4), "FailToGetDomainInformation", ErrorCategory.OperationStopped, hostName);
                    base.ThrowTerminatingError(errorRecord1);
                }
                if (!this.Repair)
                {
                    flag = this.VerifySecureChannel(str3, hostName);
                    if (flag)
                    {
                        str1 = StringUtil.Format(ComputerResources.SecureChannelAlive, str3);
                    }
                    else
                    {
                        str1 = StringUtil.Format(ComputerResources.SecureChannelBroken, str3);
                    }
                    str = str1;
                }
                else
                {
                    ResetComputerMachinePasswordCommand.ResetMachineAccountPassword(str3, hostName, this.Server, this.Credential, this);
                    flag = this.ResetSecureChannel(str3);
                    if (flag)
                    {
                        str2 = StringUtil.Format(ComputerResources.RepairSecureChannelSucceed, str3);
                    }
                    else
                    {
                        str2 = StringUtil.Format(ComputerResources.RepairSecureChannelFail, str3);
                    }
                    str = str2;
                }
                base.WriteObject(flag);
                base.WriteVerbose(str);
                return;
            }
            else
            {
                return;
            }
        }