powershell netsh 导入 xml 配置文件

Posted

技术标签:

【中文标题】powershell netsh 导入 xml 配置文件【英文标题】:powershell netsh importing xml profiles 【发布时间】:2015-09-16 09:44:45 【问题描述】:

我有以下脚本可以导出所有最终用户的 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
    

这是脚本完成后用户备份的样子

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

这是 csv 的样子

powershell 脚本不适用于本地打印机,但我们可以使用 printbrm.exe 通过驱动程序备份这些脚本。它确实适用于所有网络打印机。

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

【问题讨论】:

【参考方案1】:

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

试试这个

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

【讨论】:

我完全按照所示复制并粘贴了您的代码,并且成功了!!请在我的原始帖子中查看更新!【参考方案2】:

我仍然需要进行测试,但我认为我与 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

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

【讨论】:

我希望我可以将两个答案都标记为正确!请在我的原始帖子中查看更新!

以上是关于powershell netsh 导入 xml 配置文件的主要内容,如果未能解决你的问题,请参考以下文章

powershell 从XML文件导入设置

netsh

windows@网络防火墙@软件联网控制@netsh advfirewall firewall

如何从其他powershell脚本调用powershell脚本并且脚本是在powershell对象而不是文件中分配的

PowerShell 开启无线热点

如何使用 netsh 设置 SQUID 代理身份验证?