[玩转系统] 使用 PowerShell 设置本地用户帐户
作者:精品下载站 日期:2024-12-14 07:42:06 浏览:16 分类:玩电脑
使用 PowerShell 设置本地用户帐户
首先,我要指出的是,使用 PowerShell 管理远程计算机上的本地用户帐户实际上非常容易。您需要做的就是学习如何使用 NET USER 命令并使用 Invoke-Command 执行它。
invoke-command { net user } -computername chi-core01
invoke-command { net user localadmin } -computername chi-core01
CHI-CORE01 上的 LocalAdmin 帐户当前已禁用(帐户活动等于否)。但启用和设置新密码非常容易。
invoke-command { net user localadmin P@ssw0rd /active:Yes } -computername chi-core01
但是,这不能很好地扩展,并且 NET USER 命令的功能可能会因操作系统而异。因此,这是一个利用 ADSI 执行相同操作的 PowerShell 函数。
#requires -version 2.0
Function Set-LocalUserAccount {
<#
.SYNOPSIS
Enable or disable a local user account.
.DESCRIPTION
This command will allow you to set the password of a local user account as well
as enable or disable it. By default, this command will not write anything to
the pipeline unless you use -Passthru. You must run this under credentials
that have administrator rights on the remote computer.
.PARAMETER ComputerName
The name of the computer to connect to. This parameter has an alias of CN.
.PARAMETER UserName
The name of the local user account on the computer.
.PARAMETER Password
The new password to set. This parameter has an alias of PWD.
.PARAMETER Status
Enable or disable the local user account.
.PARAMETER Passthru
Write the user account object to the pipeline
.EXAMPLE
PS C:\> Set-LocalUserAccount SERVER01,SERVER02 DBAdmin -status disable
Disable the local user account DBAdmin on SERVER01 and SERVER02
.EXAMPLE
PS C:\> get-content c:\work\computers.txt | set-localuseraccount LocalAdmin -password "^Crx33t7A"
Sets the password for account LocalAdmin on all computers in computers.txt
.NOTES
Version: 1.0
Author : Jeff Hicks (@JeffHicks)
Learn more:
PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones2/)
PowerShell Deep Dives (http://manning.com/hicks/)
Learn PowerShell 3 in a Month of Lunches (http://manning.com/jones3/)
Learn PowerShell Toolmaking in a Month of Lunches (http://manning.com/jones4/)
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
.INPUTS
String
.OUTPUTS
None or System.DirectoryServices.DirectoryEntry
#>
[cmdletbinding(SupportsShouldProcess=$True)]
Param (
[Parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[ValidateNotNullorEmpty()]
[Alias("cn")]
[string[]]$ComputerName=$env:COMPUTERNAME,
[Parameter(Position=1,Mandatory=$True,
HelpMessage="What is the name of the local user account?",
ValueFromPipelineByPropertyName=$True)]
[ValidateNotNullorEmpty()]
[string]$UserName,
[Parameter(ValueFromPipelineByPropertyName=$True)]
[Alias("pwd")]
[string]$Password,
[ValidateSet("Enable","Disable")]
[string]$Status="Enable",
[switch]$Passthru
)
Begin {
Write-Verbose "Starting $($myinvocation.mycommand)"
#define a constant to disable or enable an account
New-Variable ADS_UF_ACCOUNTDISABLE 0x0002 -Option Constant
Write-Verbose "Setting local user account $username"
} #begin
Process {
foreach ($computer in $computername) {
Write-Verbose "Connecting to $computer"
Write-Verbose "Getting user account"
$Account = [ADSI]"WinNT://$computer/$username,user"
#validate the user account was found
if (-NOT $Account.path) {
Write-Warning "Failed to find $username on $computername"
#bail out
Return
}
#Get current enabled/disabled status
if ($Account.userflags.value -band $ADS_UF_ACCOUNTDISABLE) {
$Enabled = $False
}
else {
$Enabled = $True
}
Write-verbose "Account enabled is $Enabled"
if ($enabled -AND ($Status -eq "Disable")) {
Write-Verbose "disabling the account"
$value=$Account.userflags.value -bor $ADS_UF_ACCOUNTDISABLE
$Account.put("userflags",$value)
}
elseif ((-NOT $enabled) -AND ($Status -eq "Enable")) {
Write-Verbose "Enabling the account"
$value=$Account.userflags.value -bxor $ADS_UF_ACCOUNTDISABLE
$Account.put("userflags",$value)
}
else {
#account is already in the desired state
Write-Verbose "No change necessary"
}
if ($Password) {
Write-Verbose "Setting acccount password"
$Account.SetPassword($Password)
}
#Whatif
if ($PSCmdlet.ShouldProcess("$computer$username")) {
Write-Verbose "Committing changes"
$Account.SetInfo()
}
if ($Passthru) {
Write-Verbose "Passing object to the pipeline"
$Account
}
} #foreach
} #process
End {
Write-Verbose "Ending $($myinvocation.mycommand)"
} #end
} #end Set-LocalUserAccount function
此函数应该适用于 PowerShell 2.0 及更高版本。帮助内容包括一些使用示例。您可以使用此命令简单地更改用户密码,或者在启用或禁用帐户的同时更改密码。启用和禁用是通过对 userflags 值和指示帐户已禁用的常量标志进行按位运算来完成的。
可能还有更多内容可以添加到命令中,例如设置评论属性以及帐户何时过期。但我现在将这些更改留给您。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag