我正在运行以下脚本来获取有关已安装打印机的报告:
https://community.spiceworks.com/scripts/show/2186-export-printer-information-to-spreadsheet?page=1
然而,当脚本运行时,它会扫描 64 位驱动程序并检索它,将其复制到 excel 输出,然后扫描 32 位并覆盖原始 64 位。
我曾尝试过但没有成功找到解决方案:
# Get printer information
ForEach ($PrintServer in $PrintServers)
{ Write-Verbose "$(Get-Date): Working on $PrintServer..."
$Printers = Get-WmiObject Win32_Printer -ComputerName $PrintServer
ForEach ($Printer in $Printers)
{
If ($Printer.Name -notlike "Microsoft XPS*")
{
$Drivers = Get-WmiObject Win32_PrinterDriver -Filter "__path like '%$($Printer.DriverName)%'" -ComputerName $Printserver
ForEach ($Driver in $Drivers)
{ $Drive = $Driver.DriverPath.Substring(0,1)
$Sheet.Cells.Item($intRow, 1) = $PrintServer $Sheet.Cells.Item($intRow, 2) = $Printer.Name
$Sheet.Cells.Item($intRow, 3) = $Printer.Location
$Sheet.Cells.Item($intRow, 4) = $Printer.Comment
If ($Printer.PortName -notlike "*\*")
{ $Ports = Get-WmiObject Win32_TcpIpPrinterPort -Filter "name = '$($Printer.Portname)'" -ComputerName $Printserver
ForEach ($Port in $Ports)
{
$Sheet.Cells.Item($intRow, 5) = $Port.HostAddress
}
}
$Sheet.Cells.Item($intRow, 6) = $Printer.DriverName
$Sheet.Cells.Item($intRow, 7) = (Get-ItemProperty ($Driver.DriverPath.Replace("$Drive`:","\\$PrintServer\$Drive`$"))).VersionInfo.ProductVersion
$Sheet.Cells.Item($intRow, 8) = Split-Path $Driver.DriverPath -Leaf
$Sheet.Cells.Item($intRow, 9) = $Printer.Shared
$Sheet.Cells.Item($intRow, 10) = $Printer.ShareName
$Sheet.Cells.Item($intRow, 11) = Split-Path
$Driver.SupportedPlatform -Leaf
$intRow ++
}
**************************
这失败了,有人可以建议如何检索 64 位驱动程序并将其输入到电子表格中。
答案1
您可以创建一个变量来跟踪您的列索引,类似于$intRow,但将其设置为获取打印机循环的本地值。然后每次插入电子表格时都增加它。代码如下。您可能希望将所有驱动程序信息移动到行的末尾,以便您的列对齐。
# Get printer information
ForEach ($PrintServer in $PrintServers)
{ Write-Verbose "$(Get-Date): Working on $PrintServer..."
$Printers = Get-WmiObject Win32_Printer -ComputerName $PrintServer
ForEach ($Printer in $Printers)
{
If ($Printer.Name -notlike "Microsoft XPS*")
{
$intCol = 1 #Declare column index here
$Drivers = Get-WmiObject Win32_PrinterDriver -Filter "__path like '%$($Printer.DriverName)%'" -ComputerName $Printserver
ForEach ($Driver in $Drivers)
{ $Drive = $Driver.DriverPath.Substring(0,1)
$Sheet.Cells.Item($intRow, $intCol++) = $PrintServer $Sheet.Cells.Item($intRow, 2) = $Printer.Name
$Sheet.Cells.Item($intRow, $intCol++) = $Printer.Location
$Sheet.Cells.Item($intRow, $intCol++) = $Printer.Comment
If ($Printer.PortName -notlike "*\*")
{ $Ports = Get-WmiObject Win32_TcpIpPrinterPort -Filter "name = '$($Printer.Portname)'" -ComputerName $Printserver
ForEach ($Port in $Ports)
{
$Sheet.Cells.Item($intRow, $intCol++) = $Port.HostAddress
}
}
$Sheet.Cells.Item($intRow, $intCol++) = $Printer.DriverName
$Sheet.Cells.Item($intRow, $intCol++) = (Get-ItemProperty ($Driver.DriverPath.Replace("$Drive`:","\\$PrintServer\$Drive`$"))).VersionInfo.ProductVersion
$Sheet.Cells.Item($intRow, $intCol++) = Split-Path $Driver.DriverPath -Leaf
$Sheet.Cells.Item($intRow, $intCol++) = $Printer.Shared
$Sheet.Cells.Item($intRow, $intCol++) = $Printer.ShareName
$Sheet.Cells.Item($intRow, $intCol++) = Split-Path
$Driver.SupportedPlatform -Leaf
$intRow ++
}
**************************