使用命令行查看给定计算机的本地来宾帐户是否被禁用

我需要生成一个报告,显示给定的计算机列表的访客帐户被禁用。

我如何使用net userPowerShell,或任何其他常用的工具为此目的?

这里有一个小PowerShell函数来检查这个。

 function Test-LocalAccountDisabled { param ( [string] $AccountName = 'Guest', [string[]] $ComputerName = $env:COMPUTERNAME ) $AccountDisable=0x0002 foreach ($Computer in $ComputerName) { [ADSI]$Guest="WinNT://$Computer/$AccountName,User" if ($Guest -ne $null) { New-Object PSObject -Property @{ Disabled = ($Guest.UserFlags.Value -band $AccountDisable) -as [boolean] AccountName = $AccountName ComputerName = $Computer } } else { Write-Error "Unable to find $AccountName on $Computer." } } } 

如果你有一个文本文件中的换行符分隔的计算机列表,你可以做类似的事情

 Test-LocalAccountDisabled -ComputerName (get-content computers.txt) 

PowerShell可能是最简单的方法:

 foreach ( $computer in (Get-Content computers.txt) ) { Get-WmiObject Win32_UserAccount -Computer $computer -Filter "Name = 'guest'" ` | Select-Object __Server, Disabled } 

批量使用wmic是丑陋的,但也会起作用:

 set query=useraccount where name^^="guest" get disabled for /f %c in ('computers.txt') do ( for /f "delims== tokens=2" %a in ('wmic /node:%c %query% /value') do ( echo %c %a ) ) 

一个像这样的东西Powershell脚本应该做的伎俩:

 $Servers = Get-Content "C:\Path\To\File\With\Servers.txt" foreach ($Server in $Servers) { Get-WmiObject Win32_UserAccount -computername $Server -filter "LocalAccount=True AND` Name='Guest'" | Select-Object Domain,Name,Disabled } 

这将从文本文件读取服务器名称列表,并循环显示每个禁用的访客帐户的条目。 如果您取出AND Name=Guest ,它会在每台机器上显示所有禁用的帐户。