2016-10-03 105 views
1

我试图在C#应用程序内执行此命令(列出Firefox扩展)。在C#上执行PowerShell命令行

Get-ChildItem -Path $env:USERPROFILE"\AppData\Roaming\Mozilla\Firefox\Profiles\*\extensions.json" | Get-Content 

从MSDN文档,我想通了,应该看起来像是代码类似

PowerShell ps = PowerShell.Create(); 
ps.AddCommand("Get-ChildItem"); 
ps.AddParameter("Path", "\"$env:USERPROFILE\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\*\\extensions.json\""); 
ps.AddCommand("Get-Content"); 
Collection<PSObject> results = ps.Invoke(); 

然而,“结果”是null(当PS线不是)。我发现了一些类似的问题,但我真的没有办法用它来回答这个问题。任何人都知道我该如何修复我的代码?

回答

1

您可以尝试使用此代码以获得您想要的结果。而不是使用PowerShell.Create,而是使用RunspaceInvoke.Invoke,仅使用完整命令作为参数。希望它有帮助:

using (RunspaceInvoke invoke = new RunspaceInvoke()) 
{ 
    Collection<PSObject>result = invoke.Invoke("Get-ChildItem -Path $env:USERPROFILE\"\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\*\\extensions.json\" | Get-Content"); 
}