简体   繁体   English

将自定义对象从C#传递给Powershell

[英]Pass a custom object from C# to Powershell

I have a C# class that should execute a Powershell script by passing it a Log object. 我有一个C#类,它应该通过传递一个Log对象来执行Powershell脚本。 Log is entirely written in C#, and should be shared across the C# code and Powershell to achieve common logging. Log完全用C#编写,并且应该在C#代码和Powershell之间共享以实现常见的日志记录。

Can someone please tell me how I can pass the custom object from C# to Powershell and use it there? 有人可以告诉我如何将自定义对象从C#传递给Powershell并在那里使用它吗?

I believe it using runspace will solve your problem. 我相信它使用运行空间将解决您的问题。 A good explanation and examples on them you can find in the following this link : http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C . 您可以在以下链接中找到有关它们的详细解释和示例: http//www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C

Just a generic idea of how to use them. 只是如何使用它们的一般概念。

if you have script : 如果你有脚本:

param($logger2)
$logger2.Write("logger2 Write was called");
$logger.Write("logger Write was called");

and you want to subsitute logger2 and logger use following code : 并且您希望替换logger2和logger使用以下代码:

String scriptContent = // get script content
Logger logger = new Logger();
Logger2 logger2 = new Logger2();
// create ps runspace
using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
    // start session
    runspace.Open();
    // set variables
    runspace.SessionStateProxy.SetVariable("logger", logger);
    // supply commands
    using (Pipeline pipeline = runspace.CreatePipeline())
    {
        // create ps representation of the script
        Command script = new Command(scriptContent, true, false);
        // suppy parameters to the script
        script.Parameters.Add(new CommandParameter("logger2", logger2));
        // send script to pipeline
        pipeline.Commands.Add(script);
        // execute it
        Collection<PSObject> results = pipeline.Invoke();
        // close runspace
        runspace.Close();
    }
}

You can share a class between PowerShell and C# by compiling your Log class into an assembly and use LoadFile in PowerShell (should work in V1 and V2) 您可以通过将Log类编译为程序集并在PowerShell中使用LoadFile来共享PowerShell和C#之间的类(应该在V1和V2中工作)

$lib = [System.Reflection.Assembly]::LoadFile('path to your dll');
$lib.Log("Terrible things have happened!")

If however, you want to share an instance of a class between PowerShell and C# you could use SessionStateProxy.SetVariable . 但是,如果要在PowerShell和C#之间共享类的实例,则可以使用SessionStateProxy.SetVariable

If you just need to use the class in a powershell script, you can put the actual C# code into the script itself and use that as the definition for a type. 如果您只需要在powershell脚本中使用该类,则可以将实际的C#代码放入脚本本身并将其用作类型的定义。

There is a tutorial here. 这里有一个教程。

Basically you use Add-Type -TypeDefinition <C# code> -Language CSharp 基本上你使用Add-Type -TypeDefinition <C# code> -Language CSharp

Add the reference to system.management.automation.dll to your project and use the PowerShell Class . system.management.automation.dll的引用添加到项目中并使用PowerShell类 See the example there, it is almost ready, change the line 看那里的例子,它几乎准备好了,改变线

ps.AddCommand("Get-Process");

to something like: 类似于:

ps.AddCommand(<script-path/name-of-script-in-path>).AddParameter("Log", logInstance);

It tells to invoke your script with the parameter Log which value is an instance logInstance to be passed in the script. 它告诉您使用参数Log调用脚本,该值是要在脚本中传递的实例logInstance The script should have the parameter Log and use it as $Log in its code: 该脚本应具有参数Log并将其用作其代码中的$Log

param($Log)

$Log.Write("Begin ...")
...
$Log.Write("End ...")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM