Windows-powershell / PowerShell-master /src /Microsoft.PowerShell.LocalAccounts /LocalAccounts /Commands /NewLocalGroupCommand.cs
| // Copyright (c) Microsoft Corporation. | |
| // Licensed under the MIT License. | |
| using System; | |
| using System.Management.Automation; | |
| using System.Management.Automation.SecurityAccountsManager; | |
| using System.Management.Automation.SecurityAccountsManager.Extensions; | |
| using Microsoft.PowerShell.LocalAccounts; | |
| namespace Microsoft.PowerShell.Commands | |
| { | |
| /// <summary> | |
| /// The New-LocalGroup Cmdlet can be used to create a new local security group | |
| /// in the Windows Security Accounts Manager. | |
| /// </summary> | |
| [ | |
| ] | |
| [] | |
| public class NewLocalGroupCommand : Cmdlet | |
| { | |
| private Sam sam = null; | |
| /// <summary> | |
| /// The following is the definition of the input parameter "Description". | |
| /// A descriptive comment. | |
| /// </summary> | |
| [] | |
| [] | |
| public string Description | |
| { | |
| get { return this.description;} | |
| set { this.description = value; } | |
| } | |
| private string description; | |
| /// <summary> | |
| /// The following is the definition of the input parameter "Name". | |
| /// The group name for the local security group. | |
| /// </summary> | |
| [ | |
| ] | |
| [] | |
| [] | |
| public string Name | |
| { | |
| get { return this.name;} | |
| set { this.name = value; } | |
| } | |
| private string name; | |
| /// <summary> | |
| /// BeginProcessing method. | |
| /// </summary> | |
| protected override void BeginProcessing() | |
| { | |
| sam = new Sam(); | |
| } | |
| /// <summary> | |
| /// ProcessRecord method. | |
| /// </summary> | |
| protected override void ProcessRecord() | |
| { | |
| try | |
| { | |
| if (CheckShouldProcess(Name)) | |
| { | |
| var group = sam.CreateLocalGroup(new LocalGroup | |
| { | |
| Description = Description, | |
| Name = Name | |
| }); | |
| WriteObject(group); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| WriteError(ex.MakeErrorRecord()); | |
| } | |
| } | |
| /// <summary> | |
| /// EndProcessing method. | |
| /// </summary> | |
| protected override void EndProcessing() | |
| { | |
| if (sam != null) | |
| { | |
| sam.Dispose(); | |
| sam = null; | |
| } | |
| } | |
| private bool CheckShouldProcess(string target) | |
| { | |
| return ShouldProcess(target, Strings.ActionNewGroup); | |
| } | |
| } | |
| } | |