【发布时间】:2021-05-24 14:08:38
【问题描述】:
我在下面没有收到任何错误,但我也没有得到输出。下面是 Powershell cmd 和调用它的 C# 方法。我想知道它是否编写正确以及如何获得来自 powershell 的输出。当我从 PowerShell 窗口运行时它工作正常
Pwsh 命令:
public class GetRowAndPartitionKeys : Cmdlet
{
[Parameter(Mandatory = false)]
public List<string> Properties { get; set; } = new List<string>();
}
[Cmdlet( VerbsCommon.Get, "RowAndPartitionKeys" )]
public class GetRowAndPartitionKeyCmd : GetRowAndPartitionKeys
{
protected override void ProcessRecord()
{
WriteObject ("Hi");
}
}
}
C#方法:
public async Task<IEnumerable<object>> RunScript( )
{
// create a new hosted PowerShell instance using the default runspace.
// wrap in a using statement to ensure resources are cleaned up.
string scriptContents = "Import-Module 'C:\Users\...\Powershell.dll";
using( PowerShell ps = PowerShell.Create() )
{
// specify the script code to run.
ps.AddScript( scriptContents ).AddCommand( "Get-RowAndPartitionKeys" );
// execute the script and await the result.
var pipelineObjects = await ps.InvokeAsync().ConfigureAwait( false );
foreach( var item in pipelineObjects )
{
Console.WriteLine( item.BaseObject.ToString() );
}
return pipelineObjects;
}
【问题讨论】:
-
这个我的 C++ 是 C#
-
为什么你认为是c++?页面顶部显示 c#。
-
你发送的那个显示c++,但我改成c#但我不知道如何使用它,你能告诉我我目前的方法,我应该在哪里以及如何插入
-
试试看主页。您只需要获取流:docs.microsoft.com/en-us/dotnet/api/…
标签: c# powershell powershell-module powershell-sdk