Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/lib/windows-elevation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,21 @@ export function runWindowsElevatedScheduledTaskRegistration(
xml: string,
): Promise<number> {
const xmlBase64 = Buffer.from(xml, "utf16le").toString("base64");
const powerShellPath = windowsPowerShell();
const powerShellDirectory = powerShellPath.replace(/[\\/][^\\/]+$/, "");
const scheduledTasksModule = `${powerShellDirectory}\\Modules\\ScheduledTasks\\ScheduledTasks.psd1`;
const inner = [
`$taskName = ${psSingleQuote(taskName)}`,
`$xmlBase64 = ${psSingleQuote(xmlBase64)}`,
"$xml = [Text.Encoding]::Unicode.GetString([Convert]::FromBase64String($xmlBase64))",
"Register-ScheduledTask -TaskName $taskName -Xml $xml -Force -ErrorAction Stop | Out-Null",
`$module = Microsoft.PowerShell.Core\\Import-Module -Name ${psSingleQuote(scheduledTasksModule)} -PassThru -Force -ErrorAction Stop`,
"$registerTask = $module.ExportedCommands['Register-ScheduledTask']",
"if ($null -eq $registerTask) { throw 'Trusted ScheduledTasks module does not export Register-ScheduledTask.' }",
"& $registerTask -TaskName $taskName -Xml $xml -Force -ErrorAction Stop | Out-Null",
].join("; ");
const encodedCommand = Buffer.from(inner, "utf16le").toString("base64");
const script = [
`$p = Start-Process -FilePath ${psSingleQuote(windowsPowerShell())}`,
`$p = Start-Process -FilePath ${psSingleQuote(powerShellPath)}`,
` -ArgumentList ${psSingleQuote(buildWindowsElevatedArgumentList([
"-NoProfile",
"-NonInteractive",
Expand Down
11 changes: 10 additions & 1 deletion tests/windows-elevation-spawn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,16 @@ describe("runWindowsElevated spawn contract", () => {
const match = /-EncodedCommand ([A-Za-z0-9+/=]+)/.exec(commandScript);
expect(match).not.toBeNull();
const elevatedScript = Buffer.from(match![1]!, "base64").toString("utf16le");
expect(elevatedScript).toContain("Register-ScheduledTask -TaskName $taskName -Xml $xml -Force");
expect(elevatedScript).toContain(
"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\Modules\\ScheduledTasks\\ScheduledTasks.psd1",
);
expect(elevatedScript).toContain("Microsoft.PowerShell.Core\\Import-Module");
expect(elevatedScript).toContain("$module.ExportedCommands['Register-ScheduledTask']");
expect(elevatedScript).toContain(
"Trusted ScheduledTasks module does not export Register-ScheduledTask.",
);
expect(elevatedScript).toContain("& $registerTask -TaskName $taskName -Xml $xml -Force");
expect(elevatedScript.match(/\bRegister-ScheduledTask\b/g)).toHaveLength(2);
expect(elevatedScript).toContain(Buffer.from(xml, "utf16le").toString("base64"));
expect(commandScript).not.toContain("/xml");
expect(commandScript).not.toContain("task.xml");
Expand Down
Loading