1

Need a little help finishing off my first Powershell scriptlet...

I would like to create a single CSV file of all the XP PC's on my AD domain (approx 3,000) and record all the printers attached. Ultimately I'd like to know how many printers we have that are locally attached.

My "Googled" together script will happily scan all the PC's using WMI for the printers and output to the screen the results. However, I can't seem to be able to refactor the code to output the results to CSV (via Export-CSV) that won't just keep overwriting the file. I want it to append to the file.

# Get the list of computer names from AD
$strCategory = "computer"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.CacheResults = -1
$objSearcher.PageSize = 5000
$objSearcher.SizeLimit = 5000

$objSearcher.Filter = ("(&(operatingSystemVersion=5*)(objectCategory=$strCategory)(operatingSystem=Windows XP*))")

$wmiPrinterClass = "win32_printer"

$colProplist = "name"
foreach ($i in $colPropList)
    {$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()
$colResults.count

# for each computer, use WMI to scan for printers
foreach ($objComputer in $colResults)
    {
    $machine = $objComputer.Properties.name
    $machine
    get-WmiObject -class $wmiPrinterClass -computername $machine | `
      ft systemName, name, shareName, Local -auto | `
      Export-CSV "c:\printers.csv"  #<<< HOW DO I STOP THIS FROM OVERWRITING???
    }

The script as it stands will scan AD for all the XP PC's and then execute a remote WMI search on Win32_Printer. How do I get all of this information into a single CSV file?

Desperatuss0ccus
  • 252
  • 1
  • 4
  • 9
Guy
  • 2,668
  • 2
  • 20
  • 24

1 Answers1

2

You want the "-noClobber" option to ExportCSV it appears.

http://ss64.com/ps/export-csv.html

I also did an alternative solution that might work:

# Get the list of computer names from AD
$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.CacheResults = -1
$objSearcher.PageSize = 5000
$objSearcher.SizeLimit = 5000
$objSearcher.PropertiesToLoad.Add("name")
$objSearcher.Filter = ("(&(operatingSystemVersion=5*)(objectCategory=computer)(operatingSystem=Windows XP*))")

$output = @()

# for each computer, use WMI to scan for printers
foreach ($objComputer in $objSearcher.FindAll())
        {
        $output += get-WmiObject -class "win32_printer" -computername $objComputer.Properties.name | ft systemName, name, shareName, Local -auto
        }

Export-CSV "c:\printers.csv" -inputObject $output
LapTop006
  • 6,496
  • 20
  • 26
  • -noClobber only stops you from overwriting an existing file. It does not enable "append to the file" writing. But the $output += and exporting $output will work. Just testing it now... Ta – Guy Aug 07 '09 at 15:42