2015-06-24 142 views
0

我編寫了一個腳本來將用戶配置文件備份到網絡共享。我的老闆也希望它備份和恢復網絡打印機。此腳本包含以下PowerShell行...從CSV安裝網絡打印機

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這樣的命令行安裝打印機......但沒有任何反應。我哪裏做錯了?

另外,我怎樣才能讓它忽略任何不以「\」開頭的行?

下面的答案解決了這個問題。

以下是完整的腳本。目前,它備份用戶的個人資料,簽名,任務欄圖標,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 

回答

3

兩個問題非常簡單...你在哪裏錯了,就是當你導入CSV它創造對象的數組。每個對象都有一個屬性,Name。當你引用一個對象,你需要指定要使用的財產,所以你Invoke-Expression行應該是:

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

,將擴大的名稱,並且在這一點上預計它應該工作。至於得到它跳過不啓動項「\」你可以這樣做:

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

是僅通過與一個「\」到ForEach循環開頭的條目。 OMG!

+0

OMG!那就像魅力一樣工作!這是我的第一個PowerShell腳本,我可能已經放棄,如果它不在這個帖子!非常感謝!!! – Blake

+0

如果有人想使用我的備份和還原腳本,它們會在上面發佈。我的實際腳本分解爲單獨的.bat和.ps1文件,但您可以輕鬆地自行完成。修改你的心中的內容! – Blake