簡體   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