【问题标题】:Alternatives to Add-Printer, Remove-Printer etc. for Windows Server 2008 R2Windows Server 2008 R2 的 Add-Printer、Remove-Printer 等的替代品
【发布时间】:2016-09-12 13:27:32
【问题描述】:

我被分配了从 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 中的可用信息(驱动程序列表)来实现我想要做的事情?

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

【问题讨论】:

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


    【解决方案1】:

    我不这么认为,打印机 cmdlet 都适用于 Win8/Server2008R2 及更高版本。不幸的是,一些 cmdlet 是特定于操作系统的,更新 PowerShell 不包括它们。

    但是,您可以运行名为 cscript.exe 的 PowerShell 脚本,并在 PrnMngr.vbs 等中完成所需的操作。

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

    【讨论】:

      猜你喜欢
      • 2014-04-18
      • 2011-06-26
      • 1970-01-01
      • 2013-08-09
      • 2010-12-03
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多