C# PowerShell对象返回null

C# PowerShell对象返回null,c#,powershell,powershell-module,powershell-sdk,C#,Powershell,Powershell Module,Powershell Sdk,我有下面的命令,它返回空对象。当我在PowerShell窗口中单独运行该命令时,我得到了正确的结果。下面是我的PowerShell方法,它正在调用该命令以及我定义的PowerShell命令。我基本上希望返回一个字符串值。请让我知道我做错了什么 C#方法: 使用PowerShell SDK时,如果要使用.AddParameter()/.AddParameters()/AddArgument(),将参数传递给单个命令,请使用,而不是.AddScript() 用于传递作为脚本块执行的任意PowerSh

我有下面的命令,它返回空对象。当我在PowerShell窗口中单独运行该命令时,我得到了正确的结果。下面是我的PowerShell方法,它正在调用该命令以及我定义的PowerShell命令。我基本上希望返回一个字符串值。请让我知道我做错了什么

C#方法:


使用PowerShell SDK时,如果要使用
.AddParameter()
/
.AddParameters()
/
AddArgument()
将参数传递给单个命令,请使用,而不是
.AddScript()

用于传递作为脚本块执行的任意PowerShell代码段,使用
.AddParameters()
添加的参数将传递到该脚本块

也就是说,您的调用相当于
和{Get RowAndPartitionKey}
,您可以看到,
Get RowAndPartitionKey
命令因此不接收参数值

请参阅一个或多个信息


注意:作为调用自定义
Get-RowAndPartitionKey
cmdlet的先决条件,您可能必须显式导入包含它的模块(DLL),您可以执行以下操作:

  • 或者:使用预先执行的单独、同步的
    导入模块
    调用(为简单起见,我在这里使用
    .AddArgument()
    ,按位置传递一个参数,该参数绑定到
    -Name
    参数(也接受路径)):

指包含
Get-RowAndPartitionKey
cmdlet的模块的完整文件系统路径;根据该模块的实现方式,它要么是模块目录的路径,要么是模块的
.psd1
模块清单的路径,要么是模块的
.dll
的路径(如果它是独立程序集)

替代导入方法,使用PowerShell SDK的专用方法

此方法无需在会话中调用导入模块,但需要额外设置:

  • 创建默认会话状态
  • 对其调用
    .ImportPSModule()
    ,以导入模块
  • 将此会话状态传递给PowerShell.Create()
var iss=InitialSessionState.CreateDefault();
iss.ImportPSModule(新字符串[]{@'});
var ps=PowerShell.Create(iss);
//现在,PowerShell命令已提交到'ps'实例
//将看到模块的导出命令。
警告:一个
PowerShell
实例在
.Runspace.InitialSessionState
中反映其初始会话状态,但在概念上是只读属性;棘手的是,它在技术上仍然是可修改的,因此错误的修改尝试被悄悄地忽略,而不是导致异常


要对这些调用进行故障排除,请执行以下操作:

  • .Invoke()
    /
    .EndInvoke()
    之后检查
    ps.haderors
    ,以查看PowerShell命令是否报告任何(非终止)错误

  • 枚举
    ps.Streams.Errors
    以检查发生的特定错误


查看演示这些技术的自包含示例代码的后续问题。

什么是
EntityProperty
,以及
ps.HadErrors
=
Invoke()
之后的
true
?EntityProperty是Microsoft.Azure.Cosmos.Table.EntityProperty。我没有检查ps.haderors。只是在BeginInvoke()之后检查了它的false
     public string RunScript( string contentScript, Dictionary<string, EntityProperty> parameters)
            {
                List<string> parameterList = new List<string>();
                foreach( var item in parameters )
                {
                    parameterList.Add( item.Value.ToString() );
                }
               
                using( PowerShell ps = PowerShell.Create() )
                {
                    ps.AddScript( contentScript );
// in ContentScript I get "Get-RowAndPartitionKey" on debugging
                    ps.AddParameters( parameterList );//I get list of strings
                   
                    IAsyncResult async = ps.BeginInvoke();
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach( PSObject result in ps.EndInvoke( async ) )
// here i get result empty in ps.EndInvoke(async)
                    {
                        stringBuilder.AppendLine( result.ToString() );
                    }
                    return stringBuilder.ToString();
                }
            }
        }
public abstract class GetRowAndPartitionKey : PSCmdlet
    {
        [Parameter]
        public List<string> Properties { get; set; } = new List<string>();
    }

    [Cmdlet( VerbsCommon.Get, "RowAndPartitionKey" )]
    public class GetRowAndPartitionKeyCmd : GetRowAndPartitionKey

    {
        protected override void ProcessRecord()
        {
            string rowKey = string.Join( "_", Properties );
            string pKey = string.Empty;
            
            WriteObject( new
            {
                RowKey = rowKey,
                PartitionKey = pKey
            } );
        }
    }
}
ps.AddCommand("Import-Module").AddArgument(@"<your-module-path-here>").Invoke();
IAsyncResult async = 
  ps.AddCommand("Import-Module").AddArgument(@"<your-module-path-here>")
    .AddStatement()
      .AddCommand("GetRowAndPartitionKey").AddParameter("Properties", parameterList)
          .BeginInvoke();
var iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { @"<your-module-path-here>" });
var ps = PowerShell.Create(iss);

// Now the PowerShell commands submitted to the `ps` instance
// will see the module's exported commands.