我们需要一个批处理脚本来查看已经激活的映射,删除映射并重新映射一次但使用新密码。
我有以下代码示例:
@echo off
set server=srv1
for /f "tokens=1,2*" %%a in ('wmic logicaldisk get Caption^, providername^| find /I "%server%"') do (
for /f "tokens=1,2*" %%d in ("%%a") do (
echo net use %%a /del
for /f "tokens=1,2*" %%e in ("%%a", "%%b") do (
echo net use %%a %%b /user:dom/uid1 pwd
)
)
)
问题是我需要先删除所有映射,然后再进行所有重映射。
我目前的代码有:删除,重新映射,删除,重新映射。
有人能用我的代码示例帮助我吗?
答案 0 :(得分:0)
要做你想做的事,你需要存储当前的映射somwhow。我会在类似数组的环境变量中执行此操作,例如$MAP[1]
,$MAP[2]
。由于Caption
属性似乎始终是驱动器号加上冒号,并且ProviderName
属性似乎总是UNC路径,因此变量$MAP[1]
可能包含<Caption>|<ProviderName>
之类的内容}。
因此,让我们建立两个独立的循环,第一个循环执行删除和数据存储,第二个循环执行重新映射活动 - 例如:
@echo off
set "SERVER=srv1"
rem // Initialise index counter:
set /A "COUNT=0"
rem /* Capture output of the `wmic` command line that is filtered by a `where` clause;
rem this returns only the drives, filtered for network drives (`DriveType=4`);
rem two nested loops avoid text conversion artefacts like orphaned CR marks: */
for /F "delims=" %%K in ('
wmic LogicalDisk where "DriveType=4 and ProviderName like '\\\\%SERVER%\\%%'" get DeviceID /VALUE
') do for /F "tokens=1* delims==" %%I in ("%%K") do (
rem /* Capture output of the `wmic` command line that returns the mapping target
rem that corresponds with the current drive: */
for /F "delims=" %%G in ('
wmic LogicalDisk where "DeviceID='%%J'" get ProviderName /VALUE
') do for /F "tokens=1* delims==" %%E in ("%%G") do (
rem // Increment index counter:
set /A "COUNT+=1"
rem // Revert handling of `&` by `wmic`, hence replace `&` by `&`:
set "NAME=%%F" & call set "NAME=%%NAME:&=&%%"
rem /* Store current mapping into array-like variable;
rem `call` and double-`%` in `%%COUNT%%` are necessary as `COUNT` is modified
rem and read within the same block of code (`%COUNT%` would return `0`): */
call set "$MAP[%%COUNT%%]=%%J|%%NAME%%"
rem // Delete the current mapping:
ECHO net use %%J /DELETE
)
)
rem // Loop over all array elements:
for /F "tokens=1-2* delims=|=" %%H in ('2^> nul set $MAP[') do (
rem // Establish remapping with new user data:
ECHO net use %%I "%%J" pwd /USER:dom/uid1
rem // Delete handled array element:
set "%%H="
)
当然,您需要先设置和调整过滤器变量SERVER
(用于替换where
命令的find
子句。我将搜索字符串更改为\\%SERVER%\
,因此只匹配完整的服务器名称。
此方法正确处理包含 SPACE 和&符号(&
)的网络路径。
要实际删除并重新映射远程路径,请删除大写ECHO
命令。
答案 1 :(得分:0)
非常感谢aschipfl!
Scipt工作得非常好!如果连接到多个域,我还要添加一个关于cmdkey
的部分来处理用户/ PWD。最后这里是我的compl scipt:
@echo off
::# ------------------------------------------------------------------
::# specify Server Name (FQDN or IP Adresse)
::# IP: 10.0.0.1
::# FQDN: srv1.foo.bar
::# ------------------------------------------------------------------
set SERVER=10.0.0.1
::#
::# ------------------------------------------------------------------
::# delete old mappings for Server
::# ------------------------------------------------------------------
rem // Initialise index counter:
set /A "COUNT=0"
rem /* Capture output of the `wmic` command line that is filtered by a `where` clause;
rem this returns only the drives, filtered for network drives (`DriveType=4`);
rem two nested loops avoid text conversion artefacts like orphaned CR marks: */
for /F "delims=" %%K in ('
wmic LogicalDisk where "DriveType=4 and ProviderName like '\\\\%SERVER%\\%%'" get DeviceID /VALUE
') do for /F "tokens=1* delims==" %%I in ("%%K") do (
rem /* Capture output of the `wmic` command line that returns the mapping target
rem that corresponds with the current drive: */
for /F "delims=" %%G in ('
wmic LogicalDisk where "DeviceID='%%J'" get ProviderName /VALUE
') do for /F "tokens=1* delims==" %%E in ("%%G") do (
rem // Increment index counter:
set /A "COUNT+=1"
rem // Revert handling of `&` by `wmic`, hence replace `&` by `&`:
set "NAME=%%F" & call set "NAME=%%NAME:&=&%%"
rem /* Store current mapping into array-like variable;
rem `call` and double-`%` in `%%COUNT%%` are necessary as `COUNT` is modified
rem and read within the same block of code (`%COUNT%` would return `0`): */
call set "$MAP[%%COUNT%%]=%%J|%%NAME%%"
rem // Delete the current mapping:
net use %%J /DELETE
cmdkey /delete:%SERVER%
)
)
::#
::# ------------------------------------------------------------------
::# Get User Information
::# ------------------------------------------------------------------
set /p U-ID=Bitte U-ID eingeben:
set /p Pwd=Bitte das NEU vergebene Passwort eingeben:
::#
::# ------------------------------------------------------------------
::# New mappings for Server
::# ------------------------------------------------------------------
rem // Loop over all array elements:
for /F "tokens=1-2* delims=|=" %%H in ('2^> nul set $MAP[') do (
rem // Establish remapping with new user data:
cmdkey /add:%SERVER% /user:%U-ID%@foo.bar /pass:%PWD%
net use %%I "%%J" /PERSISTENT:YES
rem // Delete handled array element:
set "%%H="
)
当然,cmdkey运行了几次,但没关系。 非常感谢您的帮助! Beste regrads AxelF