0

I'm trying to script powershell to reach out to all AD Computers and pull a list of shares. I'm seeing some strange behavior with Get-WMIObject.

The script:

Import-Module ActiveDirectory

$Computers = Get-ADComputer -Filter * -SearchBase "Some OU" |
    Where-Object {
        $_.Name -match "-LT$" -or
        $_.Name -match "-PC$" -or
        $_.Name -match "-VPC$" 
    } |
    Select-Object Name -ExpandProperty Name |
    Sort

$Computers | ForEach-Object {
    Write-Host $_
    Write-Host "========================="
    Get-WMIObject -Class Win32_Share -Computer $_ 
}

Normally, the output from the gwmi command looks like this:

Name       Path          Description
----       ----          -----------
ADMIN$     C:\Windows    Remote Admin
C$         C:\           Default share
IPC$                     Remote IPC

But instead, for that same computer I get this output:

...

[Computername]
=========================
Name   Path                              Description
----   ----                              -----------
ADMIN$ C:\WINDOWS                        Remote Admin
C$     C:\                               Default share
IPC$                                     Remote IPC
print$ C:\WINDOWS\system32\spool\drivers Printer Drivers
ADMIN$ C:\WINDOWS                        Remote Admin
C$     C:\                               Default share
IPC$                                     Remote IPC
print$ C:\WINDOWS\system32\spool\drivers Printer Drivers
ADMIN$ C:\Windows                        Remote Admin
C$     C:\                               Default share
IPC$                                     Remote IPC

...

These two outputs are from the same computer. I've also output the computer name list, and I'm not missing a computer, so I don't think that they were combined.

Any clues?

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    you are mixing _direct_ & _indirect_ screen writes. PoSh delays indirect screen writes for about 300ms to see if they can be grouped. ///// the fix = [1] don't mix them. [*grin*] use the same stream for everything. [2] add `| Out-Host` to the indirect writes to force them into direct writes. – Lee_Dailey Apr 09 '19 at 22:18
  • 1
    Run the command against all of the computers in a single command, and pipe to Select. `gwmi -Class Win32__Share -Computer $Computers|Select PSComputerName,Name,Path,Description` and see if that works for you. – TheMadTechnician Apr 09 '19 at 22:58
  • Lee_Dailey's point fixed it. – Austin_Manuel Apr 17 '19 at 17:32

0 Answers0