Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Management;
- using System.Xml;
- using System.Xml.Schema;
- using Microsoft.SqlServer.Management.Smo;
- using Microsoft.SqlServer.Management.Common;
- using System.Data.SqlTypes;
- using System.Data.SqlClient;
- using System.Threading;
- namespace Image_Tool
- {
- public partial class Form1 : Form
- {
- string newpcname = "";
- string oldpcname = "";
- public Form1()
- {
- InitializeComponent();
- textBoxOldPCNavn.Text = Environment.MachineName;
- oldpcname = Environment.MachineName;
- }
- public void buttonDomain_Click(object sender, EventArgs e)
- {
- newpcname = textBoxPCNavn.Text + "PC";
- HandleJoinandRenameDomain("domain.local", newpcname, domainUser.Text, domainPassword.Text);
- }
- public string GetComputerName()
- {
- string szRet = "";
- ManagementClass objMC;
- ManagementObjectCollection objMOC;
- try
- {
- // caller doesn't have to catch
- objMC = new ManagementClass("Win32_ComputerSystem");
- objMOC = objMC.GetInstances();
- }
- catch (Exception e)
- {
- textBoxOutput.AppendText(e.Message);
- return szRet;
- }
- foreach (ManagementObject objMO in objMOC)
- {
- if (null != objMO)
- {
- szRet = objMO["Name"].ToString();
- break;
- }
- }
- return szRet;
- }
- public string GetDomainName()
- {
- string szRet = "";
- ManagementClass objMC;
- ManagementObjectCollection objMOC;
- try
- {
- // caller does not have to catch
- objMC = new ManagementClass("Win32_ComputerSystem");
- objMOC = objMC.GetInstances();
- }
- catch (Exception e)
- {
- textBoxOutput.AppendText(e.Message);
- return szRet;
- }
- foreach (ManagementObject objMO in objMOC)
- {
- if (null != objMO)
- {
- if ((bool)objMO["partofdomain"])
- {
- szRet = objMO["domain"].ToString();
- textBoxOutput.AppendText("Computeren er med i domænet: " + szRet + Environment.NewLine);
- }
- else
- {
- textBoxOutput.AppendText("Computeren er i en Workgroup, ikke domæne." + Environment.NewLine);
- }
- }
- }
- return szRet;
- }
- public void HandleJoinandRenameDomain(string szDomain, string szNewHostname, string szUsername, string szPassword)
- {
- string szCurrentHostname = GetComputerName();
- string szCurrent = GetDomainName();
- ManagementObject objMO = new ManagementObject("Win32_ComputerSystem.Name='" + szCurrentHostname + "'");
- if (null != objMO)
- {
- ManagementBaseObject result;
- objMO.Scope.Options.EnablePrivileges = true;
- objMO.Scope.Options.Authentication = AuthenticationLevel.PacketPrivacy;
- objMO.Scope.Options.Impersonation = ImpersonationLevel.Impersonate;
- if ((szCurrentHostname.ToUpper() != szNewHostname.ToUpper()) && ("" != szNewHostname))
- {
- try
- {
- textBoxOutput.AppendText("Join domæne: Sætter computernavn til " + szNewHostname.ToUpper() + Environment.NewLine);
- ManagementBaseObject query2;
- query2 = objMO.GetMethodParameters("Rename");
- query2["Name"] = szNewHostname;
- query2["Password"] = null;
- query2["UserName"] = null;
- result = objMO.InvokeMethod("Rename", query2, null);
- if (0 != (uint)result["ReturnValue"])
- {
- textBoxOutput.AppendText("Kunne ikke ændre computernavnet, fejlkode: " + result["ReturnValue"].ToString() + Environment.NewLine);
- }
- textBoxOutput.AppendText("Success!" + Environment.NewLine);
- }
- catch (InvalidOperationException e)
- {
- textBoxOutput.AppendText("Join domæne fejlede: " + e.Message + Environment.NewLine);
- return;
- }
- catch (ManagementException e)
- {
- textBoxOutput.AppendText("Join domæne fejlede, kunne ikke ændre computernavnet: " + e.Message + Environment.NewLine);
- return;
- }
- }
- Thread.Sleep(45000);
- if ("" != szCurrent)
- {
- if (szCurrent.ToUpper() == szDomain.ToUpper())
- {
- textBoxOutput.AppendText("Denne computer er allerede på domænet: " + szCurrentHostname + szCurrent + Environment.NewLine);
- return;
- }
- else
- {
- textBoxOutput.AppendText("Denne computer er allerede på et andet domain: " + szCurrent + Environment.NewLine);
- return;
- }
- }
- ManagementBaseObject query;
- try
- {
- query = objMO.GetMethodParameters("JoinDomainOrWorkgroup");
- }
- catch (ManagementException)
- {
- textBoxOutput.AppendText("Join domæne fejlede, kunne ikke finde den lokale computer (???)." + Environment.NewLine);
- return;
- }
- query["Name"] = szDomain;
- query["Password"] = szPassword;
- query["UserName"] = szUsername;
- query["FJoinOptions"] = 3; // + 256 (tried with and with 256
- textBoxOutput.AppendText("Prøver WMI metoden JoinDomainOrWorkgroup." + Environment.NewLine + "Domæne: " + query["Name"].ToString() + Environment.NewLine + "Username: " + query["UserName"].ToString() + Environment.NewLine + "Join options: " + query["FJoinOptions"].ToString() + Environment.NewLine);
- try
- {
- result = objMO.InvokeMethod("JoinDomainOrWorkgroup", query, null);
- }
- catch (ManagementException e)
- {
- textBoxOutput.AppendText("Join domæne fejlede, kode: " + (uint)e.ErrorCode + " kunne ikke udføre forespørgslen: " + e.Message + Environment.NewLine);
- return;
- }
- if (0 != (uint)result["ReturnValue"])
- {
- textBoxOutput.AppendText("Join domæne fejlede: " + (uint)result["ReturnValue"] + " kunne ikke udføre forespørgslen." + Environment.NewLine);
- return;
- }
- }
- else
- {
- textBoxOutput.AppendText("Join domæne fejlede: Kunne ikke åbne management objektet." + Environment.NewLine);
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement