简体   繁体   English

PowerShell命令行开关被调用但没有响应

[英]PowerShell commandlet getting invoked but not responding

Invoke-MyFunction is a commandlet I wrote that takes an input file, changes it, and creates a new output file at a specified location. Invoke-MyFunction是我编写的一个命令行,它接受输入文件,更改它,并在指定位置创建一个新的输出文件。 If I open a PowerShell on my desktop, import MyCommandlet.ps1, and run 如果我在桌面上打开PowerShell,请导入MyCommandlet.ps1,然后运行

Invoke-MyFunction -InputPath path\to\input -OutputPath path\to\output

everything works as expected. 一切都按预期工作。 But when I try to import and invoke the command from a C# program with the code below, the commandlet doesn't run, doesn't log output, and doesn't produce the output file. 但是当我尝试使用下面的代码从C#程序导入和调用命令时,命令行开关不运行,不记录输出,也不产生输出文件。 It doesn't throw a CommandNotFoundException, so I assume the PowerShell object recognizes my commandlet. 它不会抛出CommandNotFoundException,因此我假设PowerShell对象识别我的命令行开关。 But I can't figure out why it doesn't execute it. 但我无法弄清楚它为什么不执行它。

    //set up the PowerShell object
    InitialSessionState initial = InitialSessionState.CreateDefault();
    initial.ImportPSModule(new string[] { @"C:\path\to\MyCommandlet.ps1" });
    Runspace runspace = RunspaceFactory.CreateRunspace(initial);
    runspace.Open();
    PowerShell ps = PowerShell.Create();
    ps.Runspace = runspace;

    //have MyFunction take input and create output
    ps.AddCommand("Invoke-MyFunction");
    ps.AddParameter("OutputPath", @"C:\path\to\output");
    ps.AddParameter("InputPath", @"C:\path\to\input");
    Collection<PSObject> output = ps.Invoke();

Further, after invoking MyFunction, the PowerShell object ps fails to execute any other commands. 此外,在调用MyFunction之后,PowerShell对象ps无法执行任何其他命令。 Even known ones. 甚至是已知的。

This works for me: 这对我有用:

//set up the PowerShell object
var initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] { @"C:\Users\Keith\MyModule.ps1" });
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;

//have MyFunction take input and create output
ps.AddCommand("Invoke-MyFunction");
ps.AddParameter("OutputPath", @"C:\path\to\output");
ps.AddParameter("InputPath", @"C:\path\to\input");
var output = ps.Invoke();
foreach (var item in output)
{
    Console.WriteLine(item);
}

With a MyModule.ps1 of: 使用MyModule.ps1:

function Invoke-MyFunction($InputPath, $OutputPath) {
   "InputPath is '$InputPath', OutputPath is '$OutputPath'"
}

One thing that did cause me a failure is that on Visual Studio 2013 (maybe 2012 as well) AnyCPU apps will actually run 32-bit on a 64-bit OS. 确实让我失败的一件事是在Visual Studio 2013上(也许是2012年)AnyCPU应用程序实际上将在64位操作系统上运行32位。 You have to have set the execution policy for PowerShell x86 to allow script execution. 您必须为PowerShell x86设置执行策略以允许脚本执行。 Try opening up a PowerShell x86 shell in admin mode and run Get-ExecutionPolicy . 尝试在管理模式下打开PowerShell x86 shell并运行Get-ExecutionPolicy If it is set to Restricted , use Set-ExecutionPolicy RemoteSigned to allows scripts to execute. 如果将其设置为Restricted ,请使用Set-ExecutionPolicy RemoteSigned以允许脚本执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM