Powershell verbundene Drucker von Active-Directory Computern auslesen

Wer einen Überblick benötigt, an welchen AD-Computer welcher Drucker angebunden ist, wird sich an diesem kleinen Script erfreuen. Das Script liest alle Computer der lokalen Active-Directory aus und exportiert die angebundenen Drucker nach erfolgreichen Pingcheck in eine CSV-Datei die später über Excel ausgewertet werden kann.

Das komplette Powershell Script

# get connected printers 
#
# This script creates a CSV-file with printers which were connected to ad-computers and sends the CSV-file via mail
#
#
#
$script_headline="get connected printers from "+$env:USERDNSDOMAIN

## e-mail notfication
    $email_server = “mail.mailserver.de”
    $email_receiver=@("placeholder@mathias-jaekel.de") # if this is empty, e-mail is disabled
    $email_sender="placeholder@mathias-jaekel.de "
    $email_subject=$script_headline


$ad_computer=Get-ADComputer -filter *
$csv_delimiter=";"
$csv_file="c:\temp\ad_printer.csv"

$skip_computers=@("CCTV","Time","Kiosk") #
$dataset="ComputerName;PrinterName;Shared;Published"

foreach($computer in $ad_computer.DNSHostName){

   if (Test-Connection -ComputerName $computer -count 1 -Quiet){

       $skip_computer=0

       foreach($searchcrit in $skip_computers){
        $checkresult=$computer -match $searchcrit

        if($checkresult){$skip_computer=1}

       }
        if($skip_computer -eq 0){

            write-host "- get printers from " $computer
            $printer_result=Get-Printer -ComputerName $computer

            foreach($printer in $printer_result){
                $dataset+=$printer.ComputerName+$csv_delimiter+$printer.Name+$csv_delimiter+$printer.shared+$csv_delimiter+$printer.Published+"`r`n"
            }
        }else{
            write-host "- skipped: " $computer
        }
    }else{
        write-host "- not pingable: "$computer
    }

}

$dataset  | set-content $csv_file

if($email_receiver.count -gt 0){
    foreach($receiver in $email_receiver){
        write-host "-send email to: "$receiver
        send-mailmessage -from $email_sender -to $receiver -subject $email_subject -Body "see attached backup" -Attachments $csv_file -dno onSuccess, onFailure -smtpServer $email_server
    }
}

weitere Beiträge zum Thema Powershell und Drucker