# Mappage des imprimantes réseaux avec un fichier texte # =========================================== # powershell getprint.ps1 # # Exemple : # --------- # powershell getprint.ps1 "printer.ini" # # =========================================== # # Par GBO le 17/07/2013 # # =========================================== # Récupération du chemin du fichier Param([parameter(Mandatory=$True)][String]$ini_file) # ============================================================================================= # # Fonctions # # ============================================================================================= # function affiche_imprimante_message ([string]$ligne_courante, [Bool]$standard, [Bool]$resultat) { If ($standard -eq $true) { if ($resultat -eq $true) { Write-Host $lignes[$i]" ==> Imprimante par défaut installée" -foregroundcolor green } else { Write-Host $lignes[$i]" ==> Imprimante par défaut non installée" -foregroundcolor red } } else { if ($resultat -eq $true) { Write-Host $lignes[$i]" ==> Imprimante installée" -foregroundcolor green } else { Write-Host $lignes[$i]" ==> Imprimante non installée" -foregroundcolor red } } } function affiche_aide { Write-Host "Nombre d'arguments incorrect !`n" -foregroundcolor yellow Write-Host "Exemple : powershell getprint.ps1 printer.ini`n Le fichier d'affectation de l'imprimante est un fichier texte qui contient toutes les informations d'affectation de l'imprimante. Pour chaque imprimante affectée à un poste de travail une ligne avec la syntaxe suivante doit exister.`n ,,, `tnom netbios de l'ordinateur `t`t`tport de connexion de l'imprimante `t`tdéfinition de l'imprimante par défaut `tchemin UNC de l'imprimante`n"-foregroundcolor red Write-Host "Le caractère * en début de ligne est un caractère d'échappement" -foregroundcolor yellow Exit } # ============================================================================================= # # Programme principal # # ============================================================================================= # # Déclaration de la variable $net permettant d'installer les imprimantes $net = new-Object -com WScript.Network # Si le nom du fichier = NULL ou = help, on affiche l'aide if(($ini_file -eq "") -or ($ini_file -eq "--help")) { affiche_aide } # Récupération du nom de l'ordinateur $computername = $env:computername # Vérification de la présence du fichier $test_file = Test-Path $ini_file if ($test_file -ne $True) { affiche_aide } # Démontage des imprimantes réseaux Get-WMIObject Win32_Printer | where{$_.Network -eq $true} | foreach{$_.delete()} # Extraction des lignes contenant le nom de l'ordinateur sans le caractère d'échappement "*" $lignes = Select-String -Path $ini_file -Pattern $computername -Context 0 |ForEach-Object {$_.Line} # Lecture de chaque ligne For($i=0;$i -le ($lignes.length - 1);$i++) { # Si la ligne lue contient le mot clé "STANDARD" if ($lignes[$i].Contains("STANDARD")) { # Extraction du nom de l'imprimante $printer_name = $lignes[$i].substring(23) # Affectation de l'imprimante en tant qu'imprimante par défaut $net.AddWindowsPrinterConnection($printer_name) $net.SetDefaultPrinter($printer_name) affiche_imprimante_message $lignes[$i] $true $? } else { # Extraction du nom de l'imprimante $printer_name = $lignes[$i].substring(15) # Affectation de l'imprimante $net.AddWindowsPrinterConnection($printer_name) affiche_imprimante_message $lignes[$i] $false $? } }