static int Main(string[] args) { if (args.Length < 1) { return(-1); } if (args[0].ToLower() == "-unjoindom") { try { bool partOfDomain = false; string domain = null; using (ManagementObject comObj = WMIUtil.QueryLocalWMI("Select * from win32_computersystem")) { partOfDomain = (bool)comObj["PartOfDomain"]; domain = (string)comObj["Domain"]; if (!partOfDomain) {// this must be a bug, because the button shouldn't be activated if the instance isn't a domain member // MessageBox.Show("This instance is not a member of any domain"); Console.WriteLine("EucaCLI: The instance is not a member of any domain"); return(-1); } /* string adAddress = null, adUsername = null, adPassword = null; * object tmp = EucaUtil.GetRegistryValue("ADAddress"); * if (tmp != null) * adAddress = (string)tmp; * tmp = EucaUtil.GetRegistryValue("ADUsername"); * if (tmp != null) * adUsername = (string)tmp; * tmp = EucaUtil.GetRegistryValue("ADPassword"); * if (tmp != null) * adPassword = (string)tmp; * * if (adUsername == null || adPassword == null || adAddress == null) * { * Console.WriteLine("EucaCLI: Username/password/ADaddress is not found"); * return -1; * } * * if (!adUsername.Contains("\\")) * adUsername = string.Format("{0}\\{1}", adAddress, adUsername); * * adPassword = EucaUtil.Decrypt(adPassword);*/ ManagementBaseObject paramIn = comObj.GetMethodParameters("UnjoinDomainOrWorkgroup"); paramIn["Password"] = null; paramIn["UserName"] = null; paramIn["FUnjoinOptions"] = (UInt32)0x00; // default; No option ManagementBaseObject paramOut = comObj.InvokeMethod("UnjoinDomainOrWorkgroup", paramIn, null); UInt32 retVal = (UInt32)paramOut["ReturnValue"]; if (retVal == 0) { Console.WriteLine("SUCCESS"); string hostnameFile = EucaConstant.ProgramRoot + "\\hostname"; if (System.IO.File.Exists(hostnameFile)) { try { System.IO.File.Delete(hostnameFile); } catch (Exception) {; } } return(0); } else { Console.WriteLine(string.Format("EucaCLI: Could not detach the instance: exit code={0}", retVal)); return(-1); } } } catch (Exception ie) { Console.WriteLine("EucaCLI: Can't detach the instance from the domain (exception thrown)"); return(-1); } } else if (args[0].ToLower() == "-setdom") { try { string uname = args[1].Trim(); string passwd = args[2].Trim(); if (uname == null || passwd == null) { Console.WriteLine("EucaCLI: uname and passwd is null"); return(-1); } string passwdEnc = SystemsUtil.Encrypt(passwd); EucaServiceLibraryUtil.SetSvcRegistryValue("ADUsername", uname); EucaServiceLibraryUtil.SetSvcRegistryValue("ADPassword", passwdEnc); Console.WriteLine("SUCCESS"); return(0); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); return(-1); } } else { Console.WriteLine("Unknown parameter"); return(-1); } }
private void buttonApply_Click(object sender, EventArgs e) { string adAddr = textBoxADAddress.Text; if (adAddr != null && adAddr.Length == 0) { adAddr = null; } string adUsername = textBoxADUsername.Text; if (adUsername != null && adUsername.Length == 0) { adUsername = null; } string adPassword = textBoxADPassword.Text; if (adPassword != null && adPassword.Length == 0) { adPassword = null; } string adPasswordConfirm = textBoxPwdConfirm.Text; if (adPasswordConfirm != null && adPasswordConfirm.Length == 0) { adPasswordConfirm = null; } string adOu = textBoxADOU.Text; if (adOu != null && adOu.Length == 0) { adOu = null; } if (!(adAddr == null && adUsername == null && adPassword == null && adOu == null)) {// user has put something in the config window if (adAddr == null || adUsername == null || adPassword == null) { MessageBox.Show("AD address, username, and password are required to enable AD join"); return; } } else { MessageBox.Show("AD address, username, and password are required to enable AD join"); return; } if (adPassword != adPasswordConfirm) { MessageBox.Show("Passwords do not match!"); return; } bool updated = false; if (adAddr.Trim() != (string)EucaServiceLibraryUtil.GetSvcRegistryValue("ADAddress")) { EucaServiceLibraryUtil.SetSvcRegistryValue("ADAddress", adAddr.Trim()); updated = true; } if (adUsername.Trim() != (string)EucaServiceLibraryUtil.GetSvcRegistryValue("ADUsername")) { EucaServiceLibraryUtil.SetSvcRegistryValue("ADUsername", adUsername.Trim()); updated = true; } //encrypt AD password properly if (adPassword.Trim() != (string)EucaServiceLibraryUtil.GetSvcRegistryValue("ADPassword")) { string data = SystemsUtil.Encrypt(adPassword.Trim()); EucaServiceLibraryUtil.SetSvcRegistryValue("ADPassword", data); updated = true; } if (adOu != null) { if (adOu.Trim() != (string)EucaServiceLibraryUtil.GetSvcRegistryValue("ADOU")) { EucaServiceLibraryUtil.SetSvcRegistryValue("ADOU", adOu.Trim()); updated = true; } } if (updated) { MessageBox.Show("AD information is updated succesfully"); buttonApply.Enabled = false; } else { MessageBox.Show("No change has been saved"); } }