# Mappage des imprimantes réseaux avec un fichier texte # =========================================== # powershell getprint.ps1 # # Exemple : # --------- # powershell getprint.ps1 "printer.ini" # # =========================================== # Syntaxe du fichier printer.ini # # nom_ordinateur,STANDARD,chemin_unc_imprimante # STANDARD = permet de définir l'imprimante # comme imprimante par défaut # # =========================================== # # Par GBO le 17/07/2013 # # =========================================== # Effacer l'invite de commande Clear-Host # Récupération du chemin du fichier Param([parameter(Mandatory=$True)][AllowEmptyString()][String]$ini_file) # Si le nom du fichier = NULL ou = help, on affiche l'aide if(($ini_file -eq "") -or ($ini_file -eq "--help")) { Write-Host "Nombre d'arguments incorrect ! Exemple : powershell getprint.ps1 printer.ini 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. ,,, `tnom netbios de l'ordinateur `tdéfinition de l'imprimante par défaut `tchemin UNC de l'imprimante Le caractère * en début de ligne est un caractère d'échappement"-foregroundcolor red Exit } # Récupération du nom de l'ordinateur $computername = gc env:computername # Vérification de la présence du fichier $test_file = Test-Path $ini_file if ($test_file -ne "True") { Write-Host "Nombre d'arguments incorrect ! Exemple : powershell getprint.ps1 printer.ini 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. ,,, `tnom netbios de l'ordinateur `t`t`t`tport de connexion de l'imprimante `tdéfinition de l'imprimante par défaut `tchemin UNC de l'imprimante Le caractère * en début de ligne est un caractère d'échappement"-foregroundcolor red EXIT } # 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 lu 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 = new-Object -com WScript.Network $net.AddWindowsPrinterConnection($printer_name) $net.SetDefaultPrinter($printer_name) if ($? -eq True) { Write-Host $lignes[$i] + "==> imprimante par défaut OK" } else { Write-Warning $lignes[$i] + "==> imprimante non installée par défaut" } } else { # Extraction du nom de l'imprimante $printer_name = $lignes[$i].substring(15) # Affectation de l'imprimante $net = new-Object -com WScript.Network $net.AddWindowsPrinterConnection($printer_name) if ($? -eq True) { Write-Host $lignes[$i] + "==> imprimante OK" } else { Write-Warning $lignes[$i] + "==> imprimante non installée" } } } # ===========================================================================================