通过 VB.NET 的 Powershell.哪种方法以及为什么? [英] Powershell via VB.NET. Which method and why?

查看:49
本文介绍了通过 VB.NET 的 Powershell.哪种方法以及为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过我的代码调用 Powershell 命令,并且我找到了至少 2 个不同的执行此操作的示例.我想知道这些方法之间有什么区别,以及为什么我要使用一种而不是另一种.

I need to call Powershell commands via my code and I find at least 2 different examples of doing this. I'm wondering what the differences between the methods are and why I would use one as opposed to the other.

第一个(更简单?)方法是这样的:

The first (simpler?) method goes something like this:

Dim command As New PSCommand()
command.AddScript("<Powershell command here>")
Dim powershell As Management.Automation.PowerShell = powershell.Create()
powershell.Commands = command
Dim results = powershell.Invoke()

results 现在包含一组可以转换为字符串的 Powershell 对象,例如:

results now contains a collection of Powershell objects that can be converted to strings, for example:

MsgBox(results.Item(0).ToString())

第二种方法如下所示:

Dim invoker As New RunspaceInvoke
Dim command As String = "<Powershell command here>"
Dim outputObjects As Collection(Of PSObject) = invoker.Invoke(command)

然后我可以遍历返回的对象集合并以相同的方式转换为字符串:

And then I can iterate through the collection of returned objects and convert to string in the same way:

For Each result As PSObject In outputObjects
    Console.WriteLine(result.ToString)
Next

我也知道,使用任何一种方法,我都可以将命令通过管道传送到 out-string 以使 Powershell 返回字符串而不是对象.

I also know that with either method I can pipe the command to out-string to make Powershell return strings instead of objects.

我的问题是,我应该使用哪种方法,为什么?他们在我看来都一样.

My question is, which method should I use and why? They both seem the same to me.

推荐答案

让我们开始一一了解:

PowerShell 对象: 提供用于创建命令管道并在运行空间内同步或异步调用这些命令的方法.此类还提供对包含调用命令时生成的数据的输出流的访问.此类主要用于以编程方式使用 Windows PowerShell 执行任务的主机应用程序.此处的管道专用于宿主应用程序,您可以特定于 PowerShell 对象.这也保证了即使在同步和异步发送命令时,您也可以访问 powershell 结果数据.

PowerShell Object: Provides methods that are used to create a pipeline of commands and invoke those commands either synchronously or asynchronously within a runspace. This class also provides access to the output streams that contain data that is generated when the commands are invoked. This class is primarily intended for host applications that programmatically use Windows PowerShell to perform tasks. The pipeline here is exclusive to host application and you can make specific to PowerShell Objects. This also guarantee that you will have access to powershell results data even when the commands are sent sync and async.

RunspaceInvoke: 允许从 CLR 语言执行命令.如果宿主应用程序需要显式定义管道,则不能使用它.这将使用现有的对象管道并为您添加 Powershell 命令.您可能在管道中还有其他命令,并且还将包含 powershell 命令.在这里,您正在使用 CLR Pipeline 对象并向其添加更多命令,并且该管道不是您的应用程序所独有的.

RunspaceInvoke: Allows the execution of commands from a CLR language. It can not be use if host application needs to explicitly define a pipeline. This is going to use an existing object pipeline and add Powershell commands for you. You might have other commands in the pipeline and powershell commands will be included as well. Here you are using a CLR Pipeline object and adding more commands to it and the pipeline is not exclusive to your application.

最后,我想说使用 Powershell 对象是在任何要托管 Powershell 对象的应用程序中使用的最佳选择.

Finally I would say using Powershell object is the best choice to use in any application which is going to host Powershell objects.

这篇关于通过 VB.NET 的 Powershell.哪种方法以及为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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