Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using System.DirectoryServices;
  17. using System.Net.NetworkInformation;
  18. using System.Management;
  19. using System.IO;
  20. using Path = System.IO.Path;
  21.  
  22. namespace remote_install_uninstal
  23. {
  24. /// <summary>
  25. /// Логика взаимодействия для MainWindow.xaml
  26. /// </summary>
  27. public partial class MainWindow : Window
  28. {
  29. static List<string> pcname = new List<string>();
  30. public MainWindow()
  31. {
  32. InitializeComponent();
  33.  
  34. Thread PC_Name = new Thread(new ThreadStart(GetPCName));
  35. PC_Name.Start();
  36. }
  37.  
  38. public void GetPCName()
  39. {
  40. DirectoryEntry parent = new DirectoryEntry("WinNT:");
  41. foreach (DirectoryEntry dm in parent.Children)
  42. {
  43. DirectoryEntry coParent = new DirectoryEntry("WinNT://" + dm.Name);
  44. DirectoryEntries dent = coParent.Children;
  45. dent.SchemaFilter.Add("Computer");
  46. foreach (DirectoryEntry client in dent)
  47. {
  48. pcname.Add(client.Name);
  49. }
  50. }
  51. }
  52.  
  53. private void Pc_combobox_TextChanged(object sender, TextChangedEventArgs e)
  54. {
  55. RefreshComboboxItems();
  56. }
  57.  
  58. public void RefreshComboboxItems()
  59. {
  60. string textToSearch = pc_combobox.Text.ToUpper();
  61.  
  62. pc_combobox.Items.Clear();
  63.  
  64. foreach (var pc in pcname)
  65. {
  66. if (pc.Contains(textToSearch))
  67. {
  68. pc_combobox.Items.Add(pc);
  69. }
  70. }
  71. }
  72.  
  73. public void ping()
  74. {
  75. if (pc_combobox.SelectedValue == null)
  76. return;
  77.  
  78. Ping PingRemotePC = new Ping();
  79. PingReply reply = PingRemotePC.Send(pc_combobox.SelectedValue.ToString(), 1000);
  80. string status = reply.Status.ToString();
  81.  
  82. if (status == "Success")
  83. {
  84. button_install.IsEnabled = true;
  85. }
  86. else
  87. {
  88. button_install.IsEnabled = false;
  89. MessageBox.Show("Удаленный компьютер выключен");
  90. }
  91. }
  92.  
  93. private void Pc_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  94. {
  95. ping();
  96. }
  97.  
  98. private void Button_install_Click(object sender, RoutedEventArgs e)
  99. {
  100. Thread myThread = new Thread(new ParameterizedThreadStart(install_uninstall));
  101. myThread.Start(pc_combobox.Text);
  102. //install_uninstall(_targetIpAddress);
  103. }
  104.  
  105. public void install_uninstall(object PC)
  106. {
  107. string _targetIpAddress = (string)PC;
  108. string[] directum = { "1.msi", "2.msi", "3.msi" };
  109. string[] directum_uninstall = { "1", "2", "3" };
  110. string retVal = "";
  111.  
  112. ConnectionOptions options = new ConnectionOptions();
  113. //options.Username = @"domainusername";
  114. //options.Password = "password";
  115. ManagementScope scope = new ManagementScope(("\\" + _targetIpAddress + "\root\cimv2"), options);
  116. scope.Connect();
  117.  
  118. ManagementObjectSearcher searcher;
  119. SelectQuery query;
  120. EnumerationOptions enumOptions;
  121. ManagementPath p = new ManagementPath("Win32_Product");
  122. ManagementClass classInstance = new ManagementClass(scope, p, null);
  123. ManagementBaseObject inParams = classInstance.GetMethodParameters("Install");
  124. ManagementBaseObject outParams;
  125. inParams["AllUsers"] = true;
  126. inParams["Options"] = string.Empty;
  127.  
  128. foreach (var item in directum_uninstall)
  129. {
  130. query = new SelectQuery("Win32_Product", "Name='" + item + "'");
  131.  
  132. enumOptions = new EnumerationOptions();
  133. enumOptions.ReturnImmediately = true;
  134. enumOptions.Rewindable = false;
  135.  
  136. searcher = new ManagementObjectSearcher(scope, query);
  137.  
  138. foreach (ManagementObject app in searcher.Get())
  139. {
  140. outParams = app.InvokeMethod("Uninstall", null, null);
  141. retVal = outParams["ReturnValue"].ToString();
  142.  
  143. if (retVal == "0")
  144. {
  145. //text_install.Document.Blocks.Add(new Paragraph(new Run("The Uninstall " + item + " completed successfully.")));
  146. }
  147. else
  148. {
  149. //text_install.Document.Blocks.Add(new Paragraph(new Run("Unknown error" + outParams["ReturnValue"].ToString())));
  150. }
  151.  
  152. }
  153. }
  154.  
  155. foreach (var item in directum)
  156. {
  157. inParams["PackageLocation"] = @"\" + _targetIpAddress + @"d$folder" + item;
  158. outParams = classInstance.InvokeMethod("Install", inParams, null);
  159. retVal = outParams["ReturnValue"].ToString();
  160.  
  161. retVal_install(programm, inParams, retVal, item);
  162. }
  163. }
  164.  
  165. public void retVal_install(string[] _programm, ManagementBaseObject _inParams, string _retVal, string _item)
  166. {
  167. switch (_retVal)
  168. {
  169. case "0":
  170.  
  171. text_install.Document.Blocks.Add(new Paragraph(new Run("The installation " + _item + " completed successfully.")));
  172.  
  173. break;
  174.  
  175. case "2":
  176.  
  177. text_install.Document.Blocks.Add(new Paragraph(new Run("The system cannot find the specified file. nrnr" + _inParams["PackageLocation"])));
  178.  
  179. break;
  180.  
  181. case "3":
  182.  
  183. text_install.Document.Blocks.Add(new Paragraph(new Run("The system cannot find the path specified. nrnr" + _inParams["PackageLocation"])));
  184.  
  185. break;
  186.  
  187. case "1619":
  188.  
  189. text_install.Document.Blocks.Add(new Paragraph(new Run("This installation package nrnr " + _inParams["PackageLocation"] + "nrnrcould not be opened, please verify that it is accessible.")));
  190.  
  191. break;
  192.  
  193. case "1620":
  194.  
  195. text_install.Document.Blocks.Add(new Paragraph(new Run("This installation package nrnr " + _inParams["PackageLocation"] + "nrnrcould not be opened, please verify that it is a valid MSI package.")));
  196.  
  197. break;
  198.  
  199. default:
  200.  
  201. text_install.Document.Blocks.Add(new Paragraph(new Run(_retVal)));
  202. break;
  203. }
  204. }
  205.  
  206. private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
  207. {
  208. // Get the subdirectories for the specified directory.
  209. DirectoryInfo dir = new DirectoryInfo(sourceDirName);
  210.  
  211. if (!dir.Exists)
  212. {
  213. throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName);
  214. }
  215.  
  216. DirectoryInfo[] dirs = dir.GetDirectories();
  217. // If the destination directory doesn't exist, create it.
  218. if (!Directory.Exists(destDirName))
  219. {
  220. Directory.CreateDirectory(destDirName);
  221. }
  222.  
  223. // Get the files in the directory and copy them to the new location.
  224. FileInfo[] files = dir.GetFiles();
  225. foreach (FileInfo file in files)
  226. {
  227. string temppath = Path.Combine(destDirName, file.Name);
  228.  
  229. if (!File.Exists(temppath))
  230. {
  231. file.CopyTo(temppath, false);
  232. }
  233. else
  234. {
  235. continue;
  236. }
  237.  
  238. }
  239.  
  240. // If copying subdirectories, copy them and their contents to new location.
  241. if (copySubDirs)
  242. {
  243. foreach (DirectoryInfo subdir in dirs)
  244. {
  245. string temppath = Path.Combine(destDirName, subdir.Name);
  246. DirectoryCopy(subdir.FullName, temppath, copySubDirs);
  247. }
  248. }
  249. }
  250. }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement