Advertisement
Guest User

C# Problem

a guest
Apr 30th, 2012
1,889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10. using System.Management;
  11. using System.Xml;
  12. using System.Xml.Schema;
  13. using Microsoft.SqlServer.Management.Smo;
  14. using Microsoft.SqlServer.Management.Common;
  15. using System.Data.SqlTypes;
  16. using System.Data.SqlClient;
  17. using System.Threading;
  18.  
  19. namespace Image_Tool
  20. {
  21.     public partial class Form1 : Form
  22.     {
  23.         string newpcname = "";
  24.         string oldpcname = "";
  25.         public Form1()
  26.         {
  27.             InitializeComponent();
  28.             textBoxOldPCNavn.Text = Environment.MachineName;
  29.             oldpcname = Environment.MachineName;
  30.         }
  31.  
  32.         public void buttonDomain_Click(object sender, EventArgs e)
  33.         {
  34.             newpcname = textBoxPCNavn.Text + "PC";
  35.             HandleJoinandRenameDomain("domain.local", newpcname, domainUser.Text, domainPassword.Text);
  36.         }
  37.  
  38.         public string GetComputerName()
  39.         {
  40.             string szRet = "";
  41.             ManagementClass objMC;
  42.             ManagementObjectCollection objMOC;
  43.  
  44.             try
  45.             {
  46.                 // caller doesn't have to catch
  47.                 objMC = new ManagementClass("Win32_ComputerSystem");
  48.                 objMOC = objMC.GetInstances();
  49.             }
  50.             catch (Exception e)
  51.             {
  52.                 textBoxOutput.AppendText(e.Message);
  53.                 return szRet;
  54.             }
  55.  
  56.             foreach (ManagementObject objMO in objMOC)
  57.             {
  58.                 if (null != objMO)
  59.                 {
  60.                     szRet = objMO["Name"].ToString();
  61.                     break;
  62.                 }
  63.             }
  64.  
  65.             return szRet;
  66.         }
  67.  
  68.         public string GetDomainName()
  69.         {
  70.             string szRet = "";
  71.             ManagementClass objMC;
  72.             ManagementObjectCollection objMOC;
  73.  
  74.             try
  75.             {
  76.                 // caller does not have to catch
  77.                 objMC = new ManagementClass("Win32_ComputerSystem");
  78.                 objMOC = objMC.GetInstances();
  79.             }
  80.             catch (Exception e)
  81.             {
  82.                 textBoxOutput.AppendText(e.Message);
  83.                 return szRet;
  84.             }
  85.  
  86.             foreach (ManagementObject objMO in objMOC)
  87.             {
  88.                 if (null != objMO)
  89.                 {
  90.                     if ((bool)objMO["partofdomain"])
  91.                     {
  92.                         szRet = objMO["domain"].ToString();
  93.                         textBoxOutput.AppendText("Computeren er med i domænet: " + szRet + Environment.NewLine);
  94.                     }
  95.                     else
  96.                     {
  97.                         textBoxOutput.AppendText("Computeren er i en Workgroup, ikke domæne." + Environment.NewLine);
  98.                     }
  99.                 }
  100.             }
  101.  
  102.             return szRet;
  103.         }
  104.  
  105.         public void HandleJoinandRenameDomain(string szDomain, string szNewHostname, string szUsername, string szPassword)
  106.         {
  107.             string szCurrentHostname = GetComputerName();
  108.             string szCurrent = GetDomainName();
  109.             ManagementObject objMO = new ManagementObject("Win32_ComputerSystem.Name='" + szCurrentHostname + "'");
  110.             if (null != objMO)
  111.             {
  112.                 ManagementBaseObject result;
  113.  
  114.                 objMO.Scope.Options.EnablePrivileges = true;
  115.                 objMO.Scope.Options.Authentication = AuthenticationLevel.PacketPrivacy;
  116.                 objMO.Scope.Options.Impersonation = ImpersonationLevel.Impersonate;
  117.  
  118.                 if ((szCurrentHostname.ToUpper() != szNewHostname.ToUpper()) && ("" != szNewHostname))
  119.                 {
  120.                     try
  121.                     {
  122.                         textBoxOutput.AppendText("Join domæne: Sætter computernavn til " + szNewHostname.ToUpper() + Environment.NewLine);
  123.                         ManagementBaseObject query2;
  124.                         query2 = objMO.GetMethodParameters("Rename");
  125.                         query2["Name"] = szNewHostname;
  126.                         query2["Password"] = null;
  127.                         query2["UserName"] = null;
  128.  
  129.                         result = objMO.InvokeMethod("Rename", query2, null);
  130.  
  131.                         if (0 != (uint)result["ReturnValue"])
  132.                         {
  133.                             textBoxOutput.AppendText("Kunne ikke ændre computernavnet, fejlkode: " + result["ReturnValue"].ToString() + Environment.NewLine);
  134.                         }
  135.  
  136.                         textBoxOutput.AppendText("Success!" + Environment.NewLine);
  137.  
  138.                     }
  139.                     catch (InvalidOperationException e)
  140.                     {
  141.                         textBoxOutput.AppendText("Join domæne fejlede: " + e.Message + Environment.NewLine);
  142.                         return;
  143.                     }
  144.                     catch (ManagementException e)
  145.                     {
  146.                         textBoxOutput.AppendText("Join domæne fejlede, kunne ikke ændre computernavnet: " + e.Message + Environment.NewLine);
  147.                         return;
  148.                     }
  149.                 }
  150.                 Thread.Sleep(45000);
  151.                 if ("" != szCurrent)
  152.                 {
  153.                     if (szCurrent.ToUpper() == szDomain.ToUpper())
  154.                     {
  155.                         textBoxOutput.AppendText("Denne computer er allerede på domænet: " + szCurrentHostname + szCurrent + Environment.NewLine);
  156.                         return;
  157.  
  158.                     }
  159.                     else
  160.                     {
  161.                         textBoxOutput.AppendText("Denne computer er allerede på et andet domain: " + szCurrent + Environment.NewLine);
  162.                         return;
  163.                     }
  164.                 }
  165.  
  166.                 ManagementBaseObject query;
  167.                 try
  168.                 {
  169.                     query = objMO.GetMethodParameters("JoinDomainOrWorkgroup");
  170.                 }
  171.                 catch (ManagementException)
  172.                 {
  173.                     textBoxOutput.AppendText("Join domæne fejlede, kunne ikke finde den lokale computer (???)." + Environment.NewLine);
  174.                     return;
  175.                 }
  176.                 query["Name"] = szDomain;
  177.                 query["Password"] = szPassword;
  178.                 query["UserName"] = szUsername;
  179.                 query["FJoinOptions"] = 3; // + 256 (tried with and with 256
  180.  
  181.                 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);
  182.                 try
  183.                 {
  184.                     result = objMO.InvokeMethod("JoinDomainOrWorkgroup", query, null);
  185.                 }
  186.                 catch (ManagementException e)
  187.                 {
  188.                     textBoxOutput.AppendText("Join domæne fejlede, kode: " + (uint)e.ErrorCode + " kunne ikke udføre forespørgslen: " + e.Message + Environment.NewLine);
  189.                     return;
  190.                 }
  191.  
  192.                 if (0 != (uint)result["ReturnValue"])
  193.                 {
  194.                     textBoxOutput.AppendText("Join domæne fejlede: " + (uint)result["ReturnValue"] + " kunne ikke udføre forespørgslen." + Environment.NewLine);
  195.                     return;
  196.                 }
  197.  
  198.  
  199.             }
  200.             else
  201.             {
  202.                 textBoxOutput.AppendText("Join domæne fejlede: Kunne ikke åbne management objektet." + Environment.NewLine);
  203.                 return;
  204.             }
  205.         }
  206.  
  207.  
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement