0

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

  • remove the "Format-Table" part from the pipeline. That is only used for viewing in the console and will screw up stuff that comes after it. – August Jan 22 '13 at 12:15
  • @August: A format- before an out- often makes sense (write the file in a specific format). – Richard Jan 22 '13 at 13:10
0

One fix: the format and out need to follow the pipeline of the outer 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

Second: the error is coming from the ADSI Invoke call, probably unable to resolve the machine name.

  • I hate to say it, but adding the Out-file line as in the above script does not work for me or PowerShell. when adding the Out-File path it writes the error already posted, which is my problem when adding the Out-file path. – Sabeltiger Jan 22 '13 at 14:37
  • I suggest lots of Write-Debug and setting $DebugPreference to Continue to check things like the path passed to ADSI. (I think outputing to the host is likely hiding the error: scrolling off the top: using AutoSize means all the data needs to be read first so any errors will be before any output.) – Richard Jan 22 '13 at 16:14
  • And how exactly will the Write-Debug and DebugPreference be added to the script? I guess that $DebugPreference is a variable that you put something in, perhaps the ADSI path itself? – Sabeltiger Jan 23 '13 at 8:51
  • @Sabeltiger eg. Write-Debug "This is the path: $thePath" in the script to show the values of things and state. See get-help about_preference_variables. – Richard Jan 23 '13 at 9:13
  • You mentioned that errors would be shown before any output because of the Autosize. Does that mean that it trie to contact the server and if not successful then it will write the error output first and the server it tried to reach, beneath the error output? like this: Server-path not available and next line "Server 31" or will Server 31 be on the line above the error log in PowerShell? – Sabeltiger Jan 23 '13 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.