Windows-powershell / PowerShell-master /src /Microsoft.PowerShell.LocalAccounts /LocalAccounts /Commands /GetLocalGroupCommand.cs
| // Copyright (c) Microsoft Corporation. | |
| // Licensed under the MIT License. | |
| using System; | |
| using System.Management.Automation; | |
| using System.Security.Principal; | |
| using System.Management.Automation.SecurityAccountsManager; | |
| using System.Management.Automation.SecurityAccountsManager.Extensions; | |
| using System.Diagnostics.CodeAnalysis; | |
| namespace Microsoft.PowerShell.Commands | |
| { | |
| /// <summary> | |
| /// The Get-LocalGroup cmdlet gets local groups from the Windows Security | |
| /// Accounts manager. | |
| /// </summary> | |
| [ | |
| ] | |
| [] | |
| public class GetLocalGroupCommand : Cmdlet | |
| { | |
| private Sam sam = null; | |
| /// <summary> | |
| /// The following is the definition of the input parameter "Name". | |
| /// Specifies the local groups to get from the local Security Accounts Manager. | |
| /// </summary> | |
| [ | |
| ] | |
| [] | |
| [] | |
| public string[] Name | |
| { | |
| get { return this.name; } | |
| set { this.name = value; } | |
| } | |
| private string[] name; | |
| /// <summary> | |
| /// The following is the definition of the input parameter "SID". | |
| /// Specifies a local group from the local Security Accounts Manager. | |
| /// </summary> | |
| [ | |
| ] | |
| [] | |
| [] | |
| public System.Security.Principal.SecurityIdentifier[] SID | |
| { | |
| get { return this.sid;} | |
| set { this.sid = value; } | |
| } | |
| private System.Security.Principal.SecurityIdentifier[] sid; | |
| /// <summary> | |
| /// BeginProcessing method. | |
| /// </summary> | |
| protected override void BeginProcessing() | |
| { | |
| sam = new Sam(); | |
| } | |
| /// <summary> | |
| /// ProcessRecord method. | |
| /// </summary> | |
| protected override void ProcessRecord() | |
| { | |
| if (Name == null && SID == null) | |
| { | |
| foreach (var group in sam.GetAllLocalGroups()) | |
| WriteObject(group); | |
| return; | |
| } | |
| ProcessNames(); | |
| ProcessSids(); | |
| } | |
| /// <summary> | |
| /// EndProcessing method. | |
| /// </summary> | |
| protected override void EndProcessing() | |
| { | |
| if (sam != null) | |
| { | |
| sam.Dispose(); | |
| sam = null; | |
| } | |
| } | |
| /// <summary> | |
| /// Process groups requested by -Name. | |
| /// </summary> | |
| /// <remarks> | |
| /// All arguments to -Name will be treated as names, | |
| /// even if a name looks like a SID. | |
| /// Groups may be specified using wildcards. | |
| /// </remarks> | |
| private void ProcessNames() | |
| { | |
| if (Name != null) | |
| { | |
| foreach (var name in Name) | |
| { | |
| try | |
| { | |
| if (WildcardPattern.ContainsWildcardCharacters(name)) | |
| { | |
| var pattern = new WildcardPattern(name, WildcardOptions.Compiled | |
| | WildcardOptions.IgnoreCase); | |
| foreach (var group in sam.GetMatchingLocalGroups(n => pattern.IsMatch(n))) | |
| WriteObject(group); | |
| } | |
| else | |
| { | |
| WriteObject(sam.GetLocalGroup(name)); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| WriteError(ex.MakeErrorRecord()); | |
| } | |
| } | |
| } | |
| } | |
| /// <summary> | |
| /// Process groups requested by -SID. | |
| /// </summary> | |
| private void ProcessSids() | |
| { | |
| if (SID != null) | |
| { | |
| foreach (var sid in SID) | |
| { | |
| try | |
| { | |
| WriteObject(sam.GetLocalGroup(sid)); | |
| } | |
| catch (Exception ex) | |
| { | |
| WriteError(ex.MakeErrorRecord()); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |