2

I am trying to get a list of all Virtual Processes started by Microsoft AppV using C#.

I tried using Powershell in C# but I get this error:

System.Management.Automation.CommandNotFoundException: 'The 'Get-AppvVirtualProcess' command was found in the module 'AppvClient', but the module could not be loaded. For more information, run 'Import-Module AppvClient'.'

The weird thing is that if I use the Powershell command line, it works just fine and lists the virtual processes.

So in C# I did a:

ps.Commands.AddCommand("Get-Command");

and it shows Get-AppvVirtualProcess listed as a command:

The result:

Function Get-AppvVirtualProcess 1.0.0.0 A

I tried loading the module in C# manually using:

InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] {@"C:\Program Files\Microsoft Application Virtualization\Client\AppvClient\AppvClient.psd1" });

and

ps.Commands.AddCommand("Import-Module").AddArgument("AppvClient");

But it still gives me the same error mentioned above.

The code in C# looks like this:

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());
        }

}

Instead of Runspace, I tried this as well but I get the same error:

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

        powershell.Invoke();
    }
}
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
Amir
  • 61
  • 4

1 Answers1

0

You could try to iterate through all the running process, and find those that loaded either AppVEntSubsystems32.dll or AppVEntSubsystems64.dll.

You can read more about this here: https://blogs.msdn.microsoft.com/gladiator/2014/09/04/app-v-5-on-application-launch/

Bogdan Mitrache
  • 10,536
  • 19
  • 34
  • Searching in Process Explorer for the DLL is manual and not programattic. This is also not the best way to go about getting accurate results. Seems more like patch work. But thanks for the tip! – Amir Mar 29 '17 at 14:13