Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using PasswordResetter.DTOs;
- using PasswordResetter.Types;
- using System.Management.Automation;
- using System.Collections.ObjectModel;
- using System.Management.Automation.Runspaces;
- using System.Management;
- using System.Configuration;
- using System.Linq;
- namespace PasswordResetter.Services
- {
- public static class ResetCredentials
- {
- public static bool SetPassword(AccountReference accountReference, string password)
- {
- switch (accountReference.ReferenceType)
- {
- case ReferenceType.WindowsService:
- {
- return SetWindowsServicePassword(accountReference, password);
- }
- case ReferenceType.ScheduledTask:
- {
- return SetScheduledTaskPassword(accountReference, password);
- }
- case ReferenceType.IIS:
- {
- return SetIISPassword(accountReference, password);
- }
- default:
- {
- Logging.Log(string.Format("SetPassword(..) Reference Type Unsupported: {0}", accountReference.ReferenceType.ToString()), LogType.WARNING);
- return false;
- }
- }
- }
- private static bool SetWindowsServicePassword(AccountReference accountReference, string password)
- {
- Logging.Log(string.Format("Setting Password for {0}", accountReference.ReferenceName), LogType.INFO);
- ConnectionOptions connection = new ConnectionOptions();
- var adminDomain = ConfigurationManager.AppSettings["AdminDomain"];
- var adminUser = ConfigurationManager.AppSettings["AdminAccount"];
- var adminPassword = ConfigurationManager.AppSettings["AdminPassword"];
- connection.Username = string.Format("{0}\\{1}", adminDomain, adminUser);
- connection.Password = adminPassword;
- connection.EnablePrivileges = true;
- connection.Authentication = AuthenticationLevel.Default;
- connection.Impersonation = ImpersonationLevel.Impersonate;
- ManagementScope scope = new ManagementScope(string.Format("\\\\{0}\\root\\CIMV2", accountReference.Server), connection);
- scope.Connect();
- ObjectQuery query = new ObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", accountReference.ReferenceName));
- ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
- var res = searcher.Get();
- foreach (ManagementObject queryObj in res)
- {
- object[] wmiParams = new object[10];
- wmiParams[6] = accountReference.AccountName;
- wmiParams[7] = password;
- queryObj.InvokeMethod("Change", wmiParams);
- }
- return true;
- }
- private static bool SetScheduledTaskPassword(AccountReference accountReference, string password)
- {
- Logging.Log(string.Format("Setting Password for {0}", accountReference.ReferenceName), LogType.INFO);
- var errors = false;
- var connectioninfo = new WSManConnectionInfo
- {
- ComputerName = accountReference.Server
- };
- Runspace runspace = RunspaceFactory.CreateRunspace(connectioninfo);
- runspace.Open();
- using (PowerShell powerShellInstance = PowerShell.Create())
- {
- powerShellInstance.Runspace = runspace;
- powerShellInstance.AddScript(string.Format("Set-ScheduledTask '{0}' -User '{1}' -Password '{2}'", accountReference.ReferenceName, accountReference.AccountName, password));
- var result = powerShellInstance.Invoke();
- if (powerShellInstance.Streams.Error.Count > 0)
- {
- errors = true;
- Logging.Log(string.Format("Error(s) Occurred While Setting Password for {0} : {1}", accountReference.ReferenceName, string.Join(",", powerShellInstance.Streams.Error.Select(e => e.Exception.Message))), LogType.ERROR);
- }
- }
- runspace.Close();
- return !errors;
- }
- private static bool SetIISPassword(AccountReference accountReference, string password)
- {
- Logging.Log(string.Format("Setting Password for {0}", accountReference.ReferenceName), LogType.INFO);
- var errors = false;
- var connectioninfo = new WSManConnectionInfo
- {
- ComputerName = accountReference.Server
- };
- Runspace runspace = RunspaceFactory.CreateRunspace(connectioninfo);
- runspace.Open();
- using (PowerShell powerShellInstance = PowerShell.Create())
- {
- powerShellInstance.Runspace = runspace;
- powerShellInstance.AddScript("Import-Module WebAdministration");
- powerShellInstance.AddScript(string.Format("Set-ItemProperty IIS:\\AppPools\\{0} -name processModel.identityType -Value 3", accountReference.ReferenceName));
- powerShellInstance.AddScript(string.Format("Set-ItemProperty IIS:\\AppPools\\{0} -name processModel.userName -Value {1}", accountReference.ReferenceName, accountReference.AccountName));
- powerShellInstance.AddScript(string.Format("Set-ItemProperty IIS:\\AppPools\\{0} -name processModel.password -Value {1}", accountReference.ReferenceName, password));
- var result = powerShellInstance.Invoke();
- if (powerShellInstance.Streams.Error.Count > 0)
- {
- errors = true;
- Logging.Log(string.Format("Error(s) Occurred While Setting Password for {0} : {1}", accountReference.ReferenceName, string.Join(",", powerShellInstance.Streams.Error.Select(e => e.Exception.Message))), LogType.ERROR);
- }
- }
- runspace.Close();
- return !errors;
- }
- }
- }
Add Comment
Please, Sign In to add comment