Example 1:
for /F "tokens=3 delims= " %%A in ('manage-bde -status %systemdrive% ^| findstr " Encryption Method:"') do (
if "%%A"=="AES" goto EncryptionCompleted
)
:EncryptionCompleted
Example 2:
for /F %%A in ('wmic /namespace:\\root\cimv2\security\microsofttpm path win32_tpm get IsEnabled_InitialValue ^| findstr "TRUE"') do (
if "%%A"=="TRUE" goto nextcheck
)
:nextcheck
Please help to find the below code as run on .bat
to stop script execution.
The command is:
powershell.exe (Get-Tpm | Select -Property TpmReady).TpmReady -eq $False
then goto Failed
:Failed
CodePudding user response:
Since you're only looking to act on a Boolean value, you can communicate that via the PowerShell process' exit code, with
0
corresponding to$true
and1
to$false
, given that the widely observed convention is that exit code0
signals success, whereas any nonzero exit code signals an error condition.- Boolean values in PowerShell can directly be converted to integers, which, however, performs the opposite mapping:
[int] $true
is1
and[int] $false
is0
. - Therefore, the logic must be reversed with
-not
before passing the Boolean to PowerShell'sexit
statement.
- Boolean values in PowerShell can directly be converted to integers, which, however, performs the opposite mapping:
On the
cmd.exe
(batch-file) side, this allows you to act on the exit code with the||
operator, which only executes the RHS in case of failure, i.e. if the LHS command reported a nonzero exit code (such as1
).
powershell.exe -noprofile -c "exit -not (Get-Tpm).TpmReady" || goto :FAILED
echo "TPM is ready."
exit /b 0
:FAILED
echo "TPM is NOT ready." >&2
exit /b 1
Note that I've added the following CLI parameters to the PowerShell call: -noprofile
to potentially speed up execution, and -c
(-Command
) to explicitly signal that a command (piece of PowerShell code) is being passed.