繁体   English   中英

C ++应该如何执行PowerShell命令?

[英]How should C++ execute the PowerShell command?

我想在C ++编程中执行PowerShell命令。 我已经编辑了需要运行的命令语句。 PowerShell_CMDXML_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


更简单易懂。

要在c ++中运行此代码,您应该将代码转储到一个临时文件中。

#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");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM