I am trying to use Win32::OLE to create a task on a Windows 10 system, something similar to href:https://onedrive.live.com/?authkey=%21ABowWrIzVQzcElo&cid=07CD1B37769E1B7D&id=7CD1B37769E1B7D%21102691&parId=7CD1B37769E1B7D%21102690&o=OneUp. I tried to manually validate the query by creating the same task by hand in the task scheduler GUI, and that seemed to work.
Here's my test code:
use File::Basename;
use Win32;
use Win32::OLE;
$Win32::OLE::Warn = 3;
use Data::Dumper;
my ($me, $dirpath, $suffix) = fileparse($0, qr/\.[^.]*/);
my ($system, $login, $domain, $sidbin, $sidtype, $sidtxt) = "";
$login = Win32::LoginName();
Win32::LookupAccountName($system, $login, $domain, $sidbin, $sidtype);
my($Revision, $SubAuthorityCount,@IdentifierAuthorities) = unpack("CCn
+nn", $sidbin);
unless (($IdentifierAuthorities[0] || $IdentifierAuthorities[1])) {
my($temp, $temp2, @SubAuthorities) = unpack("VVV$SubAuthorityCount
+",$sidbin);
$sidtxt = "S-$Revision-$IdentifierAuthorities[2]-".join("-",@SubAu
+thorities);
}
die Win32::OLE->LastError() unless (my $service = Win32::OLE->CreateOb
+ject('Schedule.Service'));
$service->Connect;
my $RootFolder = $service->GetFolder('\\');
die Win32::OLE->LastError() unless (my $TaskDefinition = $service->New
+Task(0));
die Win32::OLE->LastError() unless (my $regInfo = $TaskDefinition->Reg
+istrationInfo);
$regInfo->{Description} = "Register a perl task as an event $me";
$regInfo->{Author} = "$domain\\$login";
$regInfo->{URI} = "$sidtxt\\$me";
die Win32::OLE->LastError() unless (my $settings = $TaskDefinition->Se
+ttings);
$settings->{Enabled} = 1;
$settings->{AllowDemandStart} = 1;
$settings->{DisallowStartIfOnBatteries} = 0;
$settings->{StopIfGoingOnBatteries} = 0;
$settings->{Hidden} = 0;
my @Triggers;
my $TriggerSet;
die Win32::OLE->LastError() unless ($TriggerSet = $TaskDefinition->Tri
+ggers);
for (10000..10001) {
die Win32::OLE->LastError() unless (push @Triggers, $TriggerSet->C
+reate(0));
$Triggers[$#Triggers]->{Id} = $_;
$Triggers[$#Triggers]->{Subscription} =
"<QueryList>
<Query Id=\"event$_\" Path=\"Microsoft-Windows-NetworkProfile/Operat
+ional\">
<Select Path=\"Microsoft-Windows-NetworkProfile/Operational\">*[Sy
+stem[(EventID=\"$_\")]]</Select>
</Query>
</QueryList>";
die Win32::OLE->LastError()
unless (my $values = $Triggers[$#Triggers]->ValueQueries->Create
+("eventId", "Event/System/EventID"));
$Triggers[$#Triggers]->{Enabled} = 1;
}
die Win32::OLE->LastError() unless (my $Action = $TaskDefinition->Acti
+ons()->Create(0));
$Action->{Path} = 'C:\Perl64\Bin\Perl.exe';
$Action->{Arguments} = "$0 -f event\${eventID}";
$RootFolder->RegisterTaskDefinition("OLE-Test",$TaskDefinition,6,undef
+,undef,3);
print Dumper $TaskDefinition->{XmlText};
If I run the code with RegisterTaskDefinition with TASK_VALIDATE_ONLY flag set (third parameter = 1), I get a nice XML dump. So far so good.
When I run the code with RegisterTaskDefinition with TASK_CREATE_OR_UPDATE (third parameter = 6), I get this error:
OLE exception from "<Unknown Source>":
(11,263):Subscription:<QueryList><Query Id="event10000"
Path="Microsoft-Windows-NetworkProfile/Operational"><Select
Path="Microsoft-Windows-NetworkProfile/Operational">*[System[(EventID=
+"10000")]]</Select></Query></QueryList>
Win32::OLE(0.1712) error 0x80073a99: "The specified query is invalid"
in METHOD/PROPERTYGET "RegisterTaskDefinition" at OLE-test.pl line
+ 63.
Anyone familiar enough with Win32::OLE, and the Windows task scheduler XML to explain what I'm doing wrong
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.