【问题标题】:Set PW never expire for a list of server为服务器列表设置 PW 永不过期
【发布时间】:2014-01-18 01:09:18
【问题描述】:

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

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

【问题讨论】:

    标签: batch-file powershell vbscript


    【解决方案1】:

    这段代码应该可以做到:

    # 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)。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2011-03-18
        • 2012-06-28
        • 2020-10-20
        • 1970-01-01
        • 1970-01-01
        • 2012-05-25
        • 2013-10-13
        • 1970-01-01
        • 2021-07-29
        相关资源
        最近更新 更多