Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Execute wmic command in cmd

by mvip (Acolyte)
on May 15, 2013 at 14:15 UTC ( [id://1033691]=perlquestion: print w/replies, xml ) Need Help??

mvip has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, im new in perl :P so i would like to ask a stupid question ( no its not stupid if you are noob :D ) so here it is ... i would like to execute two commands ( about the health of hard drive ) the first command is "wmic" and that command is opening new line ( or something like telnet try it ) after that i have to execute the "diskdrive get status" so i can get the results and i like to write that result in variable so for now i have this code but i get error on the second system command it cant be execute. Any help or solution ....

use strict; use warnings; use diagnostics; system ('wmic'); system ('diskdrive get status');

Replies are listed 'Best First'.
Re: Execute wmic command in cmd
by BrowserUk (Patriarch) on May 15, 2013 at 14:27 UTC

    If you only want one piece of information, do it all as a single command:

    @output = `wmic diskdrive get status`;; print "@output";; Status OK

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Tnx for the reply. I dont think we understand....maybe i confuse u with that telling you "is like telent". So i will try explain again. The first command when i execute is like this:

      C:\Users\mypc\Desktop\Perl>wmic wmic:root\cli>

      so as you can see wmic command is execute and i got the "wmic:root\cli>" prompt and here i have to execute the "diskdrive get status" command that will show me if the disk is ok or not. Here is how it looks like all the procedure ....

      C:\Users\mypc\Desktop\Perl>wmic wmic:root\cli>diskdrive get status Status OK wmic:root\cli>
        as you can see wmic command is execute and i got the "wmic:root\cli>" prompt and here i have to execute the "diskdrive get status" command that will show me if the disk is ok or not.

        You can do it that way. (Ie. interactive mode.)

        But, you can also do it all in one go: wmic diskdrive get status as I showed above. (Try it it on your command line. )

        And the second way is the easiest way to do it from a program. (Again; try the code I posted for yourself.)

        It is not just the easiest way. Trying to use the interactive mode from within a Perl program is (almost) impossible.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        mvib:

        As you can see below, you can either use wmic interactively ("like telnet") or as a simple command-line driven program.

        H:\>wmic wmic:root\cli>diskdrive get status Status OK wmic:root\cli>quit H:\>wmic diskdrive get status Status OK H:\>

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Execute wmic command in cmd
by ww (Archbishop) on May 15, 2013 at 14:26 UTC
    You may be able to solve this by inserting a sleep nn; where nn is an appropriate time for wmic to finish. Since you didn't tell us the exact error it's hard to tell, but the second system may well be waiting (and thus, " cant be execute" until the first is complete.

    If you didn't program your executable by toggling in binary, it wasn't really programming!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1033691]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found