读取注册表项值,如果找到该术语,则运行bat文件

时间:2018-04-16 08:37:55

标签: powershell batch-file scripting powershell-v3.0

我想在注册表值中搜索“United States”,如果存在则运行.bat文件,否则退出脚本。

Reg键:

"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Distinguished-Name"

Reg Value:(其中US = United States)

CN=machinename,OU=Computers,OU=US,OU=Sites,DC=brand,DC=com

我的powershell脚本:

$Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Distinguished-Name" -Recurse |
ForEach-Object {if ($_ -match "United States"){start-process "C:\US_Keyboard_Remove\US_Keyboard_Remove_ActivSetup.bat"}}

我的批处理脚本:

$set "key=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Distinguished-Name"
set "search=United States"
for /f "delims=" %%a in ('reg query "%key%" /s^| findstr "%search%"') do reg delete "%key%" /v "%%~a"

请帮我修复代码。

1 个答案:

答案 0 :(得分:0)

以下批处理脚本示例应该只运行您指定的命令'如果找到了搜索字符串:

@Echo Off
Set "Key=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine"
Set "Val=Distinguished-Name"
Set "Str=OU=US"
Reg Query "%Key%" /V "%Val%" | Find /I "%Str%" > Nul || GoTo :EOF
Rem your specified commands below here

如果您需要运行指定的命令'如果找不到%Str%,请将||上的5更改为&&