我想在C++编程中执行PowerShell命令。我已经编辑好了需要运行的命令语句。PowerShell_CMD、XML_File
-auto
添加到参数中。在审查提供的图像后,我也会更改PowerShell代码。$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
#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");
}
system("powershell 'echo \"hello from powershell\"'");
?不过我不知道对于比这更复杂的东西有多实用... - Blaze