PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs

https://gitlab.com/unofficial-mirrors/PowerShell
C# | 239 lines | 175 code | 21 blank | 43 comment | 19 complexity | 27296ccf149625b5262b221eb3136926 MD5 | raw file
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. using System;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Collections.Specialized;
  10. using System.Diagnostics; // Process class
  11. using System.ComponentModel; // Win32Exception
  12. using System.Globalization;
  13. using System.Runtime.Serialization;
  14. using System.Security.Permissions;
  15. using System.Threading;
  16. using System.Management;
  17. using System.Management.Automation;
  18. using System.Management.Automation.Internal;
  19. using System.Diagnostics.CodeAnalysis;
  20. using System.Net;
  21. using System.IO;
  22. using System.Security;
  23. using System.Security.Principal;
  24. using System.Security.AccessControl;
  25. using Dbg = System.Management.Automation;
  26. namespace Microsoft.PowerShell.Commands
  27. {
  28. #region Get-HotFix
  29. /// <summary>
  30. /// Cmdlet for Get-Hotfix Proxy
  31. /// </summary>
  32. [Cmdlet(VerbsCommon.Get, "HotFix", DefaultParameterSetName = "Default",
  33. HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135217", RemotingCapability = RemotingCapability.SupportedByCommand)]
  34. [OutputType(@"System.Management.ManagementObject#root\cimv2\Win32_QuickFixEngineering")]
  35. public sealed class GetHotFixCommand : PSCmdlet, IDisposable
  36. {
  37. #region Parameters
  38. /// <summary>
  39. /// Specifies the HotFixID. Unique identifier associated with a particular update.
  40. /// </summary>
  41. [Parameter(Position = 0, ParameterSetName = "Default")]
  42. [ValidateNotNullOrEmpty]
  43. [Alias("HFID")]
  44. [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
  45. public string[] Id { get; set; }
  46. /// <summary>
  47. /// To search on description of Hotfixes
  48. /// </summary>
  49. [Parameter(ParameterSetName = "Description")]
  50. [ValidateNotNullOrEmpty]
  51. [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
  52. public string[] Description { get; set; }
  53. /// <summary>
  54. /// Parameter to pass the Computer Name
  55. /// </summary>
  56. [Parameter(ValueFromPipelineByPropertyName = true)]
  57. [ValidateNotNullOrEmpty]
  58. [Alias("CN", "__Server", "IPAddress")]
  59. [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
  60. public string[] ComputerName { get; set; } = new string[] { "localhost" };
  61. /// <summary>
  62. /// Parameter to pass the Credentials.
  63. /// </summary>
  64. [Parameter]
  65. [Credential]
  66. [ValidateNotNullOrEmpty]
  67. public PSCredential Credential { get; set; }
  68. #endregion Parameters
  69. #region Overrides
  70. private ManagementObjectSearcher _searchProcess;
  71. private bool _inputContainsWildcard = false;
  72. /// <summary>
  73. /// Get the List of HotFixes installed on the Local Machine.
  74. /// </summary>
  75. protected override void BeginProcessing()
  76. {
  77. foreach (string computer in ComputerName)
  78. {
  79. bool foundRecord = false;
  80. StringBuilder QueryString = new StringBuilder();
  81. ConnectionOptions conOptions = ComputerWMIHelper.GetConnectionOptions(AuthenticationLevel.Packet, ImpersonationLevel.Impersonate, this.Credential);
  82. ManagementScope scope = new ManagementScope(ComputerWMIHelper.GetScopeString(computer, ComputerWMIHelper.WMI_Path_CIM), conOptions);
  83. scope.Connect();
  84. if (Id != null)
  85. {
  86. QueryString.Append("Select * from Win32_QuickFixEngineering where (");
  87. for (int i = 0; i <= Id.Length - 1; i++)
  88. {
  89. QueryString.Append("HotFixID= '");
  90. QueryString.Append(Id[i].ToString().Replace("'", "\\'"));
  91. QueryString.Append("'");
  92. if (i < Id.Length - 1)
  93. QueryString.Append(" Or ");
  94. }
  95. QueryString.Append(")");
  96. }
  97. else
  98. {
  99. QueryString.Append("Select * from Win32_QuickFixEngineering");
  100. foundRecord = true;
  101. }
  102. _searchProcess = new ManagementObjectSearcher(scope, new ObjectQuery(QueryString.ToString()));
  103. foreach (ManagementObject obj in _searchProcess.Get())
  104. {
  105. if (Description != null)
  106. {
  107. if (!FilterMatch(obj))
  108. continue;
  109. }
  110. else
  111. {
  112. _inputContainsWildcard = true;
  113. }
  114. // try to translate the SID to a more friendly username
  115. // just stick with the SID if anything goes wrong
  116. string installed = (string)obj["InstalledBy"];
  117. if (!String.IsNullOrEmpty(installed))
  118. {
  119. try
  120. {
  121. SecurityIdentifier secObj = new SecurityIdentifier(installed);
  122. obj["InstalledBy"] = secObj.Translate(typeof(NTAccount)); ;
  123. }
  124. catch (IdentityNotMappedException) // thrown by SecurityIdentifier.Translate
  125. {
  126. }
  127. catch (SystemException) // thrown by SecurityIdentifier.constr
  128. {
  129. }
  130. //catch (ArgumentException) // thrown (indirectly) by SecurityIdentifier.constr (on XP only?)
  131. //{ catch not needed - this is already caught as SystemException
  132. //}
  133. //catch (PlatformNotSupportedException) // thrown (indirectly) by SecurityIdentifier.Translate (on Win95 only?)
  134. //{ catch not needed - this is already caught as SystemException
  135. //}
  136. //catch (UnauthorizedAccessException) // thrown (indirectly) by SecurityIdentifier.Translate
  137. //{ catch not needed - this is already caught as SystemException
  138. //}
  139. }
  140. WriteObject(obj);
  141. foundRecord = true;
  142. }
  143. if (!foundRecord && !_inputContainsWildcard)
  144. {
  145. Exception Ex = new ArgumentException(StringUtil.Format(HotFixResources.NoEntriesFound, computer));
  146. WriteError(new ErrorRecord(Ex, "GetHotFixNoEntriesFound", ErrorCategory.ObjectNotFound, null));
  147. }
  148. if (_searchProcess != null)
  149. {
  150. this.Dispose();
  151. }
  152. }
  153. }//end of BeginProcessing method
  154. /// <summary>
  155. /// to implement ^C
  156. /// </summary>
  157. protected override void StopProcessing()
  158. {
  159. if (_searchProcess != null)
  160. {
  161. _searchProcess.Dispose();
  162. }
  163. }
  164. #endregion Overrides
  165. #region "Private Methods"
  166. private bool FilterMatch(ManagementObject obj)
  167. {
  168. try
  169. {
  170. foreach (string desc in Description)
  171. {
  172. WildcardPattern wildcardpattern = WildcardPattern.Get(desc, WildcardOptions.IgnoreCase);
  173. if (wildcardpattern.IsMatch((string)obj["Description"]))
  174. {
  175. return true;
  176. }
  177. if (WildcardPattern.ContainsWildcardCharacters(desc))
  178. {
  179. _inputContainsWildcard = true;
  180. }
  181. }
  182. }
  183. catch (Exception)
  184. {
  185. return false;
  186. }
  187. return false;
  188. }
  189. #endregion "Private Methods"
  190. #region "IDisposable Members"
  191. /// <summary>
  192. /// Dispose Method
  193. /// </summary>
  194. public void Dispose()
  195. {
  196. this.Dispose(true);
  197. // Use SuppressFinalize in case a subclass
  198. // of this type implements a finalizer.
  199. GC.SuppressFinalize(this);
  200. }
  201. /// <summary>
  202. /// Dispose Method.
  203. /// </summary>
  204. /// <param name="disposing"></param>
  205. public void Dispose(bool disposing)
  206. {
  207. if (disposing)
  208. {
  209. if (_searchProcess != null)
  210. {
  211. _searchProcess.Dispose();
  212. }
  213. }
  214. }
  215. #endregion "IDisposable Members"
  216. }//end class
  217. #endregion
  218. }//Microsoft.Powershell.commands