我正在编写一个 poweshell 脚本来查找我们域中系统的所有本地帐户。除了系统不可用的情况外,它运行良好。然后我收到以下错误:
Get-WmiObject : The RPC server is unavailable.
At C:\Scripts\List-LocalAccountsAllSystems.ps1:11 char:17
+ ... ountlist = (Get-WmiObject -Class Win32_UserAccount -ComputerName $sys ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
我尝试过多种方法让这些系统至少在 csv 中有一个条目,即使它只包含计算机名称,但都不起作用。这是所有代码。是的,我知道它很乱,csv 也很乱,但老实说,我不太在意,因为这只是他们给我们安排的繁重工作。另一个团队会关注结果,而不是我。
$systemlist = (get-adcomputer -filter *).name
foreach ($system in $systemlist){
write-host "prcessing $system"
$accountlist = (Get-WmiObject -Class Win32_UserAccount -ComputerName $system -Filter "LocalAccount=True").name
foreach ($account in $accountlist){
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name "Computer" -Value $System
$OutputObj | Add-Member -MemberType NoteProperty -Name "Local Account Name" -Value $account
$OutputObj | Export-Csv -path c:\temp\localaccountlist.csv -Append -NoTypeInformation
$account = $null
}
$system = $null
}
答案1
我在 GitHub 上找到了这个脚本,尤其是他的 try 和 catch 对你来说很有趣,可以处理无法访问的计算机。
https://github.com/lazywinadmin/PowerShell/blob/master/TOOL-Get-LocalUser/Get-LocalUser.ps1
function Get-LocalUser {
<#
.SYNOPSIS
This script can be list all of local user account.
.DESCRIPTION
This script can be list all of local user account.
The function is using WMI to connect to the remote machine
.PARAMETER ComputerName
Specifies the computers on which the command . The default is the local computer.
.PARAMETER Credential
A description of the Credential parameter.
.EXAMPLE
Get-LocalUser
This example shows how to list all of local users on local computer.
.NOTES
Francois-Xavier Cat
lazywinadmin.com
@lazywinadmin
.LINK
https://github.com/lazywinadmin/PowerShell
#>
PARAM
(
[Alias('cn')]
[String[]]$ComputerName = $Env:COMPUTERNAME,
[String]$AccountName,
[System.Management.Automation.PsCredential]$Credential
)
$Splatting = @{
Class = "Win32_UserAccount"
Namespace = "root\cimv2"
Filter = "LocalAccount='$True'"
}
#Credentials
If ($PSBoundParameters['Credential']) { $Splatting.Credential = $Credential }
Foreach ($Computer in $ComputerName) {
TRY {
Write-Verbose -Message "[PROCESS] ComputerName: $Computer"
Get-WmiObject @Splatting -ComputerName $Computer | Select-Object -Property Name, FullName, Caption, Disabled, Status, Lockout, PasswordChangeable, PasswordExpires, PasswordRequired, SID, SIDType, AccountType, Domain, Description
}
CATCH {
Write-Warning -Message "[PROCESS] Issue connecting to $Computer"
}
}
}