简体   繁体   English

从CSV安装网络打印机

[英]Installing network printers from CSV

I wrote a script to backup a users profile to a network share. 我编写了一个脚本来将用户配置文件备份到网络共享。 My boss wants it to backup and restore network printers too. 我的老板也希望它也可以备份和还原网络打印机。 This script includes the following line of PowerShell... 该脚本包括以下PowerShell行...

Get-WMIObject -class Win32_Printer -computer $env:computername | Select Name | Export-CSV -path '\\share\printer_export.csv'

this exports all of the printers to a CSV. 这会将所有打印机导出到CSV。 The values look like this. 值看起来像这样。

#TYPE Selected.System.Management.ManagementObject
Name
Snagit 10
Microsoft XPS Document Writer
\\\server\printer1
\\\server\printer2
\\\server\printer3

I wrote another script to copy the users profile from the backup to the currently logged on computer. 我编写了另一个脚本,将用户配置文件从备份复制到当前登录的计算机。 This includes the following powershell. 这包括以下Powershell。

$PrinterList=IMPORT-CSV \\share\printer_export.csv
FOREACH ($Printer in $PrinterList) {
    Invoke-Expression 'rundll32 printui.dll PrintUIEntry /in /q /n $Printer'
}

the $Printer variable should return the value \\\\\\server\\printer1 thus installing the printers from the command line... but nothing happens. $Printer变量应返回值\\\\\\server\\printer1从而从命令行安装打印机...但是什么也没有发生。 Where did I go wrong? 我哪里做错了?

ALSO, how can I get it to ignore any line of the CSV that does not start with "\\"? 另外,如何获取它以CSV开头不以“ \\”开头的任何行?

the answer below fixed the problem. 下面的答案解决了这个问题。

Here is the full script. 这是完整的脚本。 It currently backs up the users profile, signatures, taskbar icons, outlook pst, chrome bookmarks, itunes mobile backups, advanced color reg settings, desktop wallpaper, exports printers to csv 它当前备份用户配置文件,签名,任务栏图标,Outlook PST,Chrome书签,iTunes移动备份,高级颜色设置,桌面墙纸,将打印机导出到CSV

REM CLOSE OUTLOOK
cscript "\\server\outlook.vbs"

REM BACKUP USERS PROFILE
xcopy "%userprofile%" "\\server\%username%\%username%" /e /y /i

REM BACKUP SIGNATURES
xcopy "%appdata%\microsoft\signatures" "\\server\%username%\Signatures" /e /y /i

REM BACKUP PINNED TASKBAR ITEMS
xcopy "%AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar" "\\server\%username%\TaskBar" /e /y /i

REM BACKUP OUTLOOK ARCHIVES PST OUTLOOK MUST BE CLOSED
xcopy "C:\Users\%username%\AppData\Local\Microsoft\Outlook\*.pst" "\\server\%username%\Outlook" /y /i

REM BACKUP CHROME BOOKMARKS
xcopy "C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default" "\\server\%username%\Chrome" /e /y /i

REM BACKUP iTUNES MOBILE BACKUPS
xcopy "C:\Users\%username%\AppData\Roaming\Apple Computer\MobileSync" "\\server\%username%\MobileSync" /e /y /i

REM BACKUP ADVANCED COLOR SETTINGS
REG EXPORT "HKCU\Control Panel\Colors" "\\server\%username%\Wallpaper\Colors1.reg" /y

REM BACKUP ADVANCED COLOR SETTINGS
REG EXPORT "HKCU\Control Panel\Desktop\Colors" "\\server\%username%\Wallpaper\Colors2.reg" /y

REM BACKUP DESKTOP BG SETTINGS
REG EXPORT "HKCU\Control Panel\Desktop\WindowMetrics" "\\server\%username%\Wallpaper\WindowMetrics_Backup.reg" /y

REM START WALLPAPER BACKUP SCRIPT
Powershell.exe -executionpolicy remotesigned -File "wallpaper.ps1"

