Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Management;
  7.  
  8. namespace capaDatos
  9. {
  10. public class conexion
  11. {
  12.  
  13.  
  14.  
  15.  
  16. public ManagementScope ConectarWMI(string strBibliotecaWMI,bool blnHasDomain,
  17. bool blnIsRemote, string strServer, string strUsername,
  18. string strPassword, string strAuthority= null)
  19.  
  20. {
  21. ManagementScope scope = new ManagementScope();
  22. try
  23. {
  24. ConnectionOptions opciones = new ConnectionOptions();
  25. opciones.EnablePrivileges = true;
  26. opciones.Impersonation = ImpersonationLevel.Impersonate;
  27. if (blnHasDomain == true)
  28. {
  29. opciones.Authority = string.Format(@"ntlmdomain:{0}", strAuthority);
  30.  
  31. }
  32.  
  33. if (blnIsRemote)
  34. {
  35. opciones.Username = strUsername;
  36. opciones.Password = strPassword;
  37. }
  38.  
  39.  
  40. scope.Path.Path = string.Format(@"\{0}root{1}", strServer, strBibliotecaWMI);
  41.  
  42. Console.WriteLine(scope.Path.Path);
  43. scope.Options = opciones;
  44. scope.Connect();
  45. if (scope.IsConnected) { Console.WriteLine("Conexion exitosa"); }
  46. else { Console.WriteLine("Fallo de conexion"); }
  47. }
  48. catch(ManagementException me) { Console.WriteLine("ManagementException" + " ---> " + me.Message); }
  49. catch (Exception ex ) { Console.WriteLine("Exception" + " : " + ex.Message); }
  50. return scope;
  51. }
  52.  
  53. }
  54. }
  55.  
  56. --------------------------------------------------------------------------
  57. using System;
  58. using System.Collections.Generic;
  59. using System.Linq;
  60. using System.Text;
  61. using System.Threading.Tasks;
  62. using capaDatos;
  63. using System.Management;
  64.  
  65. namespace capaNegocio
  66. {
  67. public class AccesoWMI
  68. {
  69. public void Pruebas()
  70. {
  71. WMI c = new WMI();
  72.  
  73. c.ToGet_Win32_NetworkAdapter();
  74.  
  75. }
  76. public void pruebasRemotas()
  77. {
  78. conexion cone = new conexion();
  79.  
  80. cone.ConectarWMI("cimv2", false, true, "JohnnyDroop", "Dogy", "pandora864", null);
  81. }
  82. }
  83.  
  84. -------------------------------------------------------------------------
  85. using System;
  86. using System.Collections.Generic;
  87. using System.Linq;
  88. using System.Text;
  89. using System.Threading.Tasks;
  90. using capaNegocio;
  91.  
  92. namespace dgtsi_pruebas
  93. {
  94. class Program
  95. {
  96. static void Main(string[] args)
  97. {
  98. AccesoWMI w = new AccesoWMI();
  99. //w.Pruebas();
  100.  
  101. w.pruebasRemotas();
  102. Console.ReadLine();
  103.  
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement