适用于Windows Server 2008 R2的Add-Printer,Remove-Printer等替代方案

时间:2016-09-12 13:27:32

标签: sql-server-2008 powershell printing windows-server-2008

我被赋予了从VBScript将旧脚本重写为PowerShell的任务。这个脚本基本上只读取一行文本文件并安装与该行对应的打印机,然后再将其删除,以便只安装驱动程序。

此脚本每天在我们的虚拟Citrix终端服务器上执行,这样我们就可以独立于当前发布的映像更新驱动程序。

这是最终脚本的样子:

# Variables
Param([string]$FileName)
$DriverList = Get-Content $FileName
$ComputerName = hostname
$LogFile = "C:\Windows\Logs\PrinterCreation.log"
$SeperatorL = "═══════════════════════════════════════════════════════════════════════════════════════════════════════"
$SeperatorS = "═══════════════════════════════════════════"
$StartDate = Get-Date -Format "dd.MMM.yyyy HH:mm:ss"

# Log Header
"$SeperatorL" > $LogFile
" ServerName:   $ComputerName" >> $LogFile
" DriverList:   $FileName" >> $LogFile
" StartTime:    $StartDate" >> $LogFile
"$SeperatorL" >> $LogFile
"" >> $LogFile
"Beginning driver instalation process:" >> $LogFile

# Process the "$DriverList" by installing each printer on the list and  deleting the connection afterwards
foreach ($line in $DriverList) {
    "$SeperatorL" >> $LogFile
    " Print driver Installation:    $line" >> $LogFile

    # Installing the printer
    try {
        Add-Printer -ConnectionName $line -ErrorAction Stop
        Start-Sleep -s 10

        # Deleting the Printer
        try {
            Remove-Printer -Name $line -ErrorAction Stop
            " Printer installation successfull." >> $LogFile
        } catch {
            " INSTALATION NOT COMPLETE:     Printer was installed but connection could not be removed!" >> $LogFile
        }
    } catch [Microsoft.Management.Infrastructure.CimException] {
        " INSTALATION FAILED:           Printer driver cannot be found or does not exist!" >> $LogFile
    } finally {
        Start-Sleep -s 5
    }
}

# Log Footnote
$EndDate = Get-Date -Format "HH:mm:ss"
"$SeperatorL" >> $LogFile
"" >> $LogFile
"Instalation process completed:" >> $LogFile
"$SeperatorS" >> $LogFile
" End Time: $EndDate" >> $LogFile
"$SeperatorS" >> $LogFile

它被调用如下:.\scriptfilename.ps1 ".\Driverlist.txt"

驱动程序列表只包含以下行:"\\spbdn140\KM_Universal_PCL"

问题简而言之

编写此脚本时,我没有意识到打印机管理模块仅适用于Win 8和Server 2012以上版本。我们的终端服务器都在运行Server 2008。

有没有办法实现我在Server 2008上的PowerShell v3-4中提供的可用信息(驱动程序列表)?

同样,最终结果应该只是使用提供的信息在终端服务器上安装驱动程序。

1 个答案:

答案 0 :(得分:0)

我不相信,打印机cmdlet全部用于Win8 / Server2008R2及更高版本。不幸的是,某些cmdlet是特定于操作系统的,更新PowerShell不包含它们。

但是,你可以在PrnMngr.vbs等中运行一个名为cscript.exe的PowerShell脚本。

Here is a list of the vbscripts that you could use in place of the print management cmdlets.