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 ...
| [reply] [d/l] |
|
| [reply] |
|
| [reply] |
|
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";
| [reply] [d/l] [select] |
|
> 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.
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>
| [reply] [d/l] [select] |
|
|
|
|
| [reply] [d/l] |
|
Please look at my update for another way to do it :)
| [reply] |
|
Re: running command line from Perl
by soonix (Canon) on May 18, 2022 at 13:16 UTC
|
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";
}
| [reply] [d/l] |
|
| [reply] [d/l] |
|
| [reply] |
|
|
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." | [reply] [d/l] |