我正在尝试获取 Microsoft AppV 使用 C# 启动的所有虚拟进程的列表。
我尝试在 C# 中使用 Powershell,但出现此错误:
System.Management.Automation.CommandNotFoundException:“在模块“AppvClient”中找到“Get-AppvVirtualProcess”命令,但无法加载该模块。有关更多信息,请运行“Import-Module AppvClient”。
奇怪的是,如果我使用 Powershell 命令行,它就可以正常工作并列出虚拟进程。
所以在 C# 中我做了一个:
ps.Commands.AddCommand("Get-Command");
它显示作为命令列出的 Get-AppvVirtualProcess:
结果:
函数 Get-AppvVirtualProcess 1.0.0.0 A
我尝试使用以下方法手动加载 C# 中的模块:
InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] {@"C:\Program Files\Microsoft Application Virtualization\Client\AppvClient\AppvClient.psd1" });
和
ps.Commands.AddCommand("Import-Module").AddArgument("AppvClient");
但它仍然给我上面提到的同样的错误。
C# 中的代码如下所示:
public static void powershellCommand()
{
Collection<PSObject> result;
using (Runspace myRunSpace = RunspaceFactory.CreateRunspace())
{
InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] {@"C:\Program Files\Microsoft Application Virtualization\Client\AppvClient\AppvClient.psd1" });
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.Commands.AddCommand("Import-Module").AddArgument("AppvClient");
ps.Commands.AddCommand("Get-AppvVirtualProcess");
result = ps.Invoke();
var builder = new StringBuilder();
foreach (PSObject psObject in result)
{
builder.Append(psObject.ToString() + "\n");
builder.ToString();
}
Console.WriteLine("Virtual Process: {0}", builder.ToString());
}
}
而不是运行空间,我也尝试了这个,但我得到了同样的错误:
public static void p()
{
using (var powershell = PowerShell.Create())
{
powershell.AddCommand("Get-AppvVirtualProcess");
powershell.Invoke();
}
}