Batch file 为服务器列表设置PW永不过期

Batch file 为服务器列表设置PW永不过期,batch-file,powershell,vbscript,Batch File,Powershell,Vbscript,我希望为本地Windows用户帐户设置“密码永不过期”,为文本文件中的服务器列表设置密码。到目前为止,我在下面找到了这个命令行,但它只在单台计算机上本地工作。如何将其合并到VBscript、PowerShell或批处理文件中以应用于文本文件中的服务器列表 WMIC USERACCOUNT WHERE "Name='accountname'" SET PasswordExpires=FALSE 此代码应执行以下操作: # 1. Define in-line array of servers $S

我希望为本地Windows用户帐户设置“密码永不过期”,为文本文件中的服务器列表设置密码。到目前为止,我在下面找到了这个命令行,但它只在单台计算机上本地工作。如何将其合并到VBscript、PowerShell或批处理文件中以应用于文本文件中的服务器列表

WMIC USERACCOUNT WHERE "Name='accountname'" SET PasswordExpires=FALSE

此代码应执行以下操作:

# 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;
另一种方法是使用
调用命令
,但这需要您首先在环境中配置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)。

可以通过
/node
参数对远程主机运行:

wmic /node:HostA,HostB useraccount where "Name='accountname'" ...