修改本地用户(禁用或启用)- function Set-LocalUser
- {
- <#
- .Synopsis
- Enables or disables a local user
- .Description
- This function enables or disables a local user
- .Example
- Set-LocalUser -userName ed -disable
- Disables a local user account named ed
- .Example
- Set-LocalUser -userName ed -password Password
- Enables a local user account named ed and gives it the password password
- .Parameter UserName
- The name of the user to either enable or disable
- .Parameter Password
- The password of the user once it is enabled
- .Parameter Description
- A description to associate with the user account
- .Parameter Enable
- Enables the user account
- .Parameter Disable
- Disables the user account
- .Parameter ComputerName
- The name of the computer on which to perform the action
- .Notes
- NAME: Set-LocalUser
- AUTHOR: ed wilson, msft
- LASTEDIT: 06/29/2011 12:40:43
- KEYWORDS: Local Account Management, Users
- HSG: HSG-6-30-2011
- .Link
- Http://www.ScriptingGuys.com/blog
- #Requires -Version 2.0
- #>
- [CmdletBinding()]
- Param(
- [Parameter(Position=0,
- Mandatory=$True,
- ValueFromPipeline=$True)]
- [string]$userName,
- [Parameter(Position=1,
- Mandatory=$True,
- ValueFromPipeline=$True,
- ParameterSetName='EnableUser')]
- [string]$password,
- [Parameter(ParameterSetName='EnableUser')]
- [switch]$enable,
- [Parameter(ParameterSetName='DisableUser')]
- [switch]$disable,
- [string]$computerName = $env:ComputerName,
- [string]$description = "modified via powershell"
- )
- $EnableUser = 512 # ADS_USER_FLAG_ENUM enumeration value from SDK
- $DisableUser = 2 # ADS_USER_FLAG_ENUM enumeration value from SDK
- $User = [ADSI]"WinNT://$computerName/$userName,User"
-
- if($enable)
- {
- $User.setpassword($password)
- $User.description = $description
- $User.userflags = $EnableUser
- $User.setinfo()
- } #end if enable
- if($disable)
- {
- $User.description = $description
- $User.userflags = $DisableUser
- $User.setinfo()
- } #end if disable
- } #end function Set-LocalUser
复制代码
|