windows - 使用powershell修改本地账户

标签 windows powershell wmi

我正在尝试将一组服务器上的本地帐户的属性设置为“密码永不过期”。这是我能想到的最好的。我不断得到:

Get-WmiObject : Invalid parameter 
At C:\Users\xxxxxx\AppData\Local\Temp\4f06fa1c-61da-4c65-ac0b-a4167d83d51c.ps1:4 char:14
+ Get-WmiObject <<<<  -class Win32_UserAccount -Filter "name = 'localaccount'" -       ComputerName $server | Set-WmiInstance -Argument @{PasswordExpires = 0}
+ CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

------------这是我正在尝试的------------

$servers = Get-Item c:\list.txt
foreach ($server in $servers)
{
    Get-WmiObject -class Win32_UserAccount -Filter "name = 'localaccount'" -ComputerName $server | Set-WmiInstance -Argument @{PasswordExpires = 0}
}

谢谢!

最佳答案

你的错误在这一行:

$servers = Get-Item c:\list.txt

Get-Item cmdlet 返回一个FileInfo 对象,而不是文件的内容。要将内容读入变量,您需要 Get-Content cmdlet。

这应该有效:

Get-Content 'c:\list.txt' | % {
  gwmi Win32_UserAccount -Computer $_ -Filter "name='localaccount'" |
     Set-WmiInstance -Argument @{PasswordExpires = $false}
}

您也可以像这样更改属性 (source):

Get-Content 'c:\list.txt' | % {
  $account = gwmi Win32_UserAccount -Computer $_ -Filter "name='localaccount'"
  $account.PasswordExpires = $false
  $account.Put()
}

关于windows - 使用powershell修改本地账户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22234255/

相关文章:

Powershell 7Zip 压缩无法分配所需的内存

regex - 使用 PowerShell 将前导零添加到文件名

delphi - 无法读取 Windows 7 64 位上逻辑驱动器的最后几 Kb

vbscript - 如何使用 Vbscript 显示 WMI 类属性的描述?

sql-server - 将海量数据迁移到 SQL

c - 在 Windows 中,如何在 C 中跟踪子进程读取和写入的文件?

.net - Windows 应用程序图标文件

windows - 获取应用程序池的标识

c++ - 如何在 Windows 上从命令行使用 MinGW 编译器?

windows - 响应 native Windows 移动支持