"Fossies" - the Fresh Open Source Software Archive

Member "PowerShell-7.2.6/src/Microsoft.PowerShell.LocalAccounts/LocalAccounts/Commands/AddLocalGroupMemberCommand.cs" (11 Aug 2022, 10155 Bytes) of package /linux/misc/PowerShell-7.2.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AddLocalGroupMemberCommand.cs" see the Fossies "Dox" file reference documentation.

    1 // Copyright (c) Microsoft Corporation.
    2 // Licensed under the MIT License.
    3 
    4 #region Using directives
    5 using System;
    6 using System.Collections.Generic;
    7 using System.Management.Automation;
    8 using System.Security.Principal;
    9 
   10 using System.Management.Automation.SecurityAccountsManager;
   11 using System.Management.Automation.SecurityAccountsManager.Extensions;
   12 
   13 using Microsoft.PowerShell.LocalAccounts;
   14 using System.Diagnostics.CodeAnalysis;
   15 #endregion
   16 
   17 namespace Microsoft.PowerShell.Commands
   18 {
   19     /// <summary>
   20     /// The Add-LocalGroupMember cmdlet adds one or more users or groups to a local
   21     /// group.
   22     /// </summary>
   23     [Cmdlet(VerbsCommon.Add, "LocalGroupMember",
   24             SupportsShouldProcess = true,
   25             HelpUri = "https://go.microsoft.com/fwlink/?LinkId=717987")]
   26     [Alias("algm")]
   27     public class AddLocalGroupMemberCommand : PSCmdlet
   28     {
   29         #region Instance Data
   30         private Sam sam = null;
   31         #endregion Instance Data
   32 
   33         #region Parameter Properties
   34         /// <summary>
   35         /// The following is the definition of the input parameter "Group".
   36         /// Specifies a security group from the local Security Accounts Manager.
   37         /// </summary>
   38         [Parameter(Mandatory = true,
   39                    Position = 0,
   40                    ParameterSetName = "Group")]
   41         [ValidateNotNull]
   42         public Microsoft.PowerShell.Commands.LocalGroup Group
   43         {
   44             get { return this.group;}
   45 
   46             set { this.group = value; }
   47         }
   48 
   49         private Microsoft.PowerShell.Commands.LocalGroup group;
   50 
   51         /// <summary>
   52         /// The following is the definition of the input parameter "Member".
   53         /// Specifies one or more users or groups to add to this local group. You can
   54         /// identify users or groups by specifying their names or SIDs, or by passing
   55         /// Microsoft.PowerShell.Commands.LocalPrincipal objects.
   56         /// </summary>
   57         [Parameter(Mandatory = true,
   58                    Position = 1,
   59                    ValueFromPipeline = true,
   60                    ValueFromPipelineByPropertyName = true)]
   61         [ValidateNotNullOrEmpty]
   62         [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
   63         public Microsoft.PowerShell.Commands.LocalPrincipal[] Member
   64         {
   65             get { return this.member;}
   66 
   67             set { this.member = value; }
   68         }
   69 
   70         private Microsoft.PowerShell.Commands.LocalPrincipal[] member;
   71 
   72         /// <summary>
   73         /// The following is the definition of the input parameter "Name".
   74         /// Specifies a security group from the local Security Accounts Manager.
   75         /// </summary>
   76         [Parameter(Mandatory = true,
   77                    Position = 0,
   78                    ParameterSetName = "Default")]
   79         [ValidateNotNullOrEmpty]
   80         public string Name
   81         {
   82             get { return this.name;}
   83 
   84             set { this.name = value; }
   85         }
   86 
   87         private string name;
   88 
   89         /// <summary>
   90         /// The following is the definition of the input parameter "SID".
   91         /// Specifies a security group from the local Security Accounts Manager.
   92         /// </summary>
   93         [Parameter(Mandatory = true,
   94                    Position = 0,
   95                    ParameterSetName = "SecurityIdentifier")]
   96         [ValidateNotNull]
   97         public System.Security.Principal.SecurityIdentifier SID
   98         {
   99             get { return this.sid;}
  100 
  101             set { this.sid = value; }
  102         }
  103 
  104         private System.Security.Principal.SecurityIdentifier sid;
  105         #endregion Parameter Properties
  106 
  107         #region Cmdlet Overrides
  108         /// <summary>
  109         /// BeginProcessing method.
  110         /// </summary>
  111         protected override void BeginProcessing()
  112         {
  113             sam = new Sam();
  114         }
  115 
  116         /// <summary>
  117         /// ProcessRecord method.
  118         /// </summary>
  119         protected override void ProcessRecord()
  120         {
  121             try
  122             {
  123                 if (Group != null)
  124                     ProcessGroup(Group);
  125                 else if (Name != null)
  126                     ProcessName(Name);
  127                 else if (SID != null)
  128                     ProcessSid(SID);
  129             }
  130             catch (GroupNotFoundException ex)
  131             {
  132                 WriteError(ex.MakeErrorRecord());
  133             }
  134         }
  135 
  136         /// <summary>
  137         /// EndProcessing method.
  138         /// </summary>
  139         protected override void EndProcessing()
  140         {
  141             if (sam != null)
  142             {
  143                 sam.Dispose();
  144                 sam = null;
  145             }
  146         }
  147         #endregion Cmdlet Overrides
  148 
  149         #region Private Methods
  150 
  151         /// <summary>
  152         /// Creates a list of <see cref="LocalPrincipal"/> objects
  153         /// ready to be processed by the cmdlet.
  154         /// </summary>
  155         /// <param name="groupId">
  156         /// Name or SID (as a string) of the group we'll be adding to.
  157         /// This string is used primarily for specifying the target
  158         /// in WhatIf scenarios.
  159         /// </param>
  160         /// <param name="member">
  161         /// LocalPrincipal object to be processed
  162         /// </param>
  163         /// <returns>
  164         /// A LocalPrincipal Object to be added to the group
  165         /// </returns>
  166         /// <remarks>
  167         /// <para>
  168         /// LocalPrincipal objects in the Member parameter may not be complete,
  169         /// particularly those created from a name or a SID string given to the
  170         /// Member cmdlet parameter. The object returned from this method contains
  171         /// , at the very least, a valid SID.
  172         /// </para>
  173         /// <para>
  174         /// Any Member objects provided by name or SID string will be looked up
  175         /// to ensure that such an object exists. If an object is not found,
  176         /// an error message is displayed by PowerShell and null will be returned
  177         /// </para>
  178         /// <para>
  179         /// This method also handles the WhatIf scenario. If the Cmdlet's
  180         /// <b>ShouldProcess</b> method returns false on any Member object,
  181         /// that object will not be included in the returned List.
  182         /// </para>
  183         /// </remarks>
  184         private LocalPrincipal MakePrincipal(string groupId, LocalPrincipal member)
  185         {
  186             LocalPrincipal principal = null;
  187             // if the member has a SID, we can use it directly
  188             if (member.SID != null)
  189             {
  190                 principal = member;
  191             }
  192             else    // otherwise it must have been constructed by name
  193             {
  194                 SecurityIdentifier sid = this.TrySid(member.Name);
  195 
  196                 if (sid != null)
  197                 {
  198                     member.SID = sid;
  199                     principal = member;
  200                 }
  201                 else
  202                 {
  203                     try
  204                     {
  205                         principal = sam.LookupAccount(member.Name);
  206                     }
  207                     catch (Exception ex)
  208                     {
  209                         WriteError(ex.MakeErrorRecord());
  210                     }
  211                 }
  212             }
  213 
  214             if (CheckShouldProcess(principal, groupId))
  215                 return principal;
  216 
  217             return null;
  218         }
  219 
  220         /// <summary>
  221         /// Determine if a principal should be processed.
  222         /// Just a wrapper around Cmdlet.ShouldProcess, with localized string
  223         /// formatting.
  224         /// </summary>
  225         /// <param name="principal">Name of the principal to be added.</param>
  226         /// <param name="groupName">
  227         /// Name of the group to which the members will be added.
  228         /// </param>
  229         /// <returns>
  230         /// True if the principal should be processed, false otherwise.
  231         /// </returns>
  232         private bool CheckShouldProcess(LocalPrincipal principal, string groupName)
  233         {
  234             if (principal == null)
  235                 return false;
  236 
  237             string msg = StringUtil.Format(Strings.ActionAddGroupMember, principal.ToString());
  238 
  239             return ShouldProcess(groupName, msg);
  240         }
  241 
  242         /// <summary>
  243         /// Add members to a group.
  244         /// </summary>
  245         /// <param name="group">
  246         /// A <see cref="LocalGroup"/> object representing the group to which
  247         /// the members will be added.
  248         /// </param>
  249         private void ProcessGroup(LocalGroup group)
  250         {
  251             string groupId = group.Name ?? group.SID.ToString();
  252             foreach (var member in this.Member)
  253             {
  254                 LocalPrincipal principal = MakePrincipal(groupId, member);
  255                 if (principal != null)
  256                 {
  257                     var ex = sam.AddLocalGroupMember(group, principal);
  258                     if (ex != null)
  259                     {
  260                         WriteError(ex.MakeErrorRecord());
  261                     }
  262                 }
  263             }
  264         }
  265 
  266         /// <summary>
  267         /// Add members to a group specified by name.
  268         /// </summary>
  269         /// <param name="name">
  270         /// The name of the group to which the members will be added.
  271         /// </param>
  272         private void ProcessName(string name)
  273         {
  274             ProcessGroup(sam.GetLocalGroup(name));
  275         }
  276 
  277         /// <summary>
  278         /// Add members to a group specified by SID.
  279         /// </summary>
  280         /// <param name="groupSid">
  281         /// A <see cref="SecurityIdentifier"/> object identifying the group
  282         /// to which the members will be added.
  283         /// </param>
  284         private void ProcessSid(SecurityIdentifier groupSid)
  285         {
  286             foreach (var member in this.Member)
  287             {
  288                 LocalPrincipal principal = MakePrincipal(groupSid.ToString(), member);
  289                 if (principal != null)
  290                 {
  291                     var ex = sam.AddLocalGroupMember(groupSid, principal);
  292                     if (ex != null)
  293                     {
  294                         WriteError(ex.MakeErrorRecord());
  295                     }
  296                 }
  297             }
  298         }
  299 
  300         #endregion Private Methods
  301     }
  302 
  303 }
  304