Download PowerShell-master/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs from MegaBites-AI/Windows-powershell: direct link, hf CLI and curl.
- Browser
- Download file 7.56 kB
-
https://huggingface.co/datasets/MegaBites-AI/Windows-powershell/resolve/main/PowerShell-master/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs
- Command line
-
hf download hf://datasets/MegaBites-AI/Windows-powershell/PowerShell-master/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs
-
curl -L -o Hotfix.cs https://huggingface.co/datasets/MegaBites-AI/Windows-powershell/resolve/main/PowerShell-master/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs
7.56 kB
| // Copyright (c) Microsoft Corporation. | |
| // Licensed under the MIT License. | |
| using System; | |
| using System.Diagnostics.CodeAnalysis; | |
| using System.Management; | |
| using System.Management.Automation; | |
| using System.Management.Automation.Internal; | |
| using System.Security.Principal; | |
| using System.Text; | |
| namespace Microsoft.PowerShell.Commands | |
| { | |
| /// <summary> | |
| /// Cmdlet for Get-Hotfix Proxy. | |
| /// </summary> | |
| [ | |
| ] | |
| [] | |
| public sealed class GetHotFixCommand : PSCmdlet, IDisposable | |
| { | |
| /// <summary> | |
| /// Specifies the HotFixID. Unique identifier associated with a particular update. | |
| /// </summary> | |
| [] | |
| [] | |
| [] | |
| [] | |
| public string[] Id { get; set; } | |
| /// <summary> | |
| /// To search on description of Hotfixes. | |
| /// </summary> | |
| [] | |
| [] | |
| [] | |
| public string[] Description { get; set; } | |
| /// <summary> | |
| /// Parameter to pass the Computer Name. | |
| /// </summary> | |
| [] | |
| [] | |
| [] | |
| [] | |
| public string[] ComputerName { get; set; } = new string[] { "localhost" }; | |
| /// <summary> | |
| /// Parameter to pass the Credentials. | |
| /// </summary> | |
| [] | |
| [] | |
| [] | |
| public PSCredential Credential { get; set; } | |
| private ManagementObjectSearcher _searchProcess; | |
| private bool _inputContainsWildcard = false; | |
| private readonly ConnectionOptions _connectionOptions = new(); | |
| /// <summary> | |
| /// Sets connection options. | |
| /// </summary> | |
| protected override void BeginProcessing() | |
| { | |
| _connectionOptions.Authentication = AuthenticationLevel.Packet; | |
| _connectionOptions.Impersonation = ImpersonationLevel.Impersonate; | |
| _connectionOptions.Username = Credential?.UserName; | |
| _connectionOptions.SecurePassword = Credential?.Password; | |
| } | |
| /// <summary> | |
| /// Get the List of HotFixes installed on the Local Machine. | |
| /// </summary> | |
| protected override void ProcessRecord() | |
| { | |
| foreach (string computer in ComputerName) | |
| { | |
| bool foundRecord = false; | |
| StringBuilder queryString = new(); | |
| ManagementScope scope = new(ComputerWMIHelper.GetScopeString(computer, ComputerWMIHelper.WMI_Path_CIM), _connectionOptions); | |
| scope.Connect(); | |
| if (Id != null) | |
| { | |
| queryString.Append("Select * from Win32_QuickFixEngineering where ("); | |
| for (int i = 0; i <= Id.Length - 1; i++) | |
| { | |
| queryString.Append("HotFixID= '"); | |
| queryString.Append(Id[i].Replace("'", "\\'")); | |
| queryString.Append('\''); | |
| if (i < Id.Length - 1) | |
| { | |
| queryString.Append(" Or "); | |
| } | |
| } | |
| queryString.Append(')'); | |
| } | |
| else | |
| { | |
| queryString.Append("Select * from Win32_QuickFixEngineering"); | |
| foundRecord = true; | |
| } | |
| _searchProcess = new ManagementObjectSearcher(scope, new ObjectQuery(queryString.ToString())); | |
| foreach (ManagementObject obj in _searchProcess.Get()) | |
| { | |
| if (Description != null) | |
| { | |
| if (!FilterMatch(obj)) | |
| { | |
| continue; | |
| } | |
| } | |
| else | |
| { | |
| _inputContainsWildcard = true; | |
| } | |
| // try to translate the SID to a more friendly username | |
| // just stick with the SID if anything goes wrong | |
| string installed = (string)obj["InstalledBy"]; | |
| if (!string.IsNullOrEmpty(installed)) | |
| { | |
| try | |
| { | |
| SecurityIdentifier secObj = new(installed); | |
| obj["InstalledBy"] = secObj.Translate(typeof(NTAccount)); | |
| } | |
| catch (IdentityNotMappedException) | |
| { | |
| // thrown by SecurityIdentifier.Translate | |
| } | |
| catch (SystemException) | |
| { | |
| // thrown by SecurityIdentifier.constr | |
| } | |
| } | |
| WriteObject(obj); | |
| foundRecord = true; | |
| } | |
| if (!foundRecord && !_inputContainsWildcard) | |
| { | |
| Exception ex = new ArgumentException(StringUtil.Format(HotFixResources.NoEntriesFound, computer)); | |
| WriteError(new ErrorRecord(ex, "GetHotFixNoEntriesFound", ErrorCategory.ObjectNotFound, null)); | |
| } | |
| if (_searchProcess != null) | |
| { | |
| this.Dispose(); | |
| } | |
| } | |
| } | |
| /// <summary> | |
| /// To implement ^C. | |
| /// </summary> | |
| protected override void StopProcessing() | |
| { | |
| _searchProcess?.Dispose(); | |
| } | |
| private bool FilterMatch(ManagementObject obj) | |
| { | |
| try | |
| { | |
| foreach (string desc in Description) | |
| { | |
| WildcardPattern wildcardpattern = WildcardPattern.Get(desc, WildcardOptions.IgnoreCase); | |
| if (wildcardpattern.IsMatch((string)obj["Description"])) | |
| { | |
| return true; | |
| } | |
| if (WildcardPattern.ContainsWildcardCharacters(desc)) | |
| { | |
| _inputContainsWildcard = true; | |
| } | |
| } | |
| } | |
| catch (Exception) | |
| { | |
| return false; | |
| } | |
| return false; | |
| } | |
| /// <summary> | |
| /// Release all resources. | |
| /// </summary> | |
| public void Dispose() | |
| { | |
| _searchProcess?.Dispose(); | |
| } | |
| } | |
| } | |