1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| # déclaration des variables
$a = new-object -comobject wscript.shell
# déclaration des fonctions
function get-supprListe_imprimante.txt
{
Remove-Item "$Env:userprofile\Liste_imprimante.txt"
}
Function get-MsgBoxalreadyinstall
{
$Msgboxalreadyinstall = $a.popup("Pilotes d'impression déjà installé",5,"Information",1)
}
Function get-MsgBoxinstallOK
{
$MsgboxinstallOK = $a.popup("Installation des pilotes d'impression réussi",5,"Information",1)
}
Function get-installedDrivers
{
if ($args[0] -ne $Null)
{$serveur = $args[0]}
else
{$serveur = "."}
$liste=@()
$collection =Get-WmiObject -computer $serveur win32_Printerdriver | select name,version, supportedplatform
foreach ($drivers in $collection)
{ $liste+=($drivers.name -split ",")[0]} #split pour afficher juste le nom
$file=new-item "$Env:userprofile\Liste_imprimante.txt" -type file -force
Add-Content -Path $file -Value $liste
}
Function Get-InstalledPrinterDrivers ()
{
param ( [string] $ComputerName = $env:COMPUTERNAME )
$ComputerName = $env:COMPUTERNAME
$colItems = get-wmiobject -class "Win32_PrinterDriver " -namespace "root\CIMV2" -computername $ComputerName
$PrintDrivers = @()
foreach ($objItem in $colItems) {
#creating a new object called $PrinterInfo
$Driver = New-Object psobject
$Driver | Add-Member NoteProperty Name $objItem.Name.Split(",")[0]
$Driver | Add-Member NoteProperty Chip $objItem.Name.Split(",")[2]
$PrintDrivers += $Driver
}
return $PrintDrivers
}
Function Install-PrinterDriver {
param (
[string]$ComputerName = $env:COMPUTERNAME,
[string]$DriverName,
[string]$DriverPath,
[string]$INFFileName,
$aff = 1)
$ComputerName = $env:COMPUTERNAME
$InstalledDrivers = Get-InstalledPrinterDrivers -ComputerName $ComputerName
$DriverFound = $InstalledDrivers | where {$_.Name -eq $DriverName}
if($DriverFound -eq $null) {
if ($aff) {
Write-Host ("Ce pilote d'impression " + $DriverName + " n'est pas installé sur " + $ComputerName ) -ForegroundColor Red
Write-Host ("Installation du pilote " + $DriverName + " sur " + $ComputerName) -ForegroundColor Green
}
$NewPrinterDriver = [wmiclass]"\\$ComputerName\ROOT\cimv2:Win32_PrinterDriver"
$NewPrinterDriver.Scope.Options.EnablePrivileges = $true
$objDriver = $NewPrinterDriver.createinstance()
$objDriver.Name=$DriverName
if ($DriverPath -ne "" -and $DriverPath -ne $null) {
$objDriver.DriverPath=$DriverPath
$objDriver.Infname= $DriverPath +"\" + $INFFileName
}
$objDriver.Version = "3"
$objDriver.SupportedPlatform = "Windows x64"
$ResultDrv = $NewPrinterDriver.AddPrinterDriver($objDriver)
#$ResultDrv = $NewPrinterDriver.Put()
if ($aff) {
Switch ($ResultDrv.ReturnValue)
{
0 {Write-Host "Success : $DriverName"}
5 {Write-Host "Access denied : $DriverName";Break}
1797 {Write-Host "The printer driver is unknown : $DriverName";Break}
default {Write-Host "Erreur : $($ResultDrv.ReturnValue)";Break}
}
}
return $ResultDrv
}
Else {
if ($aff) {
Write-Host ("Ce pilote d'impression " + $DriverName + " est déja installé sur " + $ComputerName) -ForegroundColor Green
}
}
}
#=========================================================================
#=========================================================================
# MAIN
#=========================================================================
#=========================================================================
# déclaration pour la création d'un popup
[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
Get-InstalledPrinterDrivers
get-installedDrivers
Write-Output $printdriver
$testdell = get-content "$Env:userprofile\Liste_imprimante.txt" | where {$_ -Match "Dell 2335dn MFP"}
Write-Output $testdell
if ($testdell -ne "Dell 2335dn MFP" )
{
Install-PrinterDriver $ComputerName "Dell 2335dn MFP" "$Env:userprofile\Desktop\InstallImprimanteEuroEng\Canon_ir2270" "P564UKAL.INF"
get-MsgBoxinstallOK
#get-supprListe_imprimante.txt
}else
{
get-MsgBoxalreadyinstall
} |
Partager