How to create a script to run a command, capture output, run through if statement?

Posted on 2011-05-03
Last Modified: 2012-05-11
I need a simple script created that will run a custom command (dfc get /isfrozen), return a value (either THAWED or FROZEN), and run through an if statement that will give me a 0 for thawed and a 1 for frozen.  If anyone could help me out that would be greatly appreciated.
Question by:khdonabergerwidener
    LVL 82

    Expert Comment

    Something like this?
    dfc get /isfrozen | find /i "THAWED" >NUL
    if not errorlevel 1 (echo 0) else (echo 1)

    Open in new window


    Author Comment

    I need it to return a value to an external source, in this case, ZCM 11.  When I run that it just echo's the entire thing like so:

    C:\dfc get /isfrozen     | find /i "THAWED"   1>NUL
    C:\if not errorlevel 1 (echo 0 )  else (echo 1 )


    I ran this on both a thawed machine and a frozen machine and they're both returning 1.
    LVL 82

    Assisted Solution

    You need to be more precise in your description. What you wrote sounded like "dfc get /isfrozen" would return either THAWED or FROZEN.
    And what exactly do you mean with "return a value to an external source"? Return a certain errorlevel, echo out a certain string, call another program with certain arguments depending on the result of dfc?
    I just had a look at the dfc documentation; dfc will actually return errorlevel 1 if the machine is "frozen".
    @echo off
    dfc get /isfrozen
    set Frozen=%ErrorLevel%
    REM The variable Frozen is now 1 if the machine is frozen, 0 if it is thawed.
    echo Frozen: %Frozen%

    Open in new window

    LVL 38

    Accepted Solution


    This PDF file tells you the 5 Exit Codes for DFC and provides the rough layout of a batch file that would allow you to inser your own custom command in the appropriate place that it jumps to using GOTO:

    When you run a command in most cases an Exit Code of Zero is normally a success, whereas a 1 or greater signifies an error, warning, etc.  However, if you look at the "Return Values" of the above PDF document you will see that it refers to "Boolean".  The word "Boolean" just means a test that returns a Truie or False to a question.  So, with your     DFC /IsFrozen     command you are asking Faronics DeepFreeze "Is this computer frozen, True or False, Yes or No, Give me a 1 or 0?", to which it replies "True" with a "1" or False with a "0".

    There should be no reason to swap the Return Codes around and make a 1 into a 0 and a 0 into a 1, unless you really must call another program with a 1 or 0 as the parameter where the numbers mean different things to that program.

    You don't have to test all the Exit Codes all of the time as shown in the example batch file at the bottom of the PDF document.  If you just wanted to check if a machine is "frozen", and force a reboot to the "thawed" state if it is, then you just need to ask if it's frozen and listen out for a "1", or else just ignore it, like this basic example:

    @echo off
    dfc get /isfrozen
    if %errorlevel% equ 1 dfc /BootThawed
    LVL 38

    Expert Comment

    Thank you khdonabergerwidener

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
    Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now