与您上一个问题的答案类似,在更正您的代码中的以下问题后,以下自包含示例代码演示了该方法原则上有效:
和.AddStatement()
调用之间缺少一个调用;这对于(基于脚本块的)调用和被视为单独语句的调用是必要的。.AddScript()
.AddCommand()
Import-Module
Get-RowAndPartitionKeys
伪代码行string scriptContents = "Import-Module 'C:\Users\...\Powershell.dll";
缺少关闭'
(可能只是在此处发布的工件)。
此外,下面添加了故障排除代码。
虽然编排代码在 PowerShell 中,但使用通过.NET SDK编译的实际 C# 项目,并结合PowerShell(核心)SDK 包7.1.2
的版本,.Microsoft.PowerShell.SDK
在运行创建和运行测试项目的代码后,您可以自己检查和试验它们 (./module
是定义Get-RowAndPartitionKeys
cmdlet的模块 DLL./app
的项目,是调用它的应用程序的项目):
$tmpDir = (New-Item -Force -Type Directory (Join-Path temp: $PID)).FullName
$tmpModuleDir = (New-Item -Force -Type Directory (Join-Path $tmpDir module)).FullName
$tmpAppDir = (New-Item -Force -Type Directory (Join-Path $tmpDir app)).FullName
$tmpPublishDir = (New-Item -Force -Type Directory (Join-Path $tmpDir publish)).FullName
$tmpModuleDll = Join-Path $tmpPublishDir module.dll
Push-Location
# ---
Write-Verbose -vb "Creating module DLL with sample cmdlet..."
Set-Location $tmpModuleDir
dotnet new classlib --force >$null || $(exit $LASTEXITCODE)
dotnet add package Microsoft.PowerShell.SDK >$null || $(exit $LASTEXITCODE)
@'
using System;
using System.Collections.Generic;
using System.Management.Automation;
namespace demo {
public class GetRowAndPartitionKeys : Cmdlet
{
public List<string> Properties { get; set; }
}
[Cmdlet( VerbsCommon.Get, "RowAndPartitionKeys" )]
public class GetRowAndPartitionKeyCmd : GetRowAndPartitionKeys
{
protected override void ProcessRecord()
{
WriteObject ("Hi");
}
}
}
'@ | Set-Content Class1.cs
dotnet publish -o $tmpPublishDir >$null || $(exit $LASTEXITCODE)
# ---
Write-Verbose -vb "Creating console application that imports the module DLL and calls the sample cmdlet..."
Set-Location $tmpAppDir
dotnet new console --force >$null || $(exit $LASTEXITCODE)
dotnet add package Microsoft.PowerShell.SDK >$null || $(exit $LASTEXITCODE)
@"
using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Threading.Tasks;
namespace demo {
public static class App {
static void Main(string[] args)
{
var unused = new Foo().RunScript().Result;
}
}
public class Foo {
public async Task<IEnumerable<object>> RunScript()
{
string scriptContents = @"Import-Module -Verbose ""$tmpModuleDll""";
using(PowerShell ps = PowerShell.Create())
{
ps.AddScript(scriptContents).AddStatement().AddCommand("Get-RowAndPartitionKeys");
var pipelineObjects = await ps.InvokeAsync().ConfigureAwait( false );
// --- TROUBLESHOOTING CODE
// Print verbose output from the Import-Module call
foreach (var v in ps.Streams.Verbose) { Console.WriteLine("VERBOSE: " + v.ToString()); }
// Print any errors.
foreach (var e in ps.Streams.Error) { Console.WriteLine("ERROR: " + e.ToString()); }
// ---
foreach (var item in pipelineObjects)
{
Console.WriteLine(item.BaseObject.ToString());
}
return pipelineObjects;
}
}
}
}
"@ | Set-Content Program.cs
# ---
Write-Verbose -vb "Compiling and invoking the console application..."
dotnet run
Pop-Location
Write-Verbose -vb @"
The test projects are located in $tmpDir.
To clean up, run:
Remove-Item "$tmpdir" -Recurse
"@
在我的 Windows 10 机器上(从 PowerShell 7.1.2 运行),我得到:
如你看到的:
- 详细输出确认 cmdlet 已正确导入
Hi
显示该 cmdlet 已成功调用。