如何为powershell脚本(系统.管理.自动化.Powershell)提供参数?

hmae6n7t  于 2023-02-04  发布在  Shell
关注(0)|答案(2)|浏览(136)

我需要从C#应用程序运行PowerShell脚本。我喜欢为此使用方法AddScript。它运行得很好。但是,当我使用方法AddParameters向脚本添加参数时,它似乎无法按预期运行。
以下是测试负载(PowerShell):

param ([string]$Arg1, [string]$Arg2, [switch]$ArgParamless)
$filename = "payload_with_params.txt"
$filepath = $env:temp
$fullpath = Join-Path -Path $filepath -ChildPath $filename

$dt = Get-Date -Format "yyyy.MM.dd HH:mm:ss"
$val = $dt+' '+$Arg1+' '+$Arg2+' '+$ArgParamless
Add-Content -Path $fullpath -Value "$val"

如果我像这样从PS推它,它就能正常工作:

.\payload_with_params.ps1 -Arg1 "Bla 1" -Arg2 "Bla 2" -ArgParamless

结果:

2023.01.19 16:58:10 Bla 1 Bla 2 True
    • C#**代码(过于简化):
string command = File.ReadAllText(pathToPS1);

List<CommandParameter> paramList = new List<CommandParameter>();
paramList.Add(new CommandParameter("Arg1", "Bla 1"));
paramList.Add(new CommandParameter("Arg2", "Bla 2"));
paramList.Add(new CommandParameter("ArgParamless"));

using (PowerShell ps = PowerShell.Create())
{
    //Adding script file content to object
    ps.AddScript(command);
    if (paramList != null)
    {
        if (paramList.Count > 0)
        {
            //Adding Params to object;
            ps.AddParameters(paramList);    
        }
    }
    //Launching
    ps.Invoke();
}

结果是:

2023.01.19 16:54:00 System.Management.Automation.Runspaces.CommandParameter System.Management.Automation.Runspaces.CommandParameter False

所以...它没有像我预期的那样工作。我应该如何为脚本提供参数?

7fyelxc5

7fyelxc51#

对于[switch]参数,您需要绑定一个bool- PowerShell会将true解释为“存在”,将false解释为“不存在”:

paramList.Add(new CommandParameter("ArgParamless", true));
txu3uszq

txu3uszq2#

事实证明AddParameter(CommandParameter)方法不起作用。
这种方法正在发挥作用:

string command = File.ReadAllText(pathToPS1);
using (PowerShell ps = PowerShell.Create())
{
    //Adding script file content to object
    ps.AddScript(command);
    if (paramList != null)
    {
        if (paramList.Count > 0)
        {
            //Adding Params to object;
            ps.AddArgument("-Arg1 XXXXXX");
        }
    }
    //Launching
    ps.Invoke();
}

在PowerShell端,可以从$args数组中提取此参数以供使用:

$arglist = $args -join " "

使用哈希表的更好方法:

string command = File.ReadAllText(pathToPS1);
using (PowerShell ps = PowerShell.Create())
{
    //Adding script file content to object
    ps.AddScript(command);
    if (paramList != null)
    {
        if (paramList.Count > 0)
        {
            //Adding Params to object;
            var hashtable = new Hashtable {
                { "Arg1", "XXXXXX" },
                { "Arg2", "YYYYYY" },
                { "ArgParamless", true }
            };
            ps.AddArgument(hashtable);
        }
    }
    //Launching
    ps.Invoke();
}

这就是您在PS端使用它的方法:

function Wrapper-Test ([string]$Arg1, [string]$Arg2, [switch]$ArgParamless) {
    $Result = $Args1+' '+$Args2+' '+$ArgParamless
    return $Result
}

$MyArgs = "";
$MyArg = $args[0]
$MyArgs = Wrapper-Test @MyArg;

$filename = "payload_with_params.txt"
$filepath = $env:temp
$fullpath = Join-Path -Path $filepath -ChildPath $filename

$dt = Get-Date -Format "yyyy.MM.dd HH:mm:ss"
$arglist = $args -join " "
$val = $dt + " " + $MyArgs
Add-Content -Path $fullpath -Value "$val"

相关问题