powershell netsh导入xml配置文件

时间:2015-06-29 19:29:44

标签: powershell csv foreach netsh

我有以下脚本导出所有endusers wifi配置文件。

netsh wlan export profile folder="\\server\%username%\Wireless"
Powershell.exe -executionpolicy unrestricted -File wireless_backup.ps1

然后使用“wireless_backup.ps1”将配置文件名称放入.CSV。这是wireless_backup.ps1。

Get-ChildItem "\\server\$env:username\Wireless" | Select Name | Export-CSV -path '\\server\$env:username\Wireless'

以上所有工作正常....将这些配置文件还原到另一台PC上是我遇到困难的地方。这是恢复脚本。

Powershell.exe -executionpolicy unrestricted -File wireless_restore.ps1

wireless_restore.ps1包含这段代码。

$ProfileList=IMPORT-CSV \\server\$env:username\Wireless\wireless.csv

FOREACH ($Profile in $ProfileList) {
netsh wlan add profile filename= $Profile.name
}

我的期望是Wifi配置文件会自动添加到PC。我不确定我哪里出错了。请帮忙!

更新 我的老板出于某种原因不想使用MS用户状态迁移,但我们正在进行大规模重新成像。我需要一种方法来轻松备份以下所有内容:用户配置文件数据,chrome书签,itunes备份,outlook pst文件,打印机,无线配置文件,outlook自动完成,壁纸和其他一些东西。

这是我提出的备份脚本

REM FORCE OUTLOOK CLOSED, PST FILES WILL NOT BACKUP IF IN USE
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"

    wallpaper.ps1 looks like this
    $wallpaper = (Get-ItemProperty 'hkcu:\control panel\desktop\' -Name Wallpaper).Wallpaper
    xcopy $wallpaper "\\itmdtren\z$\backup\$env:username\Wallpaper\"

REM EXPORTS ALL CURRENTLY INSTALLED PRINTERS TO CSV
Powershell.exe -executionpolicy unrestricted -File "\\itmdtren\z$\backup scripts\print_backup.ps1"

    printer_backup.ps1 looks like this
    Get-WMIObject -class Win32_Printer -computer $env:computername | Select Name | Export-CSV

REM EXPORT ALL XML WLAN PROFILES TO BACKUP
netsh wlan export profile folder="\\itmdtren\z$\backup\%username%\Wireless"

REM EXPORTS NAMES OF ALL WLAN PROFILES TO CSV
Powershell.exe -executionpolicy unrestricted -File "\\itmdtren\z$\backup scripts\wireless_backup.ps1"

    wireless_backup.ps1 looks like this
    Get-ChildItem "\\itmdtren\z$\backup\$env:username\Wireless" | Select Name | Export-CSV -path '\\itmdtren\z$\backup\$env:username\Wireless\wireless.csv'

对计算机进行映像后,我以用户身份登录并运行以下脚本。这是您的答案所帮助的地方!如您所见,wifi配置文件导出脚本是上面显示的打印机备份脚本的修改版本。

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

REM RESTORE USERS PROFILE
xcopy "\\itmdtren\z$\backup\%username%\%username%" "%userprofile%" /e /y /i

REM RESTORE SIGNATURES
xcopy "\\itmdtren\z$\backup\%username%\Signatures" "%appdata%\microsoft\signatures" /e /y /i

REM THIS LINE NOT USED RESTORE TASKBAR
REM xcopy "\\itmdtren\z$\backup\%username%\TaskBar" "%AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar" /e /y /i

REM RESTORE OUTLOOK DATA FILES PST
xcopy "\\itmdtren\z$\backup\%username%\Outlook\*.pst" "C:\Users\%username%\Documents\Outlook Files" /y /i

REM RESTORE USER DEFAULT CHROME BOOKMARKS/SETTINGS
xcopy "\\itmdtren\z$\backup\%username%\Chrome" "C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default" /e /y /i

REM RESTORE ITUENS BACKUPS
xcopy "\\itmdtren\z$\backup\%username%\MobileSync" "C:\Users\%username%\AppData\Roaming\Apple Computer\MobileSync" /e /y /i

REM RESTORE ADVANCED DISPLAY 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 wallpaperchanger.exe IS A COMMANDLINE UTILITY THAT RANDOMLY SELECTS A IMAGE FROM A FOLDER, CONVERTS TO BMP, COPIES TO USER PROFILE AND SETS BACKGROUD
"\\itmdtren\z$\backup scripts\WallpaperChanger.exe" "\\itmdtren\z$\backup\%username%\Wallpaper" 2 "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Themes"

REM FORCE UPDATE USER SYSTEM PARAMETERS
RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters

REM RESTORE PRINTERS FROM CSV
Powershell.exe -executionpolicy unrestricted -File "\\itmdtren\z$\backup scripts\print_restore.ps1"

    print_restore.ps1 looks like this
    $PrinterList=IMPORT-CSV \\itmdtren\z$\backup\$env:username\Printers\printer_export.csv

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

REM RESTORE XML WLAN PROFILES FROM CSV
Powershell.exe -executionpolicy unrestricted -File "\\itmdtren\z$\backup scripts\wireless_restore.ps1"

    wireless_restore.ps1 looks like this
    $ProfileList=IMPORT-CSV \\itmdtren\z$\backup\$env:username\Wireless\wireless.csv

    FOREACH ($Profile in $ProfileList) {
    $fullname = Join-Path "\\itmdtren\z$\backup\$env:username\Wireless" $profile.name
    netsh wlan add profile filename= $fullname
    }

这是脚本完成后用户备份的样子 enter image description here

正如您所见,所有XML无线配置文件确实已导出,而powershell脚本将所有这些文件的名称放在csv中。 enter image description here

这是csv的样子

enter image description here

powershell脚本不适用于本地打印机,但我们可以使用printbrm.exe来支持驱动程序。它确实适用于所有网络打印机。 enter image description here

恢复脚本将所有这些重新放回到新映像的PC上,没有任何问题,我将这一切归功于Stackoverflow的成员!

2 个答案:

答案 0 :(得分:0)

$ Profile.Name是不够的,你应该提供完整的unc路径,

试试这个

FOREACH ($Profile in $ProfileList) {
$fullname = Join-Path "\\server\$env:username\Wireless" $profile.name
netsh wlan add profile filename= $fullname
}

答案 1 :(得分:0)

我仍然需要测试,但我认为我和Avshalom就这个问题的根源是什么。名称只是文件名和扩展名。它不是完整的文件路径,而是您应该使用它来获得一致的结果。如果PowerShell会话中的当前目录为\\server\%username%\Wireless,则可能可能没有问题。

没有测试,因为我在这台电脑上没有无线配置文件,我建议您一起取消CSV,因为您只想要一件事。我不知道这是不是一个错字,但你导出的并不是你想要的路径。在还原脚本中,您使用的是\\server\$env:username\Wireless\wireless.txt,但在备份脚本中,我们会看到\\server\$env:username\Wireless。假设你拥有权限,你会看到一个文件调用"无线"在\\server\$env:username下。

wireless_backup.ps1

Get-ChildItem "\\server\$env:username\Wireless" | Select-Object -Expand FullName | Set-Content -path '\\server\$env:username\Wireless\wireless.txt'

wireless_restore.ps1

$ProfileList=Get-Content \\server\$env:username\Wireless\wireless.txt

FOREACH ($Profile in $ProfileList) {
    netsh wlan add profile filename=$Profile
}

仅供参考我不知道(但我正在查询)这是否是导出无线配置文件的合适方式。