/smsclictr.automation/SMSClient.cs

# · C# · 2523 lines · 903 code · 119 blank · 1501 comment · 52 complexity · 336bbab2d69dab737ea07272ba1e927a MD5 · raw file

  1. //SCCM Client Center Automation Library (SMSCliCtr.automation)
  2. //Copyright (c) 2008 by Roger Zander
  3. //This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or any later version.
  4. //This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  5. //GNU General Public License: http://www.gnu.org/licenses/lgpl.html
  6. using System;
  7. using System.Collections;
  8. using System.Management;
  9. using System.Runtime.InteropServices;
  10. using System.Collections.Generic;
  11. [assembly: CLSCompliant(false)]
  12. namespace smsclictr.automation
  13. {
  14. /// <summary>
  15. /// SMSClient automation main class
  16. /// </summary>
  17. public class SMSClient : IDisposable
  18. {
  19. #region Internal Settings
  20. private WMIProvider oWMIProvider;
  21. private ManagementObject mo_SMS_Authority;
  22. private string sSiteCode;
  23. private string sMP;
  24. private SMSSchedules oSMSSchedules;
  25. private WMIRegistry oWMIRegistry;
  26. private WMIService oWMIService;
  27. private CCMSoftwareDistribution oCCMSoftwareDistribution;
  28. private WindowsInstaller oMSI;
  29. private WMIFileIO oWMIFileIO;
  30. private WMIComputerSystem oCompSys;
  31. private SMSComponents oSMSComponents;
  32. private SCCMDCM oDCM;
  33. private ManagementObject oSMS_Client;
  34. private ManagementObject oCCM_Client;
  35. private ManagementObject oCacheConfig;
  36. internal string pHostname;
  37. internal string pUsername;
  38. internal string pPassword;
  39. internal string sSMSVersion = "";
  40. internal string sLocalSMSPath = "";
  41. #endregion
  42. #region Constructors
  43. /// <summary>
  44. /// SMSClient Constructor
  45. /// </summary>
  46. /// <param name="hostname">Hostname or IP-Address of the remote host</param>
  47. /// <example>
  48. /// C#:
  49. /// <code>
  50. /// using System;
  51. /// using System.Collections.Generic;
  52. /// using System.Text;
  53. /// using smsclictr.automation;
  54. /// using System.Management;
  55. ///
  56. /// namespace ConsoleApplication1
  57. /// {
  58. /// class Program
  59. /// {
  60. /// static void Main(string[] args)
  61. /// {
  62. /// SMSClient oClient = new SMSClient("workstation01");
  63. /// }
  64. /// }
  65. /// }
  66. /// </code>
  67. /// PowerShell:
  68. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  69. /// <code>
  70. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  71. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  72. /// </code>
  73. /// </example>
  74. public SMSClient(string hostname)
  75. {
  76. connect(hostname, null, null);
  77. }
  78. /// <summary>
  79. /// SMSClient Constructor
  80. /// </summary>
  81. /// <param name="hostname">Hostname or IP-Address of the remote host</param>
  82. /// <param name="username">Username to Logon (Domain\User)</param>
  83. /// <param name="password">Password of the specified Username</param>
  84. /// <example>
  85. /// C#:
  86. /// <code>
  87. /// using System;
  88. /// using System.Collections.Generic;
  89. /// using System.Text;
  90. /// using smsclictr.automation;
  91. /// using System.Management;
  92. ///
  93. /// namespace ConsoleApplication1
  94. /// {
  95. /// class Program
  96. /// {
  97. /// static void Main(string[] args)
  98. /// {
  99. /// SMSClient oClient = new SMSClient("workstation01", "Domain\SMSAdmin", "password");
  100. /// }
  101. /// }
  102. /// }
  103. /// </code>
  104. /// PowerShell:
  105. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  106. /// <code>
  107. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  108. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01", "Domain\SMSAdmin", "password")
  109. /// </code>
  110. /// </example>
  111. public SMSClient(string hostname, string username, string password)
  112. {
  113. connect(hostname, username, password);
  114. }
  115. /// <summary>
  116. /// SMSClient Constructor
  117. /// </summary>
  118. /// <param name="wmiProvider">WMIProvider</param>
  119. public SMSClient(WMIProvider wmiProvider)
  120. {
  121. connect(wmiProvider);
  122. }
  123. /// <summary>
  124. /// Connect the WMI Namespace root\cimv2 on a remote system
  125. /// </summary>
  126. /// <param name="sHostname">Hostname</param>
  127. protected void connect(string sHostname)
  128. {
  129. connect(sHostname, null, null);
  130. }
  131. /// <summary>
  132. /// Connect the WMI Namespace root\cimv2 on a remote system
  133. /// </summary>
  134. /// <param name="sHostname"></param>
  135. /// <param name="username"></param>
  136. /// <param name="password"></param>
  137. protected void connect(string sHostname, string username, string password)
  138. {
  139. oWMIProvider = new WMIProvider(@"\\" + sHostname + @"\ROOT\cimv2", username, password);
  140. connect(oWMIProvider);
  141. pHostname = sHostname;
  142. pUsername = username;
  143. pPassword = password;
  144. }
  145. /// <summary>
  146. /// Connect the WMI Namespace root\cimv2 on a remote system
  147. /// </summary>
  148. /// <param name="oProvider">WMI Provider</param>
  149. protected void connect(WMIProvider oProvider)
  150. {
  151. oWMIProvider = oProvider;
  152. oSMSSchedules = new SMSSchedules(oWMIProvider);
  153. oWMIRegistry = new WMIRegistry(oWMIProvider);
  154. oWMIService = new WMIService(oWMIProvider);
  155. oCCMSoftwareDistribution = new CCMSoftwareDistribution(oWMIProvider);
  156. oMSI = new WindowsInstaller(oWMIProvider);
  157. oWMIFileIO = new WMIFileIO(oWMIProvider);
  158. oCompSys = new WMIComputerSystem(oWMIProvider);
  159. oSMSComponents = new SMSComponents(oWMIProvider);
  160. oDCM = new SCCMDCM(oWMIProvider);
  161. }
  162. /// <summary>
  163. /// SMSSchedules Class
  164. /// </summary>
  165. public SMSSchedules Schedules { get { return oSMSSchedules; } }
  166. /// <summary>
  167. /// CCM_SoftwareDistribution Class
  168. /// </summary>
  169. public CCMSoftwareDistribution SoftwareDistribution { get { return oCCMSoftwareDistribution; } }
  170. /// <summary>
  171. /// WindowsInstaller Class
  172. /// </summary>
  173. public WindowsInstaller MSI { get { return oMSI; } }
  174. /// <summary>
  175. /// WMIFileIO Class
  176. /// </summary>
  177. public WMIFileIO FileIO { get { return oWMIFileIO; } }
  178. /// <summary>
  179. /// WMIComputerSystem Class
  180. /// </summary>
  181. public WMIComputerSystem ComputerSystem { get { return oCompSys; } }
  182. /// <summary>
  183. /// SMSComponents Class
  184. /// </summary>
  185. public SMSComponents Components { get { return oSMSComponents; } }
  186. /// <summary>
  187. /// SCCMDCM Class
  188. /// </summary>
  189. public SCCMDCM DCM { get { return oDCM; } }
  190. /// <summary>
  191. /// Cache the Object "root\ccm:SMS_Client"
  192. /// </summary>
  193. internal ManagementObject mSMS_Client
  194. {
  195. get
  196. {
  197. if ((oSMS_Client == null) | Reload)
  198. {
  199. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  200. oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  201. Reload = false;
  202. return oProv.GetObject("SMS_Client=@");
  203. }
  204. else
  205. return oSMS_Client;
  206. }
  207. set
  208. {
  209. oSMS_Client = value;
  210. }
  211. }
  212. /// <summary>
  213. /// Cache the Object "root\ccm:CCM_Client"
  214. /// </summary>
  215. internal ManagementObject mCCM_Client
  216. {
  217. get
  218. {
  219. if ((oCCM_Client == null) | Reload)
  220. {
  221. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  222. oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  223. Reload = false;
  224. return oProv.GetObject("CCM_Client=@");
  225. }
  226. else
  227. return oCCM_Client;
  228. }
  229. set
  230. {
  231. oCCM_Client = value;
  232. }
  233. }
  234. /// <summary>
  235. /// Cache the Object "root\ccm\SoftMgmtAgent:CacheConfig"
  236. /// </summary>
  237. internal ManagementObject mCacheConfig
  238. {
  239. get
  240. {
  241. if ((oCacheConfig == null) | Reload)
  242. {
  243. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  244. oProv.mScope.Path.NamespacePath = @"root\ccm\SoftMgmtAgent";
  245. Reload = false;
  246. return oProv.GetObject("CacheConfig.ConfigKey='Cache'");
  247. }
  248. else
  249. return oCacheConfig;
  250. }
  251. set
  252. {
  253. oCacheConfig = value;
  254. }
  255. }
  256. /// <summary>
  257. /// Do not use cached Object. Reload the Object (Once!)
  258. /// </summary>
  259. public bool Reload
  260. {
  261. get;set;
  262. }
  263. #endregion
  264. #region Puplic Properties
  265. /// <summary>
  266. /// Dispose
  267. /// </summary>
  268. public void Dispose()
  269. {
  270. oWMIProvider = null;
  271. GC.Collect();
  272. }
  273. /// <summary>
  274. /// Get or Set the assigned SMS SiteCode
  275. /// </summary>
  276. /// <example>
  277. /// C#:
  278. /// <code>
  279. /// using System;
  280. /// using System.Collections.Generic;
  281. /// using System.Text;
  282. /// using smsclictr.automation;
  283. /// using System.Management;
  284. ///
  285. /// namespace ConsoleApplication1
  286. /// {
  287. /// class Program
  288. /// {
  289. /// static void Main(string[] args)
  290. /// {
  291. /// SMSClient oClient = new SMSClient("workstation01");
  292. /// Console.WriteLine(oClient.SiteCode);
  293. /// }
  294. /// }
  295. /// }
  296. /// </code>
  297. /// PowerShell:
  298. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  299. /// <code>
  300. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  301. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  302. /// $SMSClient.SiteCode
  303. /// </code>
  304. /// </example>
  305. public string SiteCode
  306. {
  307. get
  308. {
  309. if (sSiteCode != null)
  310. return sSiteCode;
  311. else
  312. {
  313. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  314. oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  315. sSiteCode = oProv.ExecuteMethod("SMS_Client", "GetAssignedSite").GetPropertyValue("sSiteCode").ToString();
  316. return sSiteCode;
  317. }
  318. }
  319. set
  320. {
  321. if (string.IsNullOrEmpty(value))
  322. sSiteCode = null;
  323. else
  324. {
  325. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  326. oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  327. ManagementBaseObject inParams = oProv.GetClass("SMS_Client").GetMethodParameters("SetAssignedSite");
  328. inParams["sSiteCode"] = value;
  329. oProv.ExecuteMethod("SMS_Client", "SetAssignedSite", inParams);
  330. //sSiteCode = value;
  331. sSiteCode = null; //to clear the cached code...
  332. }
  333. }
  334. }
  335. /// <summary>
  336. /// Get the assigned Management Point
  337. /// </summary>
  338. /// <example>
  339. /// C#:
  340. /// <code>
  341. /// using System;
  342. /// using System.Collections.Generic;
  343. /// using System.Text;
  344. /// using smsclictr.automation;
  345. /// using System.Management;
  346. ///
  347. /// namespace ConsoleApplication1
  348. /// {
  349. /// class Program
  350. /// {
  351. /// static void Main(string[] args)
  352. /// {
  353. /// SMSClient oClient = new SMSClient("workstation01");
  354. /// Console.WriteLine(oClient.ManagementPoint);
  355. /// }
  356. /// }
  357. /// }
  358. /// </code>
  359. /// PowerShell:
  360. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  361. /// <code>
  362. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  363. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  364. /// $SMSClient.ManagementPoint
  365. /// </code>
  366. /// </example>
  367. public string ManagementPoint
  368. {
  369. get
  370. {
  371. if (!string.IsNullOrEmpty(sMP))
  372. return sMP;
  373. else
  374. {
  375. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  376. oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  377. mo_SMS_Authority = oProv.GetObject("SMS_Authority.Name='SMS:" + SiteCode + "'");
  378. return mo_SMS_Authority.GetPropertyValue("CurrentManagementPoint").ToString();
  379. }
  380. }
  381. set
  382. {
  383. if (string.IsNullOrEmpty(value))
  384. sMP = null;
  385. }
  386. }
  387. /// <summary>
  388. /// Get the assigned proxy Management Point (if the client belongs to a secondary site)
  389. /// </summary>
  390. /// <example>
  391. /// C#:
  392. /// <code>
  393. /// using System;
  394. /// using System.Collections.Generic;
  395. /// using System.Text;
  396. /// using smsclictr.automation;
  397. /// using System.Management;
  398. ///
  399. /// namespace ConsoleApplication1
  400. /// {
  401. /// class Program
  402. /// {
  403. /// static void Main(string[] args)
  404. /// {
  405. /// SMSClient oClient = new SMSClient("workstation01");
  406. /// Console.WriteLine(oClient.ProxyManagementPoint);
  407. /// }
  408. /// }
  409. /// }
  410. /// </code>
  411. /// PowerShell:
  412. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  413. /// <code>
  414. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  415. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  416. /// $SMSClient.ProxyManagementPoint
  417. /// </code>
  418. /// </example>
  419. public string ProxyManagementPoint
  420. {
  421. get
  422. {
  423. ManagementObjectCollection MPProxies;
  424. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  425. oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  426. MPProxies = oProv.ExecuteQuery("SELECT * FROM SMS_MPProxyInformation Where State = 'Active'");
  427. foreach (ManagementObject MPProxy in MPProxies)
  428. {
  429. return MPProxy.GetPropertyValue("Name").ToString();
  430. }
  431. return null;
  432. }
  433. }
  434. /// <summary>
  435. /// Get the assigned Internet Management Point
  436. /// </summary>
  437. /// <example>
  438. /// C#:
  439. /// <code>
  440. /// using System;
  441. /// using System.Collections.Generic;
  442. /// using System.Text;
  443. /// using smsclictr.automation;
  444. /// using System.Management;
  445. ///
  446. /// namespace ConsoleApplication1
  447. /// {
  448. /// class Program
  449. /// {
  450. /// static void Main(string[] args)
  451. /// {
  452. /// SMSClient oClient = new SMSClient("workstation01");
  453. /// Console.WriteLine(oClient.InternetMP);
  454. /// }
  455. /// }
  456. /// }
  457. /// </code>
  458. /// PowerShell:
  459. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  460. /// <code>
  461. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  462. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  463. /// $SMSClient.InternetMP
  464. /// </code>
  465. /// </example>
  466. public string InternetMP
  467. {
  468. get
  469. {
  470. string sPort = oWMIRegistry.GetString(2147483650, @"SOFTWARE\Microsoft\SMS\Client\Internet Facing", "Internet MP Hostname", "");
  471. if (string.IsNullOrEmpty(sPort))
  472. {
  473. if (this.ComputerSystem.Win32_ComputerSystem["SystemType"].ToString().ToLower().Contains("x64"))
  474. {
  475. sPort = oWMIRegistry.GetString(2147483650, @"SOFTWARE\Wow6432Node\Microsoft\SMS\Client\Internet Facing", "Internet MP Hostname", "");
  476. }
  477. }
  478. return sPort;
  479. }
  480. set
  481. {
  482. if (!this.ComputerSystem.Win32_ComputerSystem["SystemType"].ToString().ToLower().Contains("x64"))
  483. {
  484. oWMIRegistry.SetStringValue(2147483650, @"SOFTWARE\Microsoft\SMS\Client\Internet Facing", "Internet MP Hostname", value);
  485. }
  486. else
  487. {
  488. oWMIRegistry.SetStringValue(2147483650, @"SOFTWARE\Wow6432Node\Microsoft\SMS\Client\Internet Facing", "Internet MP Hostname", value);
  489. }
  490. }
  491. }
  492. /// <summary>
  493. /// Get the assigned DNS Suffix
  494. /// </summary>
  495. /// <example>
  496. /// C#:
  497. /// <code>
  498. /// using System;
  499. /// using System.Collections.Generic;
  500. /// using System.Text;
  501. /// using smsclictr.automation;
  502. /// using System.Management;
  503. ///
  504. /// namespace ConsoleApplication1
  505. /// {
  506. /// class Program
  507. /// {
  508. /// static void Main(string[] args)
  509. /// {
  510. /// SMSClient oClient = new SMSClient("workstation01");
  511. /// Console.WriteLine(oClient.DNSSuffix);
  512. /// }
  513. /// }
  514. /// }
  515. /// </code>
  516. /// PowerShell:
  517. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  518. /// <code>
  519. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  520. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  521. /// $SMSClient.DNSSuffix
  522. /// </code>
  523. /// </example>
  524. public string DNSSuffix
  525. {
  526. get
  527. {
  528. string sPort = oWMIRegistry.GetString(2147483650, @"SOFTWARE\Microsoft\CCM\LocationServices", "DnsSuffix", "");
  529. if (string.IsNullOrEmpty(sPort))
  530. {
  531. if (this.ComputerSystem.Win32_ComputerSystem["SystemType"].ToString().ToLower().Contains("x64"))
  532. {
  533. sPort = oWMIRegistry.GetString(2147483650, @"SOFTWARE\Wow6432Node\Microsoft\CCM\LocationServices", "DnsSuffix", "");
  534. }
  535. }
  536. return sPort;
  537. }
  538. set
  539. {
  540. if (this.ComputerSystem.Win32_ComputerSystem["SystemType"].ToString().ToLower().Contains("x64"))
  541. {
  542. oWMIRegistry.SetStringValue(2147483650, @"SOFTWARE\Wow6432Node\Microsoft\CCM\LocationServices", "DnsSuffix", value);
  543. }
  544. else
  545. {
  546. oWMIRegistry.SetStringValue(2147483650, @"SOFTWARE\Microsoft\CCM\LocationServices", "DnsSuffix", value);
  547. }
  548. }
  549. }
  550. /// <summary>
  551. /// Get or Set the HTTP port for Client-Server communication
  552. /// </summary>
  553. /// <example>
  554. /// C#:
  555. /// <code>
  556. /// using System;
  557. /// using System.Collections.Generic;
  558. /// using System.Text;
  559. /// using smsclictr.automation;
  560. /// using System.Management;
  561. ///
  562. /// namespace ConsoleApplication1
  563. /// {
  564. /// class Program
  565. /// {
  566. /// static void Main(string[] args)
  567. /// {
  568. /// SMSClient oClient = new SMSClient("workstation01");
  569. /// Console.WriteLine(oClient.HttpPort);
  570. /// }
  571. /// }
  572. /// }
  573. /// </code>
  574. /// PowerShell:
  575. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  576. /// <code>
  577. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  578. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  579. /// $SMSClient.HttpPort
  580. /// </code>
  581. /// </example>
  582. public string HttpPort
  583. {
  584. get
  585. {
  586. string sPort = oWMIRegistry.GetDWord(2147483650, @"SOFTWARE\Microsoft\CCM", "HttpPort", "");
  587. if (string.IsNullOrEmpty(sPort))
  588. {
  589. if (this.ComputerSystem.Win32_ComputerSystem["SystemType"].ToString().ToLower().Contains("x64"))
  590. {
  591. sPort = oWMIRegistry.GetDWord(2147483650, @"SOFTWARE\Wow6432Node\Microsoft\CCM", "HttpPort", "");
  592. }
  593. }
  594. return sPort;
  595. }
  596. set
  597. {
  598. if (this.ComputerSystem.Win32_ComputerSystem["SystemType"].ToString().ToLower().Contains("x64"))
  599. {
  600. oWMIRegistry.SetDWord(2147483650, @"SOFTWARE\Wow6432Node\Microsoft\CCM", "HttpPort", value);
  601. }
  602. else
  603. {
  604. oWMIRegistry.SetDWord(2147483650, @"SOFTWARE\Microsoft\CCM", "HttpPort", value);
  605. }
  606. }
  607. }
  608. /// <summary>
  609. /// Get or Set the SCCM Server Locator Point
  610. /// </summary>
  611. /// <example>
  612. /// C#:
  613. /// <code>
  614. /// using System;
  615. /// using System.Collections.Generic;
  616. /// using System.Text;
  617. /// using smsclictr.automation;
  618. /// using System.Management;
  619. ///
  620. /// namespace ConsoleApplication1
  621. /// {
  622. /// class Program
  623. /// {
  624. /// static void Main(string[] args)
  625. /// {
  626. /// SMSClient oClient = new SMSClient("workstation01");
  627. /// Console.WriteLine(oClient.ServerLocatorPoint);
  628. /// }
  629. /// }
  630. /// }
  631. /// </code>
  632. /// PowerShell:
  633. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  634. /// <code>
  635. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  636. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  637. /// $SMSClient.ServerLocatorPoint
  638. /// </code>
  639. /// </example>
  640. public string ServerLocatorPoint
  641. {
  642. get
  643. {
  644. if (!this.oWMIProvider.isX86)
  645. {
  646. return oWMIRegistry.GetString(2147483650, @"SOFTWARE\Wow6432Node\Microsoft\CCM", "SMSSLP");
  647. }
  648. else
  649. {
  650. return oWMIRegistry.GetString(2147483650, @"SOFTWARE\Microsoft\CCM", "SMSSLP");
  651. }
  652. }
  653. set
  654. {
  655. if (!this.oWMIProvider.isX86)
  656. {
  657. oWMIRegistry.SetStringValue(2147483650, @"SOFTWARE\Wow6432Node\Microsoft\CCM", "SMSSLP", value);
  658. }
  659. else
  660. {
  661. oWMIRegistry.SetStringValue(2147483650, @"SOFTWARE\Microsoft\CCM", "SMSSLP", value);
  662. }
  663. }
  664. }
  665. /// <summary>
  666. /// Get or Set the SMS Cache Path
  667. /// </summary>
  668. /// <example>
  669. /// C#:
  670. /// <code>
  671. /// using System;
  672. /// using System.Collections.Generic;
  673. /// using System.Text;
  674. /// using smsclictr.automation;
  675. /// using System.Management;
  676. ///
  677. /// namespace ConsoleApplication1
  678. /// {
  679. /// class Program
  680. /// {
  681. /// static void Main(string[] args)
  682. /// {
  683. /// SMSClient oClient = new SMSClient("workstation01");
  684. /// Console.WriteLine(oClient.CachePath);
  685. /// }
  686. /// }
  687. /// }
  688. /// </code>
  689. /// PowerShell:
  690. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  691. /// <code>
  692. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  693. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  694. /// $SMSClient.CachePath
  695. /// </code>
  696. /// </example>
  697. public string CachePath
  698. {
  699. get
  700. {
  701. return mCacheConfig.GetPropertyValue("Location").ToString();
  702. }
  703. set
  704. {
  705. string CachePath = value;
  706. if (CachePath.Length > 3)
  707. {
  708. ManagementObject MO = mCacheConfig;
  709. MO.SetPropertyValue("Location", CachePath);
  710. MO.Put();
  711. mCacheConfig = MO;
  712. }
  713. }
  714. }
  715. /// <summary>
  716. /// Get or Set the SMS Cache Size in Megabyte (MB)
  717. /// </summary>
  718. /// <example>
  719. /// C#:
  720. /// <code>
  721. /// using System;
  722. /// using System.Collections.Generic;
  723. /// using System.Text;
  724. /// using smsclictr.automation;
  725. /// using System.Management;
  726. ///
  727. /// namespace ConsoleApplication1
  728. /// {
  729. /// class Program
  730. /// {
  731. /// static void Main(string[] args)
  732. /// {
  733. /// SMSClient oClient = new SMSClient("workstation01");
  734. /// UInt32 CacheSize = UInt32.Parse(oClient.CacheSize);
  735. /// if (CacheSize != 600)
  736. /// {
  737. /// //Set the CacheSize to 600MB
  738. /// oClient.CacheSize = "600";
  739. /// }
  740. /// }
  741. /// }
  742. /// }
  743. /// </code>
  744. /// PowerShell:
  745. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  746. /// <code>
  747. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  748. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  749. /// $SMSClient.CacheSize = "600"
  750. /// </code>
  751. /// </example>
  752. public string CacheSize
  753. {
  754. get
  755. {
  756. return mCacheConfig.GetPropertyValue("Size").ToString();
  757. }
  758. set
  759. {
  760. UInt32 CacheSize = System.Convert.ToUInt32(value);
  761. if (CacheSize > 0)
  762. {
  763. ManagementObject MO = mCacheConfig;
  764. MO.SetPropertyValue("Size", CacheSize);
  765. MO.Put();
  766. mCacheConfig = MO;
  767. }
  768. }
  769. }
  770. /// <summary>
  771. /// Size of the downloaded packages in cache
  772. /// </summary>
  773. public Int32 CacheContentSize
  774. {
  775. get
  776. {
  777. ManagementObjectCollection MOC;
  778. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  779. oProv.mScope.Path.NamespacePath = @"root\ccm\SoftMgmtAgent";
  780. if (this.SMSVersion.StartsWith("4"))
  781. {
  782. MOC = oProv.ExecuteQuery("SELECT * FROM CacheInfoEx");
  783. }
  784. else
  785. {
  786. MOC = oProv.ExecuteQuery("SELECT * FROM CacheInfo");
  787. }
  788. Int32 uSize = 0;
  789. foreach (ManagementObject MO in MOC)
  790. {
  791. uSize = uSize + Int32.Parse(MO.GetPropertyValue("ContentSize").ToString());
  792. }
  793. return uSize;
  794. }
  795. }
  796. /// <summary>
  797. /// Allow the local Admin to override Site Settings
  798. /// </summary>
  799. /// <example>
  800. /// C#:
  801. /// <code>
  802. /// using System;
  803. /// using System.Collections.Generic;
  804. /// using System.Text;
  805. /// using smsclictr.automation;
  806. /// using System.Management;
  807. ///
  808. /// namespace ConsoleApplication1
  809. /// {
  810. /// class Program
  811. /// {
  812. /// static void Main(string[] args)
  813. /// {
  814. /// SMSClient oClient = new SMSClient("workstation01");
  815. /// //Prevent an Admin to change SMS Settings (Control Panel)
  816. /// oClient.AllowLocalAdminOverride = false;
  817. /// }
  818. /// }
  819. /// }
  820. /// </code>
  821. /// PowerShell:
  822. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  823. /// <code>
  824. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  825. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  826. /// $SMSClient.AllowLocalAdminOverride = "False"
  827. /// </code>
  828. /// </example>
  829. public bool AllowLocalAdminOverride
  830. {
  831. get
  832. {
  833. return Boolean.Parse(mSMS_Client.GetPropertyValue("AllowLocalAdminOverride").ToString());
  834. }
  835. set
  836. {
  837. ManagementObject MO = mSMS_Client;
  838. MO.SetPropertyValue("AllowLocalAdminOverride", value);
  839. MO.Put();
  840. mSMS_Client = MO;
  841. MO.Dispose();
  842. }
  843. }
  844. /// <summary>
  845. /// Get the SMS Agent Type
  846. /// </summary>
  847. /// <remarks>
  848. /// <list type="bullet">
  849. /// <item>1 = Advanced Client</item>
  850. /// <item>0 = Legacy Client</item>
  851. /// </list>
  852. /// </remarks>
  853. /// <example>
  854. /// C#:
  855. /// <code>
  856. /// using System;
  857. /// using System.Collections.Generic;
  858. /// using System.Text;
  859. /// using smsclictr.automation;
  860. /// using System.Management;
  861. ///
  862. /// namespace ConsoleApplication1
  863. /// {
  864. /// class Program
  865. /// {
  866. /// static void Main(string[] args)
  867. /// {
  868. /// SMSClient oClient = new SMSClient("workstation01");
  869. /// if (oClient.ClientType == 1)
  870. /// {
  871. /// Console.WriteLine("Advanced Client");
  872. /// }
  873. /// else
  874. /// {
  875. /// Console.WriteLine("Legacy Client");
  876. /// }
  877. /// }
  878. /// }
  879. /// }
  880. /// </code>
  881. /// PowerShell:
  882. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  883. /// <code>
  884. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  885. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  886. /// $SMSClient.ClientType
  887. /// </code>
  888. /// </example>
  889. public int ClientType
  890. {
  891. get
  892. {
  893. return int.Parse(mSMS_Client.GetPropertyValue("ClientType").ToString());
  894. }
  895. }
  896. /// <summary>
  897. /// Enable automatic Site assignment.
  898. /// <para>This will force the SMS Client to automatically reassign the SMS Site if the client roams to another SMS Primary Site.</para>
  899. /// </summary>
  900. /// <example>
  901. /// C#:
  902. /// <code>
  903. /// using System;
  904. /// using System.Collections.Generic;
  905. /// using System.Text;
  906. /// using smsclictr.automation;
  907. /// using System.Management;
  908. ///
  909. /// namespace ConsoleApplication1
  910. /// {
  911. /// class Program
  912. /// {
  913. /// static void Main(string[] args)
  914. /// {
  915. /// SMSClient oClient = new SMSClient("workstation01");
  916. /// oClient.EnableAutoAssignment = true;
  917. /// }
  918. /// }
  919. /// }
  920. /// </code>
  921. /// PowerShell:
  922. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  923. /// <code>
  924. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  925. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  926. /// $SMSClient.EnableAutoAssignment = "True"
  927. /// </code>
  928. /// </example>
  929. public bool EnableAutoAssignment {
  930. get
  931. {
  932. return bool.Parse(mSMS_Client.GetPropertyValue("EnableAutoAssignment").ToString());
  933. }
  934. set
  935. {
  936. //WMIProvider oProv = new WMIProvider(oWMIProvider.mScope);
  937. //oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  938. ManagementObject MO = mSMS_Client;
  939. MO.SetPropertyValue("EnableAutoAssignment", value);
  940. MO.Put();
  941. //Refresh cached Object
  942. mSMS_Client = MO;
  943. MO.Dispose();
  944. }
  945. }
  946. /// <summary>
  947. /// Get the SMS GUID
  948. /// </summary>
  949. /// <example>
  950. /// C#:
  951. /// <code>
  952. /// using System;
  953. /// using System.Collections.Generic;
  954. /// using System.Text;
  955. /// using smsclictr.automation;
  956. /// using System.Management;
  957. ///
  958. /// namespace ConsoleApplication1
  959. /// {
  960. /// class Program
  961. /// {
  962. /// static void Main(string[] args)
  963. /// {
  964. /// SMSClient oClient = new SMSClient("workstation01");
  965. /// Console.WriteLine(oClient.ClientId);
  966. /// }
  967. /// }
  968. /// }
  969. /// </code>
  970. /// PowerShell:
  971. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  972. /// <code>
  973. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  974. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  975. /// $SMSClient.ClientID
  976. /// </code>
  977. /// Output Example:
  978. /// <code>
  979. /// GUID:645567CB-D0C5-4A31-8D54-A74A88E565C4
  980. /// </code>
  981. /// </example>
  982. public string ClientId
  983. {
  984. get
  985. {
  986. return mCCM_Client.GetPropertyValue("ClientId").ToString();
  987. }
  988. }
  989. /// <summary>
  990. /// Delete the SMS/SCCM GUID on next SMS Agent restart
  991. /// </summary>
  992. /// <example>
  993. /// C#:
  994. /// <code>
  995. /// using System;
  996. /// using System.Collections.Generic;
  997. /// using System.Text;
  998. /// using smsclictr.automation;
  999. /// using System.Management;
  1000. ///
  1001. /// namespace ConsoleApplication1
  1002. /// {
  1003. /// class Program
  1004. /// {
  1005. /// static void Main(string[] args)
  1006. /// {
  1007. /// SMSClient oClient = new SMSClient("workstation01");
  1008. /// oClient.DeleteGUID();
  1009. /// oClient.RestartSMSAgent();
  1010. /// }
  1011. /// }
  1012. /// }
  1013. /// </code>
  1014. /// PowerShell:
  1015. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1016. /// <code>
  1017. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1018. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1019. /// $SMSClient.DeleteGUID()
  1020. /// $SMSClient.RestartSMSAgent()
  1021. /// </code>
  1022. /// </example>
  1023. public void DeleteGUID()
  1024. {
  1025. if (this.SMSVersion.StartsWith("4"))
  1026. {
  1027. //Create an SMSCFG.INI File with a new, random GUID ...
  1028. using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"\\" + this.Connection.mScope.Path.Server + @"\admin$\SMSCFG.INI", false))
  1029. {
  1030. sw.WriteLine("[Configuration - Client Properties]");
  1031. sw.WriteLine("SMS Unique Identifier=GUID:" + System.Guid.NewGuid().ToString().ToUpper());
  1032. sw.Close();
  1033. }
  1034. }
  1035. else
  1036. {
  1037. //On SMS2003 Agents, delete the smscfg.ini
  1038. string WinDir = ComputerSystem.WindowsDirectory;
  1039. FileIO.DeleteFile(WinDir + @"\smscfg.ini");
  1040. }
  1041. }
  1042. /// <summary>
  1043. /// Get the last GUID change date
  1044. /// </summary>
  1045. /// <example>
  1046. /// C#:
  1047. /// <code>
  1048. /// using System;
  1049. /// using System.Collections.Generic;
  1050. /// using System.Text;
  1051. /// using smsclictr.automation;
  1052. /// using System.Management;
  1053. ///
  1054. /// namespace ConsoleApplication1
  1055. /// {
  1056. /// class Program
  1057. /// {
  1058. /// static void Main(string[] args)
  1059. /// {
  1060. /// SMSClient oClient = new SMSClient("workstation01");
  1061. /// Console.WriteLine(oClient.ClientIdChangeDate);
  1062. /// }
  1063. /// }
  1064. /// }
  1065. /// </code>
  1066. /// PowerShell:
  1067. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1068. /// <code>
  1069. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1070. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1071. /// $SMSClient.ClientIDChangeDate
  1072. /// </code>
  1073. /// Output Example:
  1074. /// <code>
  1075. /// 12/22/2006 15:18:10
  1076. /// </code>
  1077. /// </example>
  1078. public string ClientIdChangeDate
  1079. {
  1080. get
  1081. {
  1082. return mCCM_Client.GetPropertyValue("ClientIdChangeDate").ToString();
  1083. }
  1084. }
  1085. /// <summary>
  1086. /// Get the previous SMS GUID
  1087. /// </summary>
  1088. /// <example>
  1089. /// C#:
  1090. /// <code>
  1091. /// using System;
  1092. /// using System.Collections.Generic;
  1093. /// using System.Text;
  1094. /// using smsclictr.automation;
  1095. /// using System.Management;
  1096. ///
  1097. /// namespace ConsoleApplication1
  1098. /// {
  1099. /// class Program
  1100. /// {
  1101. /// static void Main(string[] args)
  1102. /// {
  1103. /// SMSClient oClient = new SMSClient("workstation01");
  1104. /// Console.WriteLine(oClient.PreviousClientID);
  1105. /// }
  1106. /// }
  1107. /// }
  1108. /// </code>
  1109. /// PowerShell:
  1110. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1111. /// <code>
  1112. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1113. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1114. /// $SMSClient.PreviousClientID
  1115. /// </code>
  1116. /// Output Example:
  1117. /// <code>
  1118. /// GUID:645567CB-D0C5-4A31-8D54-A74A88E565C4
  1119. /// </code>
  1120. /// </example>
  1121. public string PreviousClientId
  1122. {
  1123. get
  1124. {
  1125. return mCCM_Client.GetPropertyValue("PreviousClientId").ToString();
  1126. }
  1127. }
  1128. /// <summary>
  1129. /// Get the SMS Version (like 2.50 )
  1130. /// </summary>
  1131. /// <value>
  1132. /// Known SMS Versions:
  1133. /// <list type="bullet">
  1134. /// <item>2.50 = SMS 2003</item>
  1135. /// <item>2.00 = SMS 2.0</item>
  1136. /// </list>
  1137. /// </value>
  1138. /// <example>
  1139. /// C#:
  1140. /// <code>
  1141. /// using System;
  1142. /// using System.Collections.Generic;
  1143. /// using System.Text;
  1144. /// using smsclictr.automation;
  1145. /// using System.Management;
  1146. ///
  1147. /// namespace ConsoleApplication1
  1148. /// {
  1149. /// class Program
  1150. /// {
  1151. /// static void Main(string[] args)
  1152. /// {
  1153. /// SMSClient oClient = new SMSClient("workstation01");
  1154. /// Console.WriteLine(oClient.SMSVersion);
  1155. /// }
  1156. /// }
  1157. /// }
  1158. /// </code>
  1159. /// PowerShell:
  1160. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1161. /// <code>
  1162. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1163. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1164. /// $SMSClient.SMSVersion
  1165. /// </code>
  1166. /// </example>
  1167. public string SMSVersion
  1168. {
  1169. get
  1170. {
  1171. if (string.IsNullOrEmpty(sSMSVersion))
  1172. {
  1173. string sVersion = mSMS_Client.GetPropertyValue("ClientVersion").ToString();
  1174. return sVersion;
  1175. }
  1176. else
  1177. return sSMSVersion;
  1178. }
  1179. set
  1180. {
  1181. sSMSVersion = "";
  1182. oSMS_Client = null;
  1183. }
  1184. }
  1185. /// <summary>
  1186. /// Get the Log-Directory Path
  1187. /// </summary>
  1188. /// <example>
  1189. /// C#:
  1190. /// <code>
  1191. /// using System;
  1192. /// using System.Collections.Generic;
  1193. /// using System.Text;
  1194. /// using smsclictr.automation;
  1195. /// using System.Management;
  1196. ///
  1197. /// namespace ConsoleApplication1
  1198. /// {
  1199. /// class Program
  1200. /// {
  1201. /// static void Main(string[] args)
  1202. /// {
  1203. /// SMSClient oClient = new SMSClient("workstation01");
  1204. /// Console.WriteLine(oClient.LogDirectory);
  1205. /// }
  1206. /// }
  1207. /// }
  1208. /// </code>
  1209. /// PowerShell:
  1210. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1211. /// <code>
  1212. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1213. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1214. /// $SMSClient.LogDirectory
  1215. /// </code>
  1216. /// Output Example:
  1217. /// <code>
  1218. /// C:\WINDOWS\system32\CCM\Logs
  1219. /// </code>
  1220. /// </example>
  1221. public string LogDirectory
  1222. {
  1223. get
  1224. {
  1225. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1226. oProv.mScope.Path.NamespacePath = @"ROOT\CCM\Policy\Machine";
  1227. return oProv.GetObject("CCM_Logging_GlobalConfiguration.DummyKey=1").GetPropertyValue("LogDirectory").ToString();
  1228. }
  1229. }
  1230. /// <summary>
  1231. /// Get the Local SMS Path
  1232. /// </summary>
  1233. /// <example>
  1234. /// C#:
  1235. /// <code>
  1236. /// using System;
  1237. /// using System.Collections.Generic;
  1238. /// using System.Text;
  1239. /// using smsclictr.automation;
  1240. /// using System.Management;
  1241. ///
  1242. /// namespace ConsoleApplication1
  1243. /// {
  1244. /// class Program
  1245. /// {
  1246. /// static void Main(string[] args)
  1247. /// {
  1248. /// SMSClient oClient = new SMSClient("workstation01");
  1249. /// Console.WriteLine(oClient.LocalSMSPath);
  1250. /// }
  1251. /// }
  1252. /// }
  1253. /// </code>
  1254. /// PowerShell:
  1255. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1256. /// <code>
  1257. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1258. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1259. /// $SMSClient.LocalSMSPath
  1260. /// </code>
  1261. /// Output Example:
  1262. /// <code>
  1263. /// C:\WINDOWS\system32\CCM
  1264. /// </code>
  1265. /// </example>
  1266. public string LocalSMSPath
  1267. {
  1268. get
  1269. {
  1270. if (string.IsNullOrEmpty(sLocalSMSPath))
  1271. {
  1272. sLocalSMSPath = oWMIRegistry.GetString(2147483650, @"SOFTWARE\Microsoft\SMS\Client\Configuration\Client Properties", "Local SMS Path");
  1273. }
  1274. return sLocalSMSPath;
  1275. }
  1276. }
  1277. /// <summary>
  1278. /// Get the MSI ProductCode of the installed SMS Client Agent
  1279. /// </summary>
  1280. /// <example>
  1281. /// C#:
  1282. /// <code>
  1283. /// using System;
  1284. /// using System.Collections.Generic;
  1285. /// using System.Text;
  1286. /// using smsclictr.automation;
  1287. /// using System.Management;
  1288. ///
  1289. /// namespace ConsoleApplication1
  1290. /// {
  1291. /// class Program
  1292. /// {
  1293. /// static void Main(string[] args)
  1294. /// {
  1295. /// SMSClient oClient = new SMSClient("workstation01");
  1296. /// Console.WriteLine(oClient.ProductCode);
  1297. /// }
  1298. /// }
  1299. /// }
  1300. /// </code>
  1301. /// PowerShell:
  1302. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1303. /// <code>
  1304. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1305. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1306. /// $SMSClient.ProductCode
  1307. /// </code>
  1308. /// Output Example:
  1309. /// <code>
  1310. /// {D97113AD-690F-4169-8637-4A046282D8F6}
  1311. /// </code>
  1312. /// </example>
  1313. public string ProductCode
  1314. {
  1315. get
  1316. {
  1317. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1318. oProv.mScope.Path.NamespacePath = @"root\ccm";
  1319. ManagementObjectCollection MOC = oProv.ExecuteQuery("SELECT * FROM CCM_InstalledProduct");
  1320. foreach (ManagementObject MO in MOC)
  1321. {
  1322. return MO.GetPropertyValue("ProductCode").ToString();
  1323. }
  1324. return "";
  1325. }
  1326. }
  1327. /// <summary>
  1328. /// OutOfBand AutoProvisioning
  1329. /// </summary>
  1330. public Boolean OOBAutoProvision
  1331. {
  1332. get
  1333. {
  1334. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1335. oProv.mScope.Path.NamespacePath = @"root\ccm\Policy\Machine\ActualConfig";
  1336. ManagementObjectCollection MOC = oProv.ExecuteQuery("SELECT * FROM CCM_OutOfBandManagementSettings");
  1337. foreach (ManagementObject MO in MOC)
  1338. {
  1339. return Boolean.Parse(MO.GetPropertyValue("AutoProvision").ToString());
  1340. }
  1341. return false;
  1342. }
  1343. set
  1344. {
  1345. try
  1346. {
  1347. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1348. oProv.mScope.Path.NamespacePath = @"root\ccm\Policy\Machine\ActualConfig";
  1349. ManagementObject MO = oProv.GetClass("CCM_OutOfBandManagementSettings").CreateInstance();
  1350. MO.SetPropertyValue("SiteSettingsKey", "1");
  1351. MO.SetPropertyValue("AutoProvision", value.ToString());
  1352. MO.Put();
  1353. }
  1354. catch { }
  1355. }
  1356. }
  1357. /// <summary>
  1358. /// Return List of all approved (from SCCM Site) Software Update ID's
  1359. /// </summary>
  1360. public List<string> ApprovedUpdateIDs
  1361. {
  1362. get
  1363. {
  1364. List<string> sResult = new List<string>();
  1365. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1366. oProv.mScope.Path.NamespacePath = @"root\ccm\Policy\Machine\RequestedConfig";
  1367. ManagementClass MC = oProv.GetClass("CCM_UpdateCIAssignment");
  1368. foreach (ManagementObject MO in MC.GetInstances())
  1369. {
  1370. try
  1371. {
  1372. string[] sAssignedCIs = MO.Properties["AssignedCIs"].Value as string[];
  1373. if (sAssignedCIs != null)
  1374. {
  1375. foreach (string sXML in sAssignedCIs)
  1376. {
  1377. try
  1378. {
  1379. System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
  1380. xDoc.LoadXml(sXML);
  1381. foreach (System.Xml.XmlNode xNode in xDoc.SelectNodes(@"/CI/ApplicabilityCondition/ApplicabilityRule/UpdateId"))
  1382. {
  1383. sResult.Add(xNode.InnerText);
  1384. }
  1385. }
  1386. catch { }
  1387. }
  1388. }
  1389. }
  1390. catch
  1391. { }
  1392. }
  1393. return sResult;
  1394. }
  1395. }
  1396. #endregion
  1397. #region Public Methods
  1398. /// <summary>
  1399. /// Request and download new Machine Policies
  1400. /// </summary>
  1401. /// <remarks>Downloaded policies needs to be evaluated(applied) before they become active</remarks>
  1402. /// <example>
  1403. /// C#:
  1404. /// <code>
  1405. /// using System;
  1406. /// using System.Collections.Generic;
  1407. /// using System.Text;
  1408. /// using smsclictr.automation;
  1409. /// using System.Management;
  1410. ///
  1411. /// namespace ConsoleApplication1
  1412. /// {
  1413. /// class Program
  1414. /// {
  1415. /// static void Main(string[] args)
  1416. /// {
  1417. /// SMSClient oClient = new SMSClient("workstation01");
  1418. /// oClient.RequestMachinePolicy();
  1419. /// }
  1420. /// }
  1421. /// }
  1422. /// </code>
  1423. /// PowerShell:
  1424. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1425. /// <code>
  1426. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1427. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1428. /// $SMSClient.RequestMachinePolicy()
  1429. /// </code>
  1430. /// </example>
  1431. public void RequestMachinePolicy()
  1432. {
  1433. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1434. oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  1435. oProv.ExecuteMethod("SMS_Client", "RequestMachinePolicy");
  1436. }
  1437. /// <summary>
  1438. /// Assign all downloaded Machine Policies
  1439. /// </summary>
  1440. /// <remarks>Wait approximately 2 Minutes after a policy request before evaluating policies</remarks>
  1441. /// <example>
  1442. /// C#:
  1443. /// <code>
  1444. /// using System;
  1445. /// using System.Collections.Generic;
  1446. /// using System.Text;
  1447. /// using smsclictr.automation;
  1448. /// using System.Management;
  1449. ///
  1450. /// namespace ConsoleApplication1
  1451. /// {
  1452. /// class Program
  1453. /// {
  1454. /// static void Main(string[] args)
  1455. /// {
  1456. /// SMSClient oClient = new SMSClient("workstation01");
  1457. /// oClient.EvaluateMachinePolicy();
  1458. /// }
  1459. /// }
  1460. /// }
  1461. /// </code>
  1462. /// PowerShell:
  1463. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1464. /// <code>
  1465. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1466. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1467. /// $SMSClient.EvaluateMachinePolicy()
  1468. /// </code>
  1469. /// </example>
  1470. public void EvaluateMachinePolicy()
  1471. {
  1472. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1473. oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  1474. oProv.ExecuteMethod("SMS_Client", "EvaluateMachinePolicy");
  1475. }
  1476. /// <summary>
  1477. /// Start the Reset Policy cycle to cleanup orphaned policies
  1478. /// </summary>
  1479. /// <example>
  1480. /// C#:
  1481. /// <code>
  1482. /// using System;
  1483. /// using System.Collections.Generic;
  1484. /// using System.Text;
  1485. /// using smsclictr.automation;
  1486. /// using System.Management;
  1487. ///
  1488. /// namespace ConsoleApplication1
  1489. /// {
  1490. /// class Program
  1491. /// {
  1492. /// static void Main(string[] args)
  1493. /// {
  1494. /// SMSClient oClient = new SMSClient("workstation01");
  1495. /// oClient.ResetPolicy();
  1496. /// }
  1497. /// }
  1498. /// }
  1499. /// </code>
  1500. /// PowerShell:
  1501. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1502. /// <code>
  1503. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1504. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1505. /// $SMSClient.ResetPolicy()
  1506. /// </code>
  1507. /// </example>
  1508. public void ResetPolicy()
  1509. {
  1510. ResetPolicy(false);
  1511. }
  1512. /// <summary>
  1513. /// Reset all SMS Policies (Full Reset)
  1514. /// </summary>
  1515. /// <param name="hard">Hard Reset</param>
  1516. /// <remarks>a Hard-Reset will remove all assigned policies.
  1517. /// <para>All pending downloads and executions will be stopped.</para></remarks>
  1518. /// <example>
  1519. /// C#:
  1520. /// <code>
  1521. /// using System;
  1522. /// using System.Collections.Generic;
  1523. /// using System.Text;
  1524. /// using smsclictr.automation;
  1525. /// using System.Management;
  1526. ///
  1527. /// namespace ConsoleApplication1
  1528. /// {
  1529. /// class Program
  1530. /// {
  1531. /// static void Main(string[] args)
  1532. /// {
  1533. /// SMSClient oClient = new SMSClient("workstation01");
  1534. /// oClient.ResetPolicy(true);
  1535. /// }
  1536. /// }
  1537. /// }
  1538. /// </code>
  1539. /// PowerShell:
  1540. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1541. /// <code>
  1542. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1543. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1544. /// $SMSClient.ResetPolicy("True")
  1545. /// </code>
  1546. /// </example>
  1547. public void ResetPolicy(Boolean hard)
  1548. {
  1549. if (hard)
  1550. {
  1551. try
  1552. {
  1553. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy", "SELECT * FROM CCM_SoftwareDistribution");
  1554. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy", "SELECT * FROM CCM_Scheduler_ScheduledMessage");
  1555. oWMIProvider.DeleteQueryResults(@"root\ccm\Scheduler", "SELECT * FROM CCM_Scheduler_History");
  1556. oWMIProvider.DeleteQueryResults(@"root\ccm\Scanagent", "SELECT * FROM CCM_ScanToolHistory");
  1557. /*oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\ActualConfig", "SELECT * FROM CCM_SoftwareDistribution");
  1558. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_SoftwareDistribution");
  1559. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_SoftwareDistributionClientConfig");
  1560. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_Policy_EmbeddedObject");
  1561. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_Policy");
  1562. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_Policy_Config");
  1563. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_Policy_Policy");
  1564. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_Policy_Policy2");
  1565. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_Policy_Policy3");
  1566. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_Policy_Rules");
  1567. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_Policy_AuthorityData");
  1568. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_Policy_AuthorityData2");
  1569. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_Policy_Assignment");
  1570. oWMIProvider.DeleteQueryResults(@"root\ccm\Policy\Machine\RequestedConfig", "SELECT * FROM CCM_Policy_Assignment2"); */
  1571. }
  1572. catch
  1573. {
  1574. throw;
  1575. }
  1576. }
  1577. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1578. oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  1579. ManagementClass WMIClass = oProv.GetClass("SMS_Client");
  1580. WMIClass.Get();
  1581. ManagementBaseObject inParams = WMIClass.GetMethodParameters("ResetPolicy");
  1582. inParams["uFlags"] = 1;
  1583. oProv.ExecuteMethod("SMS_Client", "ResetPolicy", inParams );
  1584. RequestMachinePolicy();
  1585. }
  1586. /// <summary>
  1587. /// Repair the SMS Agent
  1588. /// </summary>
  1589. /// <remarks>
  1590. /// The repair will be triggered by the SMS Agent.
  1591. /// This is only possible if the SMS Agent Service is running.
  1592. /// Use the "ReinstallMSI_Name" function to repair the Client.msi even if the SMS Agent is running or not.
  1593. /// </remarks>
  1594. /// <example>
  1595. /// C#:
  1596. /// <code>
  1597. /// using System;
  1598. /// using System.Collections.Generic;
  1599. /// using System.Text;
  1600. /// using smsclictr.automation;
  1601. /// using System.Management;
  1602. ///
  1603. /// namespace ConsoleApplication1
  1604. /// {
  1605. /// class Program
  1606. /// {
  1607. /// static void Main(string[] args)
  1608. /// {
  1609. /// SMSClient oClient = new SMSClient("workstation01");
  1610. /// oClient.RepairClient();
  1611. /// }
  1612. /// }
  1613. /// }
  1614. /// </code>
  1615. /// PowerShell:
  1616. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1617. /// <code>
  1618. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1619. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1620. /// $SMSClient.RepairClient()
  1621. /// </code>
  1622. /// </example>
  1623. public void RepairClient()
  1624. {
  1625. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1626. oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  1627. oProv.ExecuteMethod("SMS_Client", "RepairClient");
  1628. }
  1629. /// <summary>
  1630. /// remove the SMS Agent
  1631. /// </summary>
  1632. /// <returns>MSI Exit Code</returns>
  1633. /// <remarks>It's the same like: UninstallMSI_Name("SMS Advanced Client")</remarks>
  1634. /// <example>
  1635. /// C#:
  1636. /// <code>
  1637. /// using System;
  1638. /// using System.Collections.Generic;
  1639. /// using System.Text;
  1640. /// using smsclictr.automation;
  1641. /// using System.Management;
  1642. ///
  1643. /// namespace ConsoleApplication1
  1644. /// {
  1645. /// class Program
  1646. /// {
  1647. /// static void Main(string[] args)
  1648. /// {
  1649. /// SMSClient oClient = new SMSClient("workstation01");
  1650. /// oClient.UninstallClient();
  1651. /// }
  1652. /// }
  1653. /// }
  1654. /// </code>
  1655. /// PowerShell:
  1656. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1657. /// <code>
  1658. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1659. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1660. /// $SMSClient.UninstallClient()
  1661. /// </code>
  1662. /// </example>
  1663. public UInt32 UninstallClient()
  1664. {
  1665. string msiID = this.ProductCode;
  1666. if (!string.IsNullOrEmpty(msiID))
  1667. {
  1668. return oMSI.UninstallMSI_ID(msiID);
  1669. }
  1670. else
  1671. {
  1672. return 99;
  1673. }
  1674. }
  1675. /// <summary>
  1676. /// Stop the SMS Agent Service (CCMExec)
  1677. /// </summary>
  1678. /// <remarks>Send a stop command and wait until the Service is stopped</remarks>
  1679. /// <example>
  1680. /// C#:
  1681. /// <code>
  1682. /// using System;
  1683. /// using System.Collections.Generic;
  1684. /// using System.Text;
  1685. /// using smsclictr.automation;
  1686. /// using System.Management;
  1687. ///
  1688. /// namespace ConsoleApplication1
  1689. /// {
  1690. /// class Program
  1691. /// {
  1692. /// static void Main(string[] args)
  1693. /// {
  1694. /// SMSClient oClient = new SMSClient("workstation01");
  1695. /// oClient.StopAgent();
  1696. /// }
  1697. /// }
  1698. /// }
  1699. /// </code>
  1700. /// PowerShell:
  1701. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1702. /// <code>
  1703. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1704. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1705. /// $SMSClient.StopAgent()
  1706. /// </code>
  1707. /// </example>
  1708. public void StopSMSAgent()
  1709. {
  1710. oWMIService.StopService("ccmexec");
  1711. }
  1712. /// <summary>
  1713. /// Start the SMS Agent Service (CCMExec)
  1714. /// </summary>
  1715. /// <returns>Start Code</returns>
  1716. /// <remarks>Send a start command and wait until the Service is started</remarks>
  1717. /// <example>
  1718. /// C#:
  1719. /// <code>
  1720. /// using System;
  1721. /// using System.Collections.Generic;
  1722. /// using System.Text;
  1723. /// using smsclictr.automation;
  1724. /// using System.Management;
  1725. ///
  1726. /// namespace ConsoleApplication1
  1727. /// {
  1728. /// class Program
  1729. /// {
  1730. /// static void Main(string[] args)
  1731. /// {
  1732. /// SMSClient oClient = new SMSClient("workstation01");
  1733. /// oClient.StartAgent();
  1734. /// }
  1735. /// }
  1736. /// }
  1737. /// </code>
  1738. /// PowerShell:
  1739. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1740. /// <code>
  1741. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1742. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1743. /// $SMSClient.StartAgent()
  1744. /// </code>
  1745. /// </example>
  1746. public int StartSMSAgent()
  1747. {
  1748. return oWMIService.StartService("ccmexec");
  1749. }
  1750. /// <summary>
  1751. /// Restart the SMS Agent Service
  1752. /// </summary>
  1753. /// <remarks>Stop and Start CCMExec and all dependent services</remarks>
  1754. /// <example>
  1755. /// C#:
  1756. /// <code>
  1757. /// using System;
  1758. /// using System.Collections.Generic;
  1759. /// using System.Text;
  1760. /// using smsclictr.automation;
  1761. /// using System.Management;
  1762. ///
  1763. /// namespace ConsoleApplication1
  1764. /// {
  1765. /// class Program
  1766. /// {
  1767. /// static void Main(string[] args)
  1768. /// {
  1769. /// SMSClient oClient = new SMSClient("workstation01");
  1770. /// oClient.RestartSMSAgent();
  1771. /// }
  1772. /// }
  1773. /// }
  1774. /// </code>
  1775. /// PowerShell:
  1776. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1777. /// <code>
  1778. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1779. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1780. /// $SMSClient.RestartSMSAgent()
  1781. /// </code>
  1782. /// </example>
  1783. public void RestartSMSAgent()
  1784. {
  1785. oWMIService.StopService("ccmexec");
  1786. System.Threading.Thread.Sleep(1000);
  1787. oWMIService.StartService("ccmexec");
  1788. }
  1789. /// <summary>
  1790. /// Delete all Folders in the SMS Cache
  1791. /// </summary>
  1792. /// <remarks>Caution: The SMS Agent Service will be restarted</remarks>
  1793. /// <example>
  1794. /// C#:
  1795. /// <code>
  1796. /// using System;
  1797. /// using System.Collections.Generic;
  1798. /// using System.Text;
  1799. /// using smsclictr.automation;
  1800. /// using System.Management;
  1801. ///
  1802. /// namespace ConsoleApplication1
  1803. /// {
  1804. /// class Program
  1805. /// {
  1806. /// static void Main(string[] args)
  1807. /// {
  1808. /// SMSClient oClient = new SMSClient("workstation01");
  1809. /// oClient.CacheDelete();
  1810. /// }
  1811. /// }
  1812. /// }
  1813. /// </code>
  1814. /// PowerShell:
  1815. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1816. /// <code>
  1817. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1818. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1819. /// $SMSClient.CacheDelete()
  1820. /// </code>
  1821. /// </example>
  1822. public void CacheDelete()
  1823. {
  1824. //Delete CacheInfo-Items in WMI
  1825. ManagementObjectCollection MOC;
  1826. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1827. oProv.mScope.Path.NamespacePath = @"root\ccm\SoftMgmtAgent";
  1828. if (this.SMSVersion.StartsWith("4"))
  1829. {
  1830. MOC = oProv.ExecuteQuery("SELECT * FROM CacheInfoEx");
  1831. }
  1832. else
  1833. {
  1834. MOC = oProv.ExecuteQuery("SELECT * FROM CacheInfo");
  1835. }
  1836. foreach (ManagementObject MO in MOC)
  1837. {
  1838. MO.Delete();
  1839. }
  1840. //Delete Files and Folders
  1841. WMIFileIO FileIO = new WMIFileIO(oWMIProvider);
  1842. ArrayList SubFolders = FileIO.SubFolders(CachePath);
  1843. foreach (string path in SubFolders)
  1844. {
  1845. FileIO.DeleteFolder(path);
  1846. }
  1847. MOC.Dispose();
  1848. RestartSMSAgent();
  1849. }
  1850. /// <summary>
  1851. /// Cleanup all cached Packages where a newer Version exists
  1852. /// </summary>
  1853. /// <example>
  1854. /// C#:
  1855. /// <code>
  1856. /// using System;
  1857. /// using System.Collections.Generic;
  1858. /// using System.Text;
  1859. /// using smsclictr.automation;
  1860. /// using System.Management;
  1861. ///
  1862. /// namespace ConsoleApplication1
  1863. /// {
  1864. /// class Program
  1865. /// {
  1866. /// static void Main(string[] args)
  1867. /// {
  1868. /// SMSClient oClient = new SMSClient("workstation01");
  1869. /// oClient.CacheCleanupOldPackages();
  1870. /// }
  1871. /// }
  1872. /// }
  1873. /// </code>
  1874. /// PowerShell:
  1875. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1876. /// <code>
  1877. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1878. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1879. /// $SMSClient.CacheCleanupOldPackages()
  1880. /// </code>
  1881. /// </example>
  1882. public void CacheCleanupOldPackages()
  1883. {
  1884. try
  1885. {
  1886. string sCachePath = CachePath;
  1887. ArrayList CacheFolder = oWMIFileIO.SubFolders(sCachePath);
  1888. CacheFolder.Sort();
  1889. for (int i = 0; i < CacheFolder.Count - 1; i++)
  1890. {
  1891. string sFolder1 = CacheFolder[i].ToString();
  1892. string sPkgFolder1 = sFolder1.Replace(sCachePath + @"\", "");
  1893. string sFolder2 = CacheFolder[i + 1].ToString();
  1894. string sPkgFolder2 = sFolder2.Replace(sCachePath + @"\", "");
  1895. string[] aPkg1 = sPkgFolder1.Split('.');
  1896. string[] aPkg2 = sPkgFolder2.Split('.');
  1897. //check if the folders are package folders
  1898. if ((aPkg1.Length == 3) & (aPkg2.Length == 3))
  1899. {
  1900. //check if the PkgID and UserID match
  1901. if ((aPkg1[0] == aPkg2[0]) & (aPkg1[2] == aPkg2[2]))
  1902. {
  1903. //compare the PackageVersions and delete the older PackageFolder
  1904. if (int.Parse(aPkg1[1]) < int.Parse(aPkg2[1]))
  1905. {
  1906. oWMIFileIO.DeleteFolder(CacheFolder[i].ToString());
  1907. try
  1908. {
  1909. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1910. oProv.mScope.Path.NamespacePath = @"root\ccm\SoftMgmtAgent";
  1911. //oProv.DeleteQueryResults("SELECT * FROM CacheInfo where Location='" + CacheFolder[i].ToString().Replace(@"\", @"\\") + "'");
  1912. if (this.SMSVersion.StartsWith("4"))
  1913. {
  1914. oProv.DeleteQueryResults("SELECT * FROM CacheInfoEx where Location='" + CacheFolder[i].ToString().Replace(@"\", @"\\") + "'");
  1915. }
  1916. else
  1917. {
  1918. oProv.DeleteQueryResults("SELECT * FROM CacheInfo where Location='" + CacheFolder[i].ToString().Replace(@"\", @"\\") + "'");
  1919. }
  1920. }
  1921. catch { }
  1922. }
  1923. else
  1924. {
  1925. try
  1926. {
  1927. oWMIFileIO.DeleteFolder(CacheFolder[i + 1].ToString());
  1928. }
  1929. catch { }
  1930. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1931. oProv.mScope.Path.NamespacePath = @"root\ccm\SoftMgmtAgent";
  1932. //oProv.DeleteQueryResults("SELECT * FROM CacheInfo where Location='" + CacheFolder[i + 1].ToString().Replace(@"\", @"\\") + "'");
  1933. if (this.SMSVersion.StartsWith("4"))
  1934. {
  1935. oProv.DeleteQueryResults("SELECT * FROM CacheInfoEx where Location='" + CacheFolder[i + 1].ToString().Replace(@"\", @"\\") + "'");
  1936. }
  1937. else
  1938. {
  1939. oProv.DeleteQueryResults("SELECT * FROM CacheInfo where Location='" + CacheFolder[i + 1].ToString().Replace(@"\", @"\\") + "'");
  1940. }
  1941. }
  1942. }
  1943. }
  1944. else
  1945. {
  1946. //no sms pkg folder
  1947. }
  1948. }
  1949. }
  1950. catch { }
  1951. }
  1952. /// <summary>
  1953. /// Cleanup all Cache Folders where no entry is in the CacheInfo class (orphaned Folders)
  1954. /// </summary>
  1955. /// <example>
  1956. /// C#:
  1957. /// <code>
  1958. /// using System;
  1959. /// using System.Collections.Generic;
  1960. /// using System.Text;
  1961. /// using smsclictr.automation;
  1962. /// using System.Management;
  1963. ///
  1964. /// namespace ConsoleApplication1
  1965. /// {
  1966. /// class Program
  1967. /// {
  1968. /// static void Main(string[] args)
  1969. /// {
  1970. /// SMSClient oClient = new SMSClient("workstation01");
  1971. /// oClient.CacheCleanupOrphanedPackages();
  1972. /// }
  1973. /// }
  1974. /// }
  1975. /// </code>
  1976. /// PowerShell:
  1977. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  1978. /// <code>
  1979. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  1980. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  1981. /// $SMSClient.CacheCleanupOrphanedPackages()
  1982. /// </code>
  1983. /// </example>
  1984. public void CacheCleanupOrphanedPackages()
  1985. {
  1986. string sCachePath = CachePath;
  1987. ArrayList CacheFolder = oWMIFileIO.SubFolders(sCachePath);
  1988. foreach (string sFolder in CacheFolder)
  1989. {
  1990. try
  1991. {
  1992. ManagementObjectCollection MOC;
  1993. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  1994. oProv.mScope.Path.NamespacePath = @"root\ccm\SoftMgmtAgent";
  1995. if (this.SMSVersion.StartsWith("4"))
  1996. {
  1997. MOC = oProv.ExecuteQuery("SELECT * FROM CacheInfoEx where Location='" + sFolder.Replace(@"\", @"\\") + "'");
  1998. }
  1999. else
  2000. {
  2001. MOC = oProv.ExecuteQuery("SELECT * FROM CacheInfo where Location='" + sFolder.Replace(@"\", @"\\") + "'");
  2002. }
  2003. //Check if Package is listed in the CacheInfo Class
  2004. if (MOC.Count == 0)
  2005. {
  2006. //delete the orphaned Folder
  2007. try
  2008. {
  2009. oWMIFileIO.DeleteFolder(sFolder);
  2010. }
  2011. catch
  2012. { }
  2013. }
  2014. }
  2015. catch { }
  2016. }
  2017. }
  2018. /// <summary>
  2019. /// Cleanup all CacheInfo Objects where no Package-Folder exists
  2020. /// </summary>
  2021. /// <example>
  2022. /// C#:
  2023. /// <code>
  2024. /// using System;
  2025. /// using System.Collections.Generic;
  2026. /// using System.Text;
  2027. /// using smsclictr.automation;
  2028. /// using System.Management;
  2029. ///
  2030. /// namespace ConsoleApplication1
  2031. /// {
  2032. /// class Program
  2033. /// {
  2034. /// static void Main(string[] args)
  2035. /// {
  2036. /// SMSClient oClient = new SMSClient("workstation01");
  2037. /// oClient.CacheCleanupOrphanedCacheInfo();
  2038. /// }
  2039. /// }
  2040. /// }
  2041. /// </code>
  2042. /// PowerShell:
  2043. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  2044. /// <code>
  2045. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  2046. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  2047. /// $SMSClient.CacheCleanupOrphanedCacheInfo()
  2048. /// </code>
  2049. /// </example>
  2050. public void CacheCleanupOrphanedCacheInfo()
  2051. {
  2052. string sCachePath = CachePath;
  2053. ArrayList CacheFolder = oWMIFileIO.SubFolders(sCachePath);
  2054. ManagementObjectCollection MOC;
  2055. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  2056. oProv.mScope.Path.NamespacePath = @"root\ccm\SoftMgmtAgent";
  2057. if (this.SMSVersion.StartsWith("4"))
  2058. {
  2059. MOC = oProv.ExecuteQuery("SELECT * FROM CacheInfoEx");
  2060. }
  2061. else
  2062. {
  2063. MOC = oProv.ExecuteQuery("SELECT * FROM CacheInfo");
  2064. }
  2065. foreach (ManagementObject MO in MOC)
  2066. {
  2067. try
  2068. {
  2069. if (!CacheFolder.Contains(MO.GetPropertyValue("Location").ToString().ToLower()))
  2070. {
  2071. MO.Delete();
  2072. }
  2073. }
  2074. catch
  2075. { }
  2076. }
  2077. }
  2078. /// <summary>
  2079. /// Cleanup all Software Updates from Cache
  2080. /// </summary>
  2081. /// <example>
  2082. /// C#:
  2083. /// <code>
  2084. /// using System;
  2085. /// using System.Collections.Generic;
  2086. /// using System.Text;
  2087. /// using smsclictr.automation;
  2088. /// using System.Management;
  2089. ///
  2090. /// namespace ConsoleApplication1
  2091. /// {
  2092. /// class Program
  2093. /// {
  2094. /// static void Main(string[] args)
  2095. /// {
  2096. /// SMSClient oClient = new SMSClient("workstation01");
  2097. /// oClient.CacheCleanupPatches();
  2098. /// }
  2099. /// }
  2100. /// }
  2101. /// </code>
  2102. /// PowerShell:
  2103. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  2104. /// <code>
  2105. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  2106. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  2107. /// $SMSClient.CacheCleanupPatches()
  2108. /// </code>
  2109. /// </example>
  2110. public void CacheCleanupPatches()
  2111. {
  2112. try
  2113. {
  2114. string sCachePath = CachePath;
  2115. ArrayList CacheFolder = oWMIFileIO.SubFolders(sCachePath);
  2116. CacheFolder.Sort();
  2117. for (int i = 0; i < CacheFolder.Count - 1; i++)
  2118. {
  2119. string sFolder1 = CacheFolder[i].ToString();
  2120. System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(sFolder1);
  2121. string sPkgFolder1 = di.Name.ToString();
  2122. string[] aPkg1 = sPkgFolder1.Split('.');
  2123. if (aPkg1[0].Length > 8)
  2124. {
  2125. oWMIFileIO.DeleteFolder(CacheFolder[i].ToString());
  2126. try
  2127. {
  2128. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  2129. oProv.mScope.Path.NamespacePath = @"root\ccm\SoftMgmtAgent";
  2130. if (this.SMSVersion.StartsWith("4"))
  2131. {
  2132. oProv.DeleteQueryResults("SELECT * FROM CacheInfoEx where Location='" + CacheFolder[i].ToString().Replace(@"\", @"\\") + "'");
  2133. }
  2134. else
  2135. {
  2136. oProv.DeleteQueryResults("SELECT * FROM CacheInfo where Location='" + CacheFolder[i].ToString().Replace(@"\", @"\\") + "'");
  2137. }
  2138. }
  2139. catch(Exception ex)
  2140. {
  2141. ex.Message.ToString();
  2142. }
  2143. }
  2144. }
  2145. }
  2146. catch { }
  2147. }
  2148. /// <summary>
  2149. /// Cleanup orphaned and old cached packages
  2150. /// </summary>
  2151. /// <example>
  2152. /// C#:
  2153. /// <code>
  2154. /// using System;
  2155. /// using System.Collections.Generic;
  2156. /// using System.Text;
  2157. /// using smsclictr.automation;
  2158. /// using System.Management;
  2159. ///
  2160. /// namespace ConsoleApplication1
  2161. /// {
  2162. /// class Program
  2163. /// {
  2164. /// static void Main(string[] args)
  2165. /// {
  2166. /// SMSClient oClient = new SMSClient("workstation01");
  2167. /// oClient.CacheCleanupALL();
  2168. /// }
  2169. /// }
  2170. /// }
  2171. /// </code>
  2172. /// PowerShell:
  2173. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  2174. /// <code>
  2175. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  2176. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  2177. /// $SMSClient.CacheCleanupALL()
  2178. /// </code>
  2179. /// </example>
  2180. public void CacheCleanupALL()
  2181. {
  2182. CacheCleanupOrphanedPackages();
  2183. CacheCleanupOldPackages();
  2184. CacheCleanupOrphanedCacheInfo();
  2185. CacheCleanupPatches();
  2186. }
  2187. /// <summary>
  2188. /// ResetGlobalLoggingConfiguration to the default values
  2189. /// </summary>
  2190. /// <example>
  2191. /// C#:
  2192. /// <code>
  2193. /// using System;
  2194. /// using System.Collections.Generic;
  2195. /// using System.Text;
  2196. /// using smsclictr.automation;
  2197. /// using System.Management;
  2198. ///
  2199. /// namespace ConsoleApplication1
  2200. /// {
  2201. /// class Program
  2202. /// {
  2203. /// static void Main(string[] args)
  2204. /// {
  2205. /// SMSClient oClient = new SMSClient("workstation01");
  2206. /// oClient.ResetGlobalLoggingConfiguration();
  2207. /// }
  2208. /// }
  2209. /// }
  2210. /// </code>
  2211. /// PowerShell:
  2212. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  2213. /// <code>
  2214. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  2215. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  2216. /// $SMSClient.ResetGlobalLoggingConfiguration()
  2217. /// </code>
  2218. /// </example>
  2219. public void ResetGlobalLoggingConfiguration()
  2220. {
  2221. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  2222. oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  2223. oProv.ExecuteMethod("SMS_Client", "ResetGlobalLoggingConfiguration");
  2224. }
  2225. /// <summary>
  2226. /// Connect the IPC$ Share of the remote Host
  2227. /// </summary>
  2228. /// <example>
  2229. /// C#:
  2230. /// <code>
  2231. /// using System;
  2232. /// using System.Collections.Generic;
  2233. /// using System.Text;
  2234. /// using smsclictr.automation;
  2235. /// using System.Management;
  2236. ///
  2237. /// namespace ConsoleApplication1
  2238. /// {
  2239. /// class Program
  2240. /// {
  2241. /// static void Main(string[] args)
  2242. /// {
  2243. /// SMSClient oClient = new SMSClient("workstation01");
  2244. /// oClient.ConnectIPC();
  2245. /// }
  2246. /// }
  2247. /// }
  2248. /// </code>
  2249. /// PowerShell:
  2250. /// <para>(copy smsclictr.automation.dll to the %HOMEDRIVE%\%HOMEPATH% Folder of the current User)</para>
  2251. /// <code>
  2252. /// [void][System.Reflection.Assembly]::LoadFile("$HOME\smsclictr.automation.dll")
  2253. /// $SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("workstation01")
  2254. /// $SMSClient.ConnectIPC()
  2255. /// </code>
  2256. /// </example>
  2257. public void ConnectIPC()
  2258. {
  2259. CCMSetup CCM = new CCMSetup(this);
  2260. CCM.ConnectIPC();
  2261. }
  2262. /// <summary>
  2263. /// Delete the TrustedRootKey and the MP Certificate
  2264. /// </summary>
  2265. /// <example>
  2266. /// C#:
  2267. /// <code>
  2268. /// using System;
  2269. /// using System.Collections.Generic;
  2270. /// using System.Text;
  2271. /// using smsclictr.automation;
  2272. /// using System.Management;
  2273. ///
  2274. /// namespace ConsoleApplication1
  2275. /// {
  2276. /// class Program
  2277. /// {
  2278. /// static void Main(string[] args)
  2279. /// {
  2280. /// SMSClient oClient = new SMSClient("workstation01");
  2281. /// oClient.DeleteTrustedRootKey();
  2282. /// }
  2283. /// }
  2284. /// }
  2285. /// </code>
  2286. /// </example>
  2287. public void DeleteTrustedRootKey()
  2288. {
  2289. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  2290. oProv.mScope.Path.NamespacePath = @"ROOT\CCM\LocationServices";
  2291. try
  2292. {
  2293. ManagementObject MO = oProv.GetObject("TrustedRootKey=@");
  2294. MO.Delete();
  2295. }
  2296. catch
  2297. { }
  2298. }
  2299. /// <summary>
  2300. /// Delete SMS x509 Certificates
  2301. /// </summary>
  2302. /// <example>
  2303. /// C#:
  2304. /// <code>
  2305. /// using System;
  2306. /// using System.Collections.Generic;
  2307. /// using System.Text;
  2308. /// using smsclictr.automation;
  2309. /// using System.Management;
  2310. ///
  2311. /// namespace ConsoleApplication1
  2312. /// {
  2313. /// class Program
  2314. /// {
  2315. /// static void Main(string[] args)
  2316. /// {
  2317. /// SMSClient oClient = new SMSClient("workstation01");
  2318. /// oClient.DeleteSMSCertificates();
  2319. /// }
  2320. /// }
  2321. /// }
  2322. /// </code>
  2323. /// </example>
  2324. public void DeleteSMSCertificates()
  2325. {
  2326. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  2327. try
  2328. {
  2329. WMIRegistry oReg = new WMIRegistry(oProv);
  2330. foreach (string sCert in oReg.RegKeys(2147483650, @"SOFTWARE\Microsoft\SystemCertificates\SMS\Certificates"))
  2331. {
  2332. try
  2333. {
  2334. oReg.DeleteKey(2147483650, @"SOFTWARE\Microsoft\SystemCertificates\SMS\Certificates\" + sCert);
  2335. }
  2336. catch { }
  2337. }
  2338. }
  2339. catch
  2340. { }
  2341. }
  2342. /// <summary>
  2343. /// Reset paused SoftwareDistribution Flag
  2344. /// </summary>
  2345. /// <example>
  2346. /// C#:
  2347. /// <code>
  2348. /// using System;
  2349. /// using System.Collections.Generic;
  2350. /// using System.Text;
  2351. /// using smsclictr.automation;
  2352. /// using System.Management;
  2353. ///
  2354. /// namespace ConsoleApplication1
  2355. /// {
  2356. /// class Program
  2357. /// {
  2358. /// static void Main(string[] args)
  2359. /// {
  2360. /// SMSClient oClient = new SMSClient("workstation01");
  2361. /// oClient.ResetPausedSWDist();
  2362. /// }
  2363. /// }
  2364. /// }
  2365. /// </code>
  2366. /// </example>
  2367. public void ResetPausedSWDist()
  2368. {
  2369. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  2370. try
  2371. {
  2372. //Detect if remote System is x86 or x64
  2373. if (oWMIProvider.isX86)
  2374. {
  2375. WMIRegistry oReg = new WMIRegistry(oProv);
  2376. oReg.SetDWord(2147483650, @"SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\State", "Paused", "0");
  2377. oReg.SetDWord(2147483650, @"SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\State", "PausedCookie", "0");
  2378. }
  2379. else
  2380. {
  2381. WMIRegistry oReg = new WMIRegistry(oProv);
  2382. oReg.SetDWord(2147483650, @"SOFTWARE\Wow6432Node\Microsoft\SMS\Mobile Client\Software Distribution\State", "Paused", "0");
  2383. oReg.SetDWord(2147483650, @"SOFTWARE\Wow6432Node\Microsoft\SMS\Mobile Client\Software Distribution\State", "PausedCookie", "0");
  2384. }
  2385. }
  2386. catch
  2387. { }
  2388. }
  2389. /// <summary>
  2390. /// Delete Pending Site Assignment
  2391. /// </summary>
  2392. public void DeletePendingSiteAssignment()
  2393. {
  2394. WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
  2395. oProv.mScope.Path.NamespacePath = @"ROOT\CCM";
  2396. oProv.DeleteQueryResults("SELECT * FROM SMS_PendingSiteAssignment");
  2397. }
  2398. #endregion
  2399. /// <summary>
  2400. /// WMI Connection Settings (WMIProvider Class)
  2401. /// </summary>
  2402. public WMIProvider Connection { get { return oWMIProvider; } }
  2403. }
  2404. }