从 C# 调用 powershell cmdlet [英] Invoking powershell cmdlets from C#

查看:57
本文介绍了从 C# 调用 powershell cmdlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何从 C# 调用 PS cmdlet,并且遇到了 PowerShell 类.它适用于基本用途,但现在我想执行这个 PS 命令:

Get-ChildItem |其中 {$_.Length -gt 1000000}

我尝试通过 powershell 类构建它,但我似乎无法做到这一点.到目前为止,这是我的代码:

PowerShell ps = PowerShell.Create();ps.AddCommand("Get-ChildItem");ps.AddCommand("where-object");ps.AddParameter("长度");ps.AddParameter("-gt");ps.AddParameter("10000");//调用 PowerShell.Invoke() 方法来运行//管道命令.foreach (PSObject 结果在 ps.Invoke()){Console.WriteLine("{0,-24}{1}",result.Members["Length"].Value,result.Members["Name"].Value);}//结束foreach.

我运行这个时总是遇到异常.是否可以像这样运行 Where-Object cmdlet?

解决方案

Length, -gt10000 不是 Where-Object.只有一个参数,FilterScript 位于位置 0,其值为 ScriptBlock 类型,其中包含一个 表达式.

PowerShell ps = PowerShell.Create();ps.AddCommand("Get-ChildItem");ps.AddCommand("where-object");ScriptBlock 过滤器 = ScriptBlock.Create("$_.Length -gt 10000")ps.AddParameter("FilterScript", 过滤器)

如果您有更复杂的语句需要分解,请考虑使用分词器(在 v2 或更高版本中可用)以更好地理解结构:

 # 使用单引号允许 $_ 包含在字符串中PS>$script = 'Get-ChildItem |where-object -filter {$_.Length -gt 1000000 }'PS>$parser = [System.Management.Automation.PSParser]PS>$parser::Tokenize($script, [ref]$null) |选择内容,输入 |英尺 - 自动

这会转储以下信息.它不像 v3 中的 AST 解析器那么丰富,但它仍然很有用:

<前>内容类型------- ----Get-ChildItem 命令|操作员where-object 命令-filter 命令参数{ 组开始_ 多变的.操作员长度成员-gt 运算符1000000 号组结束

希望这会有所帮助.

I'm trying to learn how to call PS cmdlets from C#, and have come across the PowerShell class. It works fine for basic use, but now I wanted to execute this PS command:

Get-ChildItem | where {$_.Length -gt 1000000}

I tried building this through the powershell class, but I can't seem to do this. This is my code so far:

PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-ChildItem");
ps.AddCommand("where-object");
ps.AddParameter("Length");
ps.AddParameter("-gt");
ps.AddParameter("10000");


// Call the PowerShell.Invoke() method to run the 
// commands of the pipeline.
foreach (PSObject result in ps.Invoke())
{
    Console.WriteLine(
        "{0,-24}{1}",
        result.Members["Length"].Value,
        result.Members["Name"].Value);
} // End foreach.

I always get an exception when I run this. Is it possible to run the Where-Object cmdlet like this?

解决方案

Length, -gt and 10000 are not parameters to Where-Object. There is only one parameter, FilterScript at position 0, with a value of type ScriptBlock which contains an expression.

PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-ChildItem");
ps.AddCommand("where-object");
ScriptBlock filter = ScriptBlock.Create("$_.Length -gt 10000")
ps.AddParameter("FilterScript", filter)

If you have more complex statements that you need to decompose, consider using the tokenizer (available in v2 or later) to understand the structure better:

    # use single quotes to allow $_ inside string
PS> $script = 'Get-ChildItem | where-object -filter {$_.Length -gt 1000000 }'
PS> $parser = [System.Management.Automation.PSParser]
PS> $parser::Tokenize($script, [ref]$null) | select content, type | ft -auto

This dumps out the following information. It's not as rich as the AST parser in v3, but it's still useful:

    Content                   Type
    -------                   ----
    Get-ChildItem          Command
    |                     Operator
    where-object           Command
    -filter       CommandParameter
    {                   GroupStart
    _                     Variable
    .                     Operator
    Length                  Member
    -gt                   Operator
    1000000                 Number
    }                     GroupEnd

Hope this helps.

这篇关于从 C# 调用 powershell cmdlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