Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

running command line from Perl

by Anonymous Monk
on May 18, 2022 at 11:22 UTC ( [id://11143964]=perlquestion: print w/replies, xml ) Need Help??

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

Hello. How do I run the following from a Perl script on Windows?

get-wmiobject Win32_ComputerSystemProduct | Select-Object -ExpandPrope +rty UUID # this runs fine from command line

The followings do not work

my ($UUID) = system ("get-wmiobject Win32_ComputerSystemProduct | Sele +ct-Object -ExpandProperty UUID"); # 'get-wmiobject' is not recognized + as an internal or external command my ($UUID) = system ('get-wmiobject Win32_ComputerSystemProduct | Sele +ct-Object -ExpandProperty UUID'); my ($UUID) = `get-wmiobject Win32_ComputerSystemProduct | Select-Objec +t -ExpandProperty UUID`; # same error as above, `` works perfectly fi +ne on macOS

Replies are listed 'Best First'.
Re: running command line from Perl
by LanX (Saint) on May 18, 2022 at 11:54 UTC
    your standard shell is most probably CMD but the command you are trying to run is Powershell.

    > # this runs fine from command line

    which is powershell? (look at the preceding PS)

    > `` works perfectly fine on macOS

    MacOS supports PS?

    UPDATE

    TIMTOWTDI, but this works for me

    C:\tmp>perl print `powershell Invoke-Command -ScriptBlock {"get-wmiobject Win32_Co +mputerSystemProduct | select-object -ExpandProperty UUID"}` __END__ C44A693E-8B14-11EC-6464-A0542DA054C5

    Only tangentially a Perl question, you need to find ways to execute your PS from CMD ...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      You are right. I am using Powershell but CMD is standard.

      I found this alternative for CMD:

      wmic path win32_computersystemproduct get uuid #works in CMD

      Still I do not get the principle from Perl for CMD

      my $UUID = system ('wmic path win32_computersystemproduct get uuid');

      No PS on macOS. I meant the way to call a command from Terminal works out-of-the-box. This works on macOS:

      my ($UUID) = `ioreg -d2 -c IOPlatformExpertDevice` =~ /^.*\bIOPlatform +UUID\b.*"([^"]+)"\s*$/m or print "failed to parse ioreg";
        > system ('wmic path win32_computersystemproduct get uuid');

        ehm ... works for me °,

        C:\tmp>perl print system ('wmic path win32_computersystemproduct get uuid'); __END__ UUID 2DA054C5-6464-11EC-8B14-A054C44A693E 0 C:\tmp>

        ... but you need to extract the second line from the output.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        update

        °) sorry, the demo is misleading, see Re^6: running command line from Perl

        a working approach is:

        C:\tmp>perl $a=qx(wmic path win32_computersystemproduct get uuid); print "<<<$a>>>"; __END__ <<<UUID 2DA054C5-6464-11EC-8B14-A054C44A693E >>> C:\tmp>

        Nothing stops you from using system to run powershell instead of cmd.

        Please look at my update for another way to do it :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re: running command line from Perl
by soonix (Canon) on May 18, 2022 at 13:16 UTC
    using DBD::WMI:
    use strict; use warnings; use DBI; my $dbh = DBI->connect('dbi:WMI:'); my $sth = $dbh->prepare('SELECT * FROM Win32_ComputerSystemProduct'); $sth->execute(); while (my @row = $sth->fetchrow) { print $row[0]->{UUID}, "\n"; }

      Nice one! My first reaction was "someone wrote a DBD module to query some Windows API". But of course, there is a DBD for stuff like that. There always is.

      perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'
        > "someone wrote a DBD module to query some Windows API"

        FYI: The dominant "Windows API" of WMI is WQL which is kind of SQL, so it's not really that surprising

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re: running command line from Perl
by Anonymous Monk on Jun 08, 2022 at 02:10 UTC
    In Windows you mostly can use:
    use Win32::OLE('in'); use constant wbemFlagReturnImmediately => 0x10; use constant wbemFlagForwardOnly => 0x20; $computer = "."; $objWMIService = Win32::OLE->GetObject ("winmgmts:\\\\$computer\\root\\CIMV2") or die "WMI connection fai +led.\n"; $colItems = $objWMIService->ExecQuery ("SELECT * FROM Win32_ComputerSystemProduct","WQL",wbemFlagReturnI +mmediately | wbemFlagForwardOnly); foreach my $objItem (in $colItems) { print "UUID: $objItem->{UUID}\n"; print "\n"; }
    Remember "There's always more than one way to do it."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-23 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found