Windows-powershell / PowerShell-master /src /Microsoft.PowerShell.LocalAccounts /LocalAccounts /Commands /DisableLocalUserCommand.cs
| // Copyright (c) Microsoft Corporation. | |
| // Licensed under the MIT License. | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Management.Automation; | |
| using System.Management.Automation.SecurityAccountsManager; | |
| using System.Management.Automation.SecurityAccountsManager.Extensions; | |
| using Microsoft.PowerShell.LocalAccounts; | |
| using System.Diagnostics.CodeAnalysis; | |
| namespace Microsoft.PowerShell.Commands | |
| { | |
| /// <summary> | |
| /// The Disable-LocalUser cmdlet disables local user accounts. When a user | |
| /// account is disabled, the user is not permitted to log on. When a user | |
| /// account is enabled, the user is permitted to log on normally. | |
| /// </summary> | |
| [ | |
| ] | |
| [] | |
| public class DisableLocalUserCommand : Cmdlet | |
| { | |
| private const Enabling enabling = Enabling.Disable; | |
| private Sam sam = null; | |
| /// <summary> | |
| /// The following is the definition of the input parameter "InputObject". | |
| /// Specifies the of the local user accounts to disable in the local Security | |
| /// Accounts Manager. | |
| /// </summary> | |
| [ | |
| ] | |
| [] | |
| [] | |
| public Microsoft.PowerShell.Commands.LocalUser[] InputObject | |
| { | |
| get { return this.inputobject; } | |
| set { this.inputobject = value; } | |
| } | |
| private Microsoft.PowerShell.Commands.LocalUser[] inputobject; | |
| /// <summary> | |
| /// The following is the definition of the input parameter "Name". | |
| /// Specifies the names of the local user accounts to disable in 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 the LocalUser accounts to disable by | |
| /// System.Security.Principal.SecurityIdentifier. | |
| /// </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() | |
| { | |
| try | |
| { | |
| ProcessUsers(); | |
| ProcessNames(); | |
| ProcessSids(); | |
| } | |
| catch (Exception ex) | |
| { | |
| WriteError(ex.MakeErrorRecord()); | |
| } | |
| } | |
| /// <summary> | |
| /// EndProcessing method. | |
| /// </summary> | |
| protected override void EndProcessing() | |
| { | |
| if (sam != null) | |
| { | |
| sam.Dispose(); | |
| sam = null; | |
| } | |
| } | |
| /// <summary> | |
| /// Process users requested by -Name. | |
| /// </summary> | |
| /// <remarks> | |
| /// All arguments to -Name will be treated as names, | |
| /// even if a name looks like a SID. | |
| /// </remarks> | |
| private void ProcessNames() | |
| { | |
| if (Name != null) | |
| { | |
| foreach (var name in Name) | |
| { | |
| try | |
| { | |
| if (CheckShouldProcess(name)) | |
| sam.EnableLocalUser(sam.GetLocalUser(name), enabling); | |
| } | |
| catch (Exception ex) | |
| { | |
| WriteError(ex.MakeErrorRecord()); | |
| } | |
| } | |
| } | |
| } | |
| /// <summary> | |
| /// Process users requested by -SID. | |
| /// </summary> | |
| private void ProcessSids() | |
| { | |
| if (SID != null) | |
| { | |
| foreach (var sid in SID) | |
| { | |
| try | |
| { | |
| if (CheckShouldProcess(sid.ToString())) | |
| sam.EnableLocalUser(sid, enabling); | |
| } | |
| catch (Exception ex) | |
| { | |
| WriteError(ex.MakeErrorRecord()); | |
| } | |
| } | |
| } | |
| } | |
| /// <summary> | |
| /// Process users requested by -InputObject. | |
| /// </summary> | |
| private void ProcessUsers() | |
| { | |
| if (InputObject != null) | |
| { | |
| foreach (var user in InputObject) | |
| { | |
| try | |
| { | |
| if (CheckShouldProcess(user.Name)) | |
| sam.EnableLocalUser(user, enabling); | |
| } | |
| catch (Exception ex) | |
| { | |
| WriteError(ex.MakeErrorRecord()); | |
| } | |
| } | |
| } | |
| } | |
| private bool CheckShouldProcess(string target) | |
| { | |
| return ShouldProcess(target, Strings.ActionDisableUser); | |
| } | |
| } | |
| } | |