//Remove the host from active directory private void UnRegisterComputer(Response response) { Log.Entry(Name, "Removing host from active directory"); if (!response.IsFieldValid("#ADUser") || ! response.IsFieldValid("#ADPass")) { Log.Error(Name, "Required Domain information is missing"); return; } try { var returnCode = NetUnjoinDomain(null, response.GetField("#ADUser"), response.GetField("#ADPass"), UnJoinOptions.NetsetupAccountDelete); Log.Entry(Name, string.Format("{0} {1}", (_returnCodes.ContainsKey(returnCode) ? string.Format("{0}, code = ", _returnCodes[returnCode]) : "Unknown Return Code: "), returnCode)); if (returnCode.Equals(0)) Power.Restart("Host joined to active directory, restart needed", Power.FormOption.Delay); } catch (Exception ex) { Log.Error(Name, ex); } }
//Rename the computer and remove it from active directory private void RenameComputer(Response response) { Log.Entry(Name, "Checking Hostname"); if (!response.IsFieldValid("#hostname")) { Log.Error(Name, "Hostname is not specified"); return; } if (Environment.MachineName.ToLower().Equals(response.GetField("#hostname").ToLower())) { Log.Entry(Name, "Hostname is correct"); return; } Log.Entry(Name, string.Format("Renaming host to {0}", response.GetField("#hostname"))); if (!UserHandler.IsUserLoggedIn() || response.GetField("#force").Equals("1")) { Log.Entry(Name, "Unregistering computer"); //First unjoin it from active directory UnRegisterComputer(response); Log.Entry(Name, "Updating registry"); RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters","NV Hostname", response.GetField("#hostname")); RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName", "ComputerName", response.GetField("#hostname")); RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName", response.GetField("#hostname")); Power.Restart(RegistryHandler.GetSystemSetting("Company") + " needs to rename your computer", Power.FormOption.Delay); } else if(!_notifiedUser) { Log.Entry(Name, "User is currently logged in, will try again later"); var notification = new Notification("Please log off", string.Format( "{0} is attemping to service your computer, please log off at the soonest available time", RegistryHandler.GetSystemSetting("Company")), 120); Bus.Emit(Bus.Channel.Notification, notification.GetJson(), true); _notifiedUser = true; } }
//Add a host to active directory private void RegisterComputer(Response response) { Log.Entry(Name, "Registering host with active directory"); if (response.GetField("#AD") != "1") return; if (!response.IsFieldValid("#ADDom") || !response.IsFieldValid("#ADUser") || !response.IsFieldValid("#ADPass")) { Log.Error(Name, "Required Domain Joining information is missing"); return; } // Attempt to join the domain var returnCode = DomainWrapper(response, true, (JoinOptions.NetsetupJoinDomain | JoinOptions.NetsetupAcctCreate)); if (returnCode == 2224) returnCode = DomainWrapper(response, true, JoinOptions.NetsetupJoinDomain); else if (returnCode == 2 || returnCode == 50) returnCode = DomainWrapper(response, false, (JoinOptions.NetsetupJoinDomain | JoinOptions.NetsetupAcctCreate)); // Entry the results Log.Entry(Name, string.Format("{0} {1}", (_returnCodes.ContainsKey(returnCode) ? string.Format("{0}, code = ", _returnCodes[returnCode]) : "Unknown Return Code: "), returnCode)); if (returnCode.Equals(0)) Power.Restart("Host joined to Active Directory, restart required", Power.FormOption.Delay); }
//Active a computer with a product key private void ActivateComputer(Response response) { Log.Entry(Name, "Activing host with product key"); if (!response.IsFieldValid("#Key")) return; if (response.GetField("#Key").Length != 29) { Log.Error(Name, "Invalid product key"); return; } try { using(var process = new Process { StartInfo = { FileName = @"cscript", Arguments = string.Format("//B //Nologo {0}\\slmgr.vbs /ipk {1}", Environment.SystemDirectory, response.GetField("#Key")), WindowStyle = ProcessWindowStyle.Hidden } }) { //Give windows the new key process.Start(); process.WaitForExit(); process.Close(); //Try and activate the new key process.StartInfo.Arguments = string.Format("//B //Nologo {0}\\slmgr.vbs /ato", Environment.SystemDirectory); process.Start(); process.WaitForExit(); process.Close(); } } catch (Exception ex) { Log.Error(Name, ex); } }
//Rename the computer and remove it from active directory private void RenameComputer(Response response) { Log.Entry(Name, "Checking Hostname"); if (!response.IsFieldValid("#hostname")) { Log.Error(Name, "Hostname is not specified"); return; } if (Environment.MachineName.ToLower().Equals(response.GetField("#hostname").ToLower())) { Log.Entry(Name, "Hostname is correct"); return; } Log.Entry(Name, string.Format("Renaming host to {0}", response.GetField("#hostname"))); Log.Entry(Name, "Unregistering computer"); //First unjoin it from active directory UnRegisterComputer(response); if (Power.ShuttingDown || Power.Requested) return; Log.Entry(Name, "Updating registry"); RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters","NV Hostname", response.GetField("#hostname")); RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName", "ComputerName", response.GetField("#hostname")); RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName", response.GetField("#hostname")); Power.Restart(RegistryHandler.GetSystemSetting("Company") + " needs to rename your computer", Power.FormOption.Delay); }
//Add a host to active directory private void RegisterComputer(Response response) { if (response.GetField("#AD") != "1") return; Log.Entry(Name, "Registering host with active directory"); if (!response.IsFieldValid("#ADDom") || !response.IsFieldValid("#ADUser") || !response.IsFieldValid("#ADPass")) { Log.Error(Name, "Required Domain Joining information is missing"); return; } // Check if the host is already part of the set domain by checking server IPs try { using (var domain = Domain.GetComputerDomain()) { var currentIP = Dns.GetHostAddresses(domain.Name); var targetIP = Dns.GetHostAddresses(response.GetField("#ADDom")); if (currentIP.Intersect(targetIP).Any()) { Log.Entry(Name, "Host is already joined to target domain"); return; } } } catch (Exception) { // ignored } // Attempt to join the domain var returnCode = DomainWrapper(response, true, (JoinOptions.NetsetupJoinDomain | JoinOptions.NetsetupAcctCreate)); if (returnCode == 2224) returnCode = DomainWrapper(response, true, JoinOptions.NetsetupJoinDomain); else if (returnCode == 2 || returnCode == 50) returnCode = DomainWrapper(response, false, (JoinOptions.NetsetupJoinDomain | JoinOptions.NetsetupAcctCreate)); // Entry the results Log.Entry(Name, string.Format("{0} {1}", (_returnCodes.ContainsKey(returnCode) ? string.Format("{0}, code = ", _returnCodes[returnCode]) : "Unknown Return Code: "), returnCode)); if (returnCode.Equals(0)) Power.Restart("Host joined to Active Directory, restart required", Power.FormOption.Delay); }