none
Why does variable act differently when filtering the source? RRS feed

  • Question

  • I have a script to pull disk statistics from servers.  I have it written to feed a variable like this and the script runs great:

    $computers = get-adcomputer -filter * -searchbase "ou=servers,dc=domain,dc=local"

    I'd like to change it to filter on operating system but when I do this, I get RPC server unavailable error, meaning when i send the variable to the next step it doesn't have the information.

    $computers = get-adcomputer -filter {operatingsystem -like "windows server*"} -properties operatingsystem

    Any idea why this happens, or how I can get the filtered server objects into the variable?  Both statements run fine and feed results in the same format when run alone...

    Full script here:

    <#
    .Synopsis
       Gets disk space statistics from adcomputer
    .DESCRIPTION
       Uses OU to define what machines to look at, pulls disk statistics.  Returns a txt file with results.
    .EXAMPLE
       Modify searchbase in script to control what you look at.  
    
    #>
    
    $report = @()
    # $computers = get-adcomputer -filter * -searchbase "ou=servers,dc=domain,dc=local"
      #or
    $computers = get-adcomputer -filter {operatingsystem -like "windows server*"} -properties operatingsystem
    
    
    foreach ($computer in $computers) { 
        $report += "`n"
        $report += $computer.DNSHostName + "`n"
        $drives=Get-WmiObject Win32_LogicalDisk -ComputerName $computer.dnshostname
    
            foreach ($drive in $drives){
                
                $drivename=$drive.DeviceID
                $freespace=[int]($drive.FreeSpace/1GB)
                $totalspace=[int]($drive.Size/1GB)
                $usedspace=$totalspace - $freespace
                $pctused=if ($totalspace -eq 0) {0} else {[decimal](($usedspace/$totalspace)*100)}
                $report +=$output+$drivename+"`t`t"+$usedspace+"`t`t`t"+$freespace+ "`t`t`t" +$totalspace+ "`t`t`t" +$pctused+ "`n"
        }
        }
    
        $Report = $report | Out-String 
        $report | out-file c:\temp\diskspace_report.txt
    


    Jim Tall

    • Moved by Bill_Stewart Wednesday, September 4, 2019 7:48 PM This is not "design solution for me" forum
    Tuesday, April 2, 2019 8:33 PM

All replies

  • ps.  i get the same bad result when piping to a 'where' instead of using the filter

    Jim Tall

    Tuesday, April 2, 2019 8:35 PM
  • You need to try a different DC.  The issue is a server/domain issue and has nothing to do with PowerShell.


    \_(ツ)_/

    Tuesday, April 2, 2019 9:01 PM