Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 多次在批处理文件中使用变量_Variables_Batch File - Fatal编程技术网

Variables 多次在批处理文件中使用变量

Variables 多次在批处理文件中使用变量,variables,batch-file,Variables,Batch File,我试图编译一个批处理文件来获取当前用户的userID,然后使用输出来删除某个注册表项 我找到此项以获取当前登录用户的SID: wmic用户帐户,其中名称=“%username%”获取sid 这非常有效,给了我: 希德 S-1-5-21-XXXXX-XXXXX-XXXXX-XXX 现在我想用S-1-5-21。。。。等。删除当前登录用户的某个注册表项,因此S-1-5-21等。我该怎么做?或者有没有更简单的方法,能够确定当前SID并删除密钥 谢谢大家! 试试这个: @echo off wmic use

我试图编译一个批处理文件来获取当前用户的userID,然后使用输出来删除某个注册表项

我找到此项以获取当前登录用户的SID:

wmic用户帐户,其中名称=“%username%”获取sid

这非常有效,给了我:

希德

S-1-5-21-XXXXX-XXXXX-XXXXX-XXX

现在我想用S-1-5-21。。。。等。删除当前登录用户的某个注册表项,因此S-1-5-21等。我该怎么做?或者有没有更简单的方法,能够确定当前SID并删除密钥

谢谢大家!

试试这个:

@echo off
wmic useraccount where name='%username%' get sid > temp.txt &more temp.txt > temp2.txt &del temp.txt
for /F "tokens=*" %%A in (temp2.txt) do set "userID=%%A"
del temp2.txt
echo %userID%
pause
您可以在此代码之后使用%userID%变量


请注意,您需要使用more,因为wmic解析其输出的方式假定您要删除注册表项

HKEY_USERS\[SID-OF-USER]\test
…此脚本应将其剪切:

@ECHO OFF
SET USERNAME=SomeUserName

:: retrieve user's SID, store in file. NOTE: a user can have more than 1 SID.
wmic useraccount where name='%USERNAME%' get sid | FINDSTR "^S-" > tmp.txt

:: load result into variable. NOTE: this will load the first line only.
SET /P SID_=<tmp.txt

:: remove spaces which turned out to be appended at the end.
SET SID=%SID_: =%

:: delete key in the registry (will probably required elevated privileges)
:: the /F causes reg.exe not to ask for confirmation
REG DELETE "HKEY_USERS\%SID%\test" /F

:: cleanup
DEL tmp.txt
@ECHO关闭
SET USERNAME=SomeUserName
::检索用户的SID,存储在文件中。注意:一个用户可以有多个SID。
wmic useraccount,其中name='%USERNAME%'获取sid | FINDSTR“^S-”>tmp.txt
::将结果加载到变量中。注意:这将仅加载第一行。

SET/P SID_uu=很抱歉回复太晚,我对此做了一个小改动,效果非常好!非常感谢。