[英]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 ,但我正在寻找适当的方法来做到这一点
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.