【问题标题】:Installing network printers from CSV从 CSV 安装网络打印机
【发布时间】:2015-09-11 08:53:24
【问题描述】:

我编写了一个脚本来将用户配置文件备份到网络共享。我的老板也希望它备份和恢复网络打印机。该脚本包括以下 PowerShell 行...

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

这会将所有打印机导出为 CSV。值如下所示。

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

我编写了另一个脚本来将用户配置文件从备份复制到当前登录的计算机。这包括以下 powershell。

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

$Printer 变量应该返回值\\\server\printer1 从而从命令行安装打印机......但没有任何反应。我哪里做错了?

另外,我怎样才能让它忽略不以“\”开头的任何 CSV 行?

下面的答案解决了这个问题。

这是完整的脚本。它目前备份用户配置文件、签名、任务栏图标、outlook pst、chrome 书签、itunes 移动备份、高级颜色 reg 设置、桌面壁纸、将打印机导出到 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'

这是恢复脚本。对 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

【问题讨论】:

    标签: powershell csv printing foreach


    【解决方案1】:

    这两个问题都非常简单...您出错的地方是,当您导入 CSV 时,它会创建一个对象数组。每个对象都有一个属性Name。当您引用该对象时,您需要指定要使用的属性,因此您的Invoke-Expression 行应该是:

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

    这将扩展名称,此时它应该可以按预期工作。至于让它跳过不以“\”开头的条目,您可以执行以下操作:

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

    这只会将以“\”开头的条目传递到ForEach 循环中。

    【讨论】:

    • 天啊!这就像一个魅力!这是我的第一个 powershell 脚本,如果不是这篇文章,我可能已经放弃了!非常感谢!!!
    • 如果有人想使用我的备份和恢复脚本,他们会在上面发布。我的实际脚本被分解为单独的 .bat 和 .ps1 文件,但您可以自己轻松完成。随心所欲地修改!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2012-05-28
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多