使用getprofiles.cmd 回显配置文件并使用main.cmd 完成
使用 for 循环来处理配置文件路径。
main.cmd:
@echo off
setlocal
:: Install Wireshark.
echo Wireshark-win64-2.4.6.exe /S
:: Update Wireshark app data in user profiles.
for /f "tokens=*" %%A in ('getprofiles.cmd "\AppData\Roaming"') do (
call :skip_profile "%%~A" "\\Administrator\\" "\\MSSQL\$SQLEXPRESS\\" || (
echo mkdir "%%~A\Wireshark\"
echo xcopy preferences "%%~A\Wireshark"
)
)
exit /b
:skip_profile
for %%A in (%*) do (
if not "%%~A" == "" if /i not "%%~A" == "%~1" (
echo "%~1"| findstr /i "%%~A" >nul 2>nul
if not errorlevel 1 (
echo Skip account "%~1"
exit /b 0
)
)
)
exit /b 1
getprofiles.cmd:
@echo off
setlocal
if "%~1" == "/?" goto :help
:: ProfileList key that contains profile paths.
set "ProfileListKey=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
:: Get profiles directory path.
set "ProfilesDirectory="
for /f "tokens=1,3" %%A in (
'reg query "%ProfileListKey%" /v "ProfilesDirectory"'
) do if /i "%%~A" == "ProfilesDirectory" call set "ProfilesDirectory=%%~B"
if not defined ProfilesDirectory (
>&2 echo ProfilesDirectory is undefined
exit /b 1
)
:: Search all profile paths in profiles directory and echo existing paths appended with the 1st script argument.
for /f "delims=" %%A in (
'reg query "%ProfileListKey%"'
) do call :ProfilePath "%%~A" "%~1"
exit /b
:ProfilePath
setlocal
set "arg1=%~1"
:: Validate 1st call argument is a profile subkey.
if not defined arg1 exit /b 1
if /i "%arg1%" == "%ProfileListKey%" exit /b 1
if "%arg1:~,1%" == " " exit /b 1
:: Echo existing profile paths with defined 2nd argument appended.
for /f "tokens=1,3" %%A in (
'reg query "%arg1%" /v ProfileImagePath^|find /i "%ProfilesDirectory%"'
) do (
if "%%~A" == "ProfileImagePath" (
if exist "%%~B%~2" echo "%%~B%~2"
)
)
exit /b
:help
echo Prints profile paths from the registry that exist in the Profiles Directory.
echo 1st argument can be a path to be appended to the profile path.
echo i.e. "\AppData\Roaming" is appended to become "C:\Users\...\AppData\Roaming".
exit /b
脚本main.cmd 回显测试结果。去除回声
如果命令有效,则实际使用。
注册表中的ProfileList 键存储了查找
配置文件并具有带有数据的子键,例如每个配置文件的路径
机器。
main.cmd 可以避开Administrator 和MSSQL$SQLEXPRESS 等配置文件。
被调用的标签:skip_profile 将配置文件路径作为第一个参数。
以下参数用于模式,如果匹配,将被跳过
轮廓。
findstr 用于检查配置文件路径是否匹配
正则表达式所以使用findstr /? 来满足语法要求。
大小写设置为不区分 /i 的使用。
getprofiles.cmd 脚本获取 ProfilesDirectory 路径
是可以找到用户配置文件文件夹的位置。然后它查询密钥
使用:ProfilePath的被调用标签获取配置文件密钥。
标签检查是否在每个
找到配置文件路径。然后在回显之前检查路径是否存在
路径。如果传递了可选的第一个参数,那么它将是
附加并且路径将被验证为该路径。
测试输出:
Wireshark-win64-2.4.6.exe /S
mkdir "C:\Users\Michael\AppData\Roaming\Wireshark\"
xcopy preferences "C:\Users\Michael\AppData\Roaming\Wireshark"
这看起来不错,因为我当前的机器上只有 1 个用户配置文件。
您可能可以将代码合并在一起以仅制作 1 个脚本
我决定将getprofiles.cmd 留作其他用途。