The script looks like this:
$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
}
}
If I remove this part after New-Object
…
| Format-Table -AutoSize Server, Name | Out-File C:\Scripts\servers.txt
…the script works perfect. When adding the above mentioned line I get this error for all the servers / members it finds:
Exception calling "Invoke" with "2" argument(s): "The network path was
not found. " At C:\scripts\myscript.ps1:5 char:62
+ ([ADSI]"WinNT://$($_.Name)/Administrators").psbase.invoke <<<< ('Members') |
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Best Answer
One fix: the format and out need to follow the pipeline of the outer
Foreach-Object
:Second: the error is coming from the ADSI
Invoke
call, probably unable to resolve the machine name.