REM ASSIGNES VALUE OF CURRENT WALLPAPER TO A VARIABLE
$wallpaper = (Get-ItemProperty 'hkcu:\control panel\desktop\' -Name Wallpaper).Wallpaper

REM COPIES THE VARIABLE TO THE USERS BACKUP
xcopy $wallpaper "\\server\$env:username\Wallpaper\"

REM EXPORTS ALL CURRENTLY INSTALLED PRINTERS TO CSV
Get-WMIObject -class Win32_Printer -computer $env:computername | Select Name | Export-CSV -path '\\server\$env:username\printer_export.csv'

Here is the Restoration script. 这是恢复脚本。 After I image a PC I run this script to put everything back. 在为PC成像后,我运行此脚本将所有内容放回原处。

REM CLOSES OUTLOOK
cscript "\\itmdtren\z$\backup scripts\outlook.vbs"

REM RESTORE USERS PROFILE DATA
xcopy "\\server\%username%\%username%" "%userprofile%" /e /y /i

REM RESTORE SIGS
xcopy "\\server\%username%\Signatures" "%appdata%\microsoft\signatures" /e /y /i

REM RESTORE TASKBAR ICONS, THIS LINE NOT USED
REM xcopy "\\server\%username%\TaskBar" "%AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar" /e /y /i

REM RETORE OUTLOOK ARCHIVES PST
xcopy "\\server\%username%\Outlook\*.pst" "C:\Users\%username%\Documents\Outlook Files" /y /i

REM RETORE CHROME BOOKMARKS AND USER DEFAULT DATA
xcopy "\\server\%username%\Chrome" "C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default" /e /y /i

REM RESTORE iTUNES BACKUPS
xcopy "\\server\%username%\MobileSync" "C:\Users\%username%\AppData\Roaming\Apple Computer\MobileSync" /e /y /i

REM RESTORE ADVANCED BACKGROUND COLOR SETTINGS
REG import "\\itmdtren\z$\backup\%username%\Wallpaper\Colors1.reg"
REG import "\\itmdtren\z$\backup\%username%\Wallpaper\Colors2.reg"
REG import "\\itmdtren\z$\backup\%username%\Wallpaper\WindowMetrics_Backup.reg"

REM RESTORE USERS WALLPAPER USING wallpaperchanger.exe found here http://sg20.com/techblog/2011/06/23/wallpaper-changer-command-line-utility/
REM launches exe from the server, points at the wallpaper folder, randomly selects image, converts to bmp and copies it to the users theme folder then sets as wallpaper

"\\server\WallpaperChanger.exe" "\\server\%username%\Wallpaper" 2 "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Themes"

Powershell.exe -executionpolicy Unrestricted -File "PRINT.ps1"

# PRINT.ps1 looks like this
$PrinterList=IMPORT-CSV \\server\$env:username\printer_export.csv

FOREACH ($Printer in $PrinterList) {
Invoke-Expression 'rundll32 printui.dll PrintUIEntry /in /q /n $($Printer.Name)'

}

REM REFRESH USER SYSTEM PARAMETERS
RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters

Both questions are pretty simple... Where you went wrong is that when you import the CSV it created an array of objects. 这两个问题都非常简单...哪里出错了,是在导入CSV时它创建了一个对象数组。 Each object has one property, Name . 每个对象都有一个属性Name When you reference that object you need to specify the property that you want to use, so you Invoke-Expression line should be: 当您引用该对象时,需要指定要使用的属性,因此Invoke-Expression行应为:

Invoke-Expression 'rundll32 printui.dll PrintUIEntry /in /q /n $($Printer.Name)'

That will expand the name, and it should work as expected at that point. 这将扩展名称,并且此时应该可以正常工作。 As for getting it to skip entries that don't start with "\\" you can do something like: 至于跳过不以“ \\”开头的条目,您可以执行以下操作:

FOREACH ($Printer in ($PrinterList | Where{$_.Name -like "\*"})) {

That only passes entries that start with a "\\" into the ForEach loop. 这只会将以“ \\”开头的条目传递到ForEach循环中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM