这段代码应该可以做到:
# 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 Remoting。
# 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)。