脚本如下:
$searchOU='ou=Servers,dc=mydomain,dc=NET'
Get-ADComputer -filter * -SearchBase $searchOU |
Foreach-Object {
$server = $_.Name
([ADSI]"WinNT://$($_.Name)/Administrators").psbase.invoke('Members') |
ForEach-Object {
$user = $_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)
New-Object 'PSObject' -property @{'Server'=$server; 'Admin'=$user} | Format-Table -AutoSize Server, Name | Out-File C:\Scripts\servers.txt
}
}
如果我之后删除此部分New-Object
...
| Format-Table -AutoSize Server, Name | Out-File C:\Scripts\servers.txt
...脚本运行完美。添加上述行时,我收到此错误,它找到的所有服务器/成员均出现此错误:
使用“2”个参数调用“Invoke”时发生异常:“未找到网络路径。”位于 C:\scripts\myscript.ps1:5 char:62 + ([ADSI]"WinNT://$($_.Name)/Administrators").psbase.invoke <<<< ('Members') | + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
答案1
一个解决方法:格式和输出需要遵循外部的管道Foreach-Object
:
$searchOU='ou=Servers,dc=mydomain,dc=NET'
Get-ADComputer -filter * -SearchBase $searchOU |
Foreach-Object {
$server = $_.Name
([ADSI]"WinNT://$($_.Name)/Administrators").psbase.invoke('Members') |
ForEach-Object {
$user = $_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)
New-Object 'PSObject' -property @{'Server'=$server; 'Admin'=$user}
}
} | Format-Table -AutoSize Server, Name | Out-File C:\Scripts\servers.txt
第二:错误来自ADSIInvoke
调用,可能无法解析机器名。