是否可以将此PowerShell脚本转换为批处理?

时间:2013-11-07 22:37:20

标签: powershell batch-file

这是剧本:

$devid = "*HID\VID_0EEF&PID_A001&REV_1213&Col01"
$status = devcon status $devid | Select-String "running"
if($status -eq $null) {
write-host "Enabling touch"
devcon enable $devid
} else {
write-host "Disabling touch"
devcon disable $devid
devcon restart $devid
}

这就是我所拥有的:

@ECHO OFF
SET devid="HID\VID_0EEF&PID_A001&REV_1213&Col01"
set status=
for /f "tokens=10" %%i in ('devcon status %devid%') do @set status=%%i
IF %status%==running (
devcon enable %devid%
) ELSE (
devcon disable %devid%
devcon restart %devid%
)

但这不起作用,我无法弄清楚问题。

1 个答案:

答案 0 :(得分:0)

@echo off
set "devid=HID\VID_0EEF&PID_A001&REV_1213&Col01"
devcon status "%devid%" | find "running" > nul
if errorlevel 1 (
    devcon enable "%devid%"
) else (
    devcon disable "%devid%"
    devcon restart "%devid%"
)

搜索(find)字符串。检查是否找到(not errorlevel 1)或找不到