如何使用C#获取AppV虚拟进程列表

时间:2017-03-28 15:42:53

标签: c# visual-studio powershell appv

我正在尝试使用C#获取Microsoft AppV启动的所有虚拟进程的列表。

我尝试在C#中使用Powershell,但是我收到了这个错误:

  

System.Management.Automation.CommandNotFoundException:' Get-AppvVirtualProcess'命令在模块' AppvClient'中找到,但无法加载模块。有关详细信息,请运行“导入模块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());
        }

}

而不是Runspace,我也尝试了这个,但是我得到了同样的错误:

public static void p()
{
    using (var powershell = PowerShell.Create())
    {
        powershell.AddCommand("Get-AppvVirtualProcess");

        powershell.Invoke();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试遍历所有正在运行的进程,并找到加载AppVEntSubsystems32.dll或AppVEntSubsystems64.dll的进程。

您可以在此处详细了解:https://blogs.msdn.microsoft.com/gladiator/2014/09/04/app-v-5-on-application-launch/