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

August
  • 3,114
  • 16
  • 17
Sabeltiger
  • 1
  • 1
  • 3
  • 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

1 Answers1

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.

Richard
  • 5,324
  • 1
  • 23
  • 20
  • 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 08: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`](http://technet.microsoft.com/en-us/library/hh847796.aspx). – Richard Jan 23 '13 at 09: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
  • I really can't figure out where to put the two lines `Write-Debug`and `$DebugPreferences`. I have tried to insert them above `Foreach-Object`and Above `New Object` also beneath the `Format-table` line. – Sabeltiger Jan 23 '13 at 12:25
  • @Sabeltiger You set the debug preference once (in the PSH instance you're using for developing the script) and `Write-Debug` whereever you want to see what's going on. Developing a script is an iterative process, perhaps dropping the inner loop for a while, just returning server name + user count to the pipeline to ensure you are getting something. In other words take very small steps one at a time: this is basic debugging approach (google for "printf debugging") – Richard Jan 23 '13 at 15:32