Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey experts from following code using powershell i could retrieve the list of all running vm machines on hyper v. but now the issue is without using powershell i need to retrieve list of vm machines on hyper-v. how can this be achieved guys?

        listView1.Items.Clear();
        Process proc = new Process(); ProcessStartInfo procStartInfo;
        string command = "/c" + "Powershell Get-VM | findstr  /R Running ";
        procStartInfo = new ProcessStartInfo("CMD", command);
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = true;
        //Process proc = new Process();
        proc.StartInfo = procStartInfo;

        proc.StartInfo = procStartInfo;
        proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived1);
        proc.Start();
        proc.BeginOutputReadLine();
        procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        proc.WaitForExit();
        if (k == 0)
        {
            listView1.Items.Add("No Virtual Machine is running");

        }

    }
    catch (Exception e) { }
}

void proc_OutputDataReceived1(object sender, DataReceivedEventArgs e)
{
    try
    {
        if (e.Data != null)
        {
            k++;
            string str = e.Data.Trim() + Environment.NewLine;
            if (str.Contains("Running"))
            {
                int index = str.IndexOf("Running");
                string result = str.Substring(0, index);
                if (virtualmachnames.Contains(result))
                { }
                else
                {
                    virtualmachnames.Add(result);
                }

                ListViewItem newList = new ListViewItem(result);
                newList.SubItems.Add("Running");
                MethodInvoker append = () => listView1.Items.Add(newList);
                FileIO.LogNotify("MAIN", "checkvirtual", FileIO.NotifyType.INFO, "Virtual machine runnning are:", result);
                listView1.BeginInvoke(append);
            }

        }
    }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900