我希望为本地Windows用户帐户设置“密码永不过期”,以获取文本文件中的服务器列表。到目前为止,我在下面找到了这个命令行,但它只在单台计算机上运行。如何将其合并到VBscript,PowerShell或批处理文件中以应用于文本文件中的服务器列表?
WMIC USERACCOUNT WHERE "Name='accountname'" SET PasswordExpires=FALSE
答案 0 :(得分:0)
此代码应该这样做:
# 1. Define in-line array of servers
$ServerList = @('localhost', 'localhost', 'localhost');
# 2. Define account name
$AccountName = 'test';
# 3. For each server, set the account to expire
foreach ($Server in $ServerList) {
$Account = Get-WmiObject -ComputerName $Server -Class Win32_UserAccount -Filter "Name = '$AccountName'";
$Account.PasswordExpires = $false;
[void] $Account.Put();
}
如果您想导入包含服务器名称的文本文件,您只需将第一行更改为:
$ServerList = Get-Content -Path c:\path\to\text\file.txt;
另一种方法是使用Invoke-Command
,但这需要您首先在您的环境中配置PowerShell远程处理。
# 1. Define in-line array of servers
$ServerList = @('localhost', 'localhost', 'localhost');
# 2. Define the block of code to deploy (a PowerShell ScriptBlock)
$ScriptBlock = {
$AccountName = 'test';
$Account = Get-WmiObject -Class Win32_UserAccount -Filter "Name = '$AccountName'";
$Account.PasswordExpires = $false;
[void] $Account.Put();
};
# 3. Deploy the ScriptBlock to the array of servers
Invoke-Command -ComputerName $ServerList -ScriptBlock $ScriptBlock;
要配置PowerShell远程处理,请在每台计算机上运行Enable-PSRemoting -Force
命令。您还可以使用Active Directory组策略启用PowerShell远程处理/ Windows远程管理(WinRM)。
答案 1 :(得分:0)
wmic
可以通过/node
参数对远程主机运行:
wmic /node:HostA,HostB useraccount where "Name='accountname'" ...