How should C++ execute the PowerShell command?

13,233

My assumption here is that you're trying to add -auto to the arguments. After reviewing the supplied image I would change the PowerShell code as well.

$task = Get-ScheduledTask "Test"
$items = @{}
if ($task.Actions.Execute -ne $null) {$items.Add('Execute', "$($task.Actions.Execute)")} 
$items.Add('Argument', "$($task.Actions.Arguments) -auto") 
if ($task.Actions.WorkingDirectory -ne $null) {$items.Add('WorkingDirectory',"$($task.Actions.WorkingDirectory)")} 
$action = New-ScheduledTaskAction @items
$task.Actions = $action
Set-ScheduledTask -InputObject $task


Far simpler and easier to understand.

To run this in c++ you should dump the code to a temporary file.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream file;
    file.open("test.ps1");

    string newArg = "-auto";
    string powershell;
    powershell = "$task = Get-ScheduledTask \"Test\"\n";
    powershell += "$items = @{}\n";
    powershell += "if ($task.Actions.Execute -ne $null) {$items.Add('Execute', \"$($task.Actions.Execute)\")} \n";
    powershell += "$items.Add('Argument', \"$($task.Actions.Arguments) " + newArg + "\") \n"; // 'Argument' not a typo
    powershell += "if ($task.Actions.WorkingDirectory -ne $null) {$items.Add('WorkingDirectory',\"$($task.Actions.WorkingDirectory)\")} \n";
    powershell += "$action = New-ScheduledTaskAction @items\n";
    powershell += "$task.Actions = $action\n";
    powershell += "Set-ScheduledTask -InputObject $task\n";
    file << powershell << endl;
    file.close();

    system("powershell -ExecutionPolicy Bypass -F test.ps1");

    remove("test.ps1");
}
Share:
13,233
kevin
Author by

kevin

Updated on June 04, 2022

Comments

  • kevin
    kevin almost 2 years

    I want to execute the PowerShell command in C + + programming. I've edited the command statement that needs to be run.PowerShell_CMDXML_File

    • Blaze
      Blaze about 5 years
      How about something like system("powershell 'echo \"hello from powershell\"'");? I don't know how practical it is from anything more elaborate than that though...
    • Matt R
      Matt R about 5 years
      I think it might be prudent to dump out a temporary ps1 file for something as complicated as what is shown in your image. Then @Blaze's answer could be adjusted to system("powershell -f temp.ps1");
  • kevin
    kevin about 5 years
    I call system () to execute when the script is disabled, I've changed the settings with the command--"Set-executionpolicy remotesigned", but I can do it when I right-click the PowerShell
  • Matt R
    Matt R about 5 years
    So you're saying we can't use -f file.ps1 ?
  • kevin
    kevin about 5 years
    Yes, I do not know why I use System () to perform the prompt to disable, manually right-click to do it.
  • kevin
    kevin about 5 years
    I uploaded another XML file diagram, want to increase the red part of the node, without PowerShell implementation,can you help me write the command statement to increase this node?
  • Matt R
    Matt R about 5 years
    I've added a bypass for the ExecutionPolicy and made it copy over existing values if they exist. This should run and add any parameter you like in the newArg string.
  • kevin
    kevin about 5 years
    thanks!I used it to do it -----system("powershell -ExecutionPolicy Bypass -F test.ps1");
  • keineahnung2345
    keineahnung2345 over 3 years
    Can we just run a command without creating a temporary .ps file?
  • Matt R
    Matt R over 3 years
    @keineahnung2345You can by doing something like system(("powershell -ExecutionPolicy Bypass -Command \"& {" + powershell + "}\"").c_str()); - Notice a string 'powershell' is used for the command and it's wrapped in (string-goeshere).c_str(); to convert it to the necessary type.