简体   繁体   English

Powershell中的C#管道(|)等价于Add-Type导入函数?

[英]C# pipe ( | ) equivalent in Powershell for Add-Type imported functions?

I want to call a C# function in PowerShell, but with arguments containing pipes. 我想在PowerShell中调用C#函数,但参数包含管道。 I can call it this way in C# SomeCSharpFunction( THING | OTHERTHING, ARG2, ARG3) , but I can't find a way to do the same thing once I imported the function in PowerShell with Add-Type . 我可以在C# SomeCSharpFunction( THING | OTHERTHING, ARG2, ARG3)但是一旦我在PowerShell中使用Add-Type导入了函数,我就找不到找到相同方法的方法。

The imported function works as expected if I have "single" arguments like this 如果我有这样的“单个”参数,导入的函数将按预期工作

[Imported.Functions]::SomeCSharpFunction($Arg1,$Arg2,$Arg3)  #This works

But I can't set $Arg1 to be $Thing | $OtherThing 但是我不能将$Arg1设置$Arg1 $Thing | $OtherThing $Thing | $OtherThing , because PowerShell interpret the pipe differently, nor can I call the functon $Thing | $OtherThing ,因为PowerShell用不同的方式解释管道,我也不能称其为functon

[Imported.Functions]::SomeCSharpFunction( $Thing | $OtherThing, $Arg2, $Arg3) #This doesn't

I tried a few other separators in PowerShell, tried to somehow escape the | 我在PowerShell中尝试了其他一些分隔符,试图以某种方式转义| character (`|, \\|, '|', none did the trick ) , tried to send a PowerShell array @($Thing,OtherThing) ) 字符(`|,\\ |,'|',没有花招),尝试发送PowerShell数组@($Thing,OtherThing)

My online searches about this were unsuccessful, as search engines seemed to ignore the | 我对此的在线搜索未成功,因为搜索引擎似乎忽略了| character. 字符。

A workaround I'm currently using is to define a C# Wrapper function that accepts multiple arguments, and then calls SomeCSharpFunction with these arguments "merged" with a | 我当前正在使用的解决方法是定义一个C#包装函数,该函数接受多个参数,然后使用| |合并这些参数来调用SomeCSharpFunction , but I'm searching for the proper way to do that if there's any ,但我正在寻找适当的方法来做到这一点


Example

In my PowerShell Script, I used Add-Type to "import" a function from Kernel32, like this 在我的PowerShell脚本中,我使用Add-Type从Kernel32“导入”一个函数,就像这样

$Signature = @"
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
"@

$Kernel32 = Add-Type -MemberDefinition $Signature -Name "Process" -Namespace Win32Functions -PassThru

I can then call it inside PowerShell with $Kernel32::OpenProcess($DesiredAccess,$InheritHandle, $ProcessId) , which works perfectly if my arguments are "unique". 然后,我可以在PowerShell内部使用$Kernel32::OpenProcess($DesiredAccess,$InheritHandle, $ProcessId)调用它,如果我的参数是“唯一的”,则可以完美地工作。

Now, in C# I can call it this way (note that I already set PROCESS_VM_OPERATION and PROCESS_VM_READ to their respective uint values) 现在,在C#中,我可以这样称呼(注意,我已经将PROCESS_VM_OPERATION和PROCESS_VM_READ设置为各自的uint值)

OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ, false, vProcessId)

I'm trying to achieve the same thing but in PowerShell, as explained in the General part. 正如常规部分中所述,我正在尝试在PowerShell中实现相同的目的。

To combine the (enum) values, you should use the + operator (if the binary values don't overlap) or the -bor operator (Boolean or, if they do overlap. 要合并(枚举)值,应使用+运算符(如果二进制值不重叠)或-bor运算符(布尔值,如果它们确实重叠)。

If you have constants from the same enum, you can do something like this, too: [FlagsEnumType] "Value1, Value3" 如果您具有来自同一枚举的常量,则也可以执行以下操作:[FlagsEnumType]“ Value1,Value3”

here is a link to a post about using enum values in PowerShell. 是有关在PowerShell中使用枚举值的文章的链接。

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

相关问题 Powershell使用来自Add-Type的C#代码崩溃shell(SystemParamtersInfo) - Powershell using C# code from Add-Type crashes shell (SystemParamtersInfo) PowerShell Add-Type Simple C# 结构——一种有效,一种无效 - PowerShell Add-Type Simple C# Structures -- One works and one doessn't Powershell Add-Type在编译.dll(C#)时禁止创建.pdb - Powershell Add-Type suppress creation of .pdb when compiling .dll (C#) 如何使用 Add-Type C# 代码禁用 PowerShell Windows 窗体中的关闭按钮? - How to disable close button in PowerShell Windows Forms using Add-Type C# code? `Add-Type` C#6+具有抛出错误的功能 - `Add-Type` C# 6+ features throwing errors PowerShell 添加类型:无法添加类型。 已经存在 - PowerShell Add-Type : Cannot add type. already exist 如何通过命令行在 powershell 中使用 Add-Type 以执行 VB.NET 或 C#(或 JScript.net)代码? - How I can use Add-Type in powershell through command line in order to execute VB.NET or C# (or JScript.net) code? 如何使用Add-Type通过System.Windows.Forms命名空间添加C#代码? - How can I Use Add-Type to add C# code with System.Windows.Forms namespace? Add-Type PowerShell 2.0找不到方法 - Add-Type PowerShell 2.0 can't find method PowerShell:枚举名称中的点导致添加类型失败 - PowerShell: Dot in Enum Name Causing Add-Type to Fail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM