从 C# (ASP.NET) 调用 Powershell - 传递自定义对象? [英] Calling Powershell from C# (ASP.NET) - pass custom object?
问题描述
我在尝试从 C# 调用 Powershell 函数时遇到问题.具体来说,我在尝试将项目的通用列表传递给 powershell 模块函数时遇到了困难.代码如下:
I'm having trouble trying to call a Powershell function from C#. Specifically, I'm getting stuck trying to pass a generic list of Project's to a powershell module function. Here is the code:
var script = @". \\server01\Modules\Runspace.ps1; Get-MyCommand";
var allProjects = _pmRepository.GetAllProjects();
using (Runspace runSpace = RunspaceFactory.CreateRunspace())
{
runSpace.Open();
PowerShell posh = PowerShell.Create();
posh.Runspace = runSpace;
posh.AddScript(script);
posh.AddArgument(allProjects);
Collection<PSObject> results = posh.Invoke();
}
GetAllProjects() 方法返回一个通用的 Project 列表,Project 是一个自定义类.我的模块函数签名如下所示:
The GetAllProjects() method returns a generic list of Project's and Project is a custom class. My module function signature looks like this:
function Get-MyCommand
{
[CmdletBinding()]
Param
(
[Parameter(ValueFromPipeline = $true)]
[PSCustomObject[]] $Projects
)
Begin
{}
Process
{
$consumables = New-GenericList "Company.Project.Entities.Project"
foreach($project in $projects)
{
if ($project.State -eq $ProjectStates.Development)
{
$consumables.Add($project)
}
}
}
}
当我尝试遍历数组时出现此错误:
I'm getting this error when I try to iterate over the array:
{"在此对象上找不到属性 'State'.请确保它存在."}
{"Property 'State' cannot be found on this object. Make sure that it exists."}
可以做我想做的事吗?
我使用了以下代码一段时间,但最终使用了此 Web 应用程序的后端 C# 代码.创建 powershell 会话的加载时间对于我们的情况来说太长了.希望这会有所帮助.
I used the below code for a while, but ended up consuming the back-end C# code for this web application. The load time to create a powershell session was just too great for our situation. Hope this helps.
private void LoadConsumableProjects()
{
var results = new Collection<PSObject>();
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(_modules);
using (Runspace runSpace = RunspaceFactory.CreateRunspace(iss))
{
runSpace.Open();
using (var ps = PowerShell.Create())
{
ps.Runspace = runSpace;
ps.AddScript("Get-LatestConsumableProjects $args[0]");
ps.AddArgument(Repository.GetAllProjects().ToArray());
results = ps.Invoke();
if (ps.Streams.Error.Count > 0)
{
var errors = "Errors";
}
}
}
_projects = new List<Project>();
foreach (var psObj in results)
{
_projects.Add((Project)psObj.BaseObject);
}
}
推荐答案
好的,我把它作为答案,因为我认为你可以测试它.据我了解您的代码,您将参数传递给您的脚本:
Ok I put it as an answer because, I think you can test it. As I understand your code your pass an argument to your script :
posh.AddArgument(allProjects);
但是在您的脚本中,您不使用此参数传递给您的函数.对我来说,你可以测试:
But inside your script you don't use this argument to pass to your Function. For me you can test :
var script = @". \\server01\Modules\Runspace.ps1; Get-MyCommand $args[0]";
在您的脚本中,您点源Runspace.ps1",然后不带参数调用您的函数 Get-MyCommand.PowerShell 进入 foreach 循环,$project 等于 null.如果 $Projects 为空,则发出哔声.
In your script, you dot source 'Runspace.ps1' and then call your function Get-MyCommand without parameters. PowerShell get into the foreach loop with $project equal to null. Just beep if $Projects is null.
这篇关于从 C# (ASP.NET) 调用 Powershell - 传递自定义对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!