簡體   English   中英

從CSV安裝網絡打印機

[英]Installing network printers from CSV

我編寫了一個腳本來將用戶配置文件備份到網絡共享。 我的老板也希望它也可以備份和還原網絡打印機。 該腳本包括以下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移動備份,高級顏色設置,桌面牆紙,將打印機導出到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

這兩個問題都非常簡單...哪里出錯了,是在導入CSV時它創建了一個對象數組。 每個對象都有一個屬性Name 當您引用該對象時,需要指定要使用的屬性,因此Invoke-Expression行應為:

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

這將擴展名稱,並且此時應該可以正常工作。 至於跳過不以“ \\”開頭的條目,您可以執行以下操作:

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

這只會將以“ \\”開頭的條目傳遞到ForEach循環中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM