我正在尝试调用 PowerShell 脚本文件中的函数。我在用WinUI 3
。我在调试器输出中得到难以理解的输出,并且 PowerShell 脚本似乎根本没有执行。我已验证当我直接从 PowerShell 运行 PowerShell 脚本(或直接在 Visual Studio 中启动它)时,PowerShell 脚本可以正确运行。
下面的C#代码用于调用该函数Setup-Workspaces
在 PowerShell 脚本中。请注意,数据类型workspace.Name
is string
。这是基于在这里回答 https://stackoverflow.com/questions/4179351/calling-powershell-functions-from-c-sharp.
/// <summary>
/// Launches the given workspace using the given powershell script.
/// </summary>
/// <param name="script"></param>
/// <param name="workspace"></param>
internal static void LaunchWorkspace(string script, Workspace workspace)
{
using (PowerShell powerShell = PowerShell.Create())
{
// Add the script to run from.
powerShell.AddScript(script, false);
powerShell.Invoke();
powerShell.Commands.Clear();
// Add the command in the script to run.
powerShell.AddCommand("Setup-Workspaces").AddParameter("WorkspaceName", workspace.Name);
// Execute the command from the script.
var results = powerShell.Invoke();
//System.Diagnostics.Debug.WriteLine("Stuff happened");
//System.Diagnostics.Debug.WriteLine("THE SCRIPT\n\n\n");
}
}
以下是从 PowerShell 脚本调用的函数。请注意,编写该函数时只需要提供两个参数之一。我已经验证了在独立于 C# 代码运行此脚本时该方法有效。
function Setup-Workspaces {
# Define the parameter.
param (
$Workspaces, $WorkspaceName
)
Write-Host ("Setup-Workspaces called... ")
# ... rest of function here
}
The Write-Host
输出永远不会出现在调试器输出窗口中...所以我认为这意味着该函数根本没有运行。
我在调试器窗口中得到的输出(省略了大部分内容,它们是一堆“LOADED”语句:
onecore\com\combase\objact\objact.cxx(4080)\combase.dll!00007FFB6A405105: (caller: 00007FFB6A401B12) ReturnHr(4) tid(9874) 80040154 Class not registered
onecore\net\netprofiles\service\src\nsp\dll\namespaceserviceprovider.cpp(550)\nlansp_c.dll!00007FFB242CC759: (caller: 00007FFB6A04388E) LogHr(1) tid(ea84) 8007277C No such service is known. The service cannot be found in the specified name space.
onecore\printscan\appxpackaging\signing\src\appxsip.cpp(612)\AppxSip.dll!00007FFA8490183C: (caller: 00007FFB69582C5A) ReturnHr(1) tid(ea84) 80070032 The request is not supported.
onecore\printscan\appxpackaging\signing\src\appxbundlesip.cpp(693)\AppxSip.dll!00007FFA84901327: (caller: 00007FFB69582C5A) ReturnHr(2) tid(ea84) 80070032 The request is not supported.