1

I want to get empty space of every drive of server with powershell using C#

this is the powershell script which works fine

Get-PSDrive |Format-Table

what I want is to take output of this script and show it on UI

What I tried till now.

      string scriptToCheckDBServerMemorySpace = "Get-PSDrive |Format-Table";
      using (PowerShell PowerShellInstance = PowerShell.Create())
      {
                    PowerShellInstance.AddScript(scriptToCheckDBServerMemorySpace);

                    Collection<PSObject> PSObject = PowerShellInstance.Invoke();

                    foreach (PSObject PSOutputItem in PSObject)
                    {
                        if (PSOutputItem != null)
                        {

                            //TxtFirstStepResult.Text = PSOutputItem.BaseObject.ToString() + "\n";
                        }
                    }
                    if (PowerShellInstance.Streams.Error.Count > 0)
                    {
                        TxtFirstStepResult.Text = PowerShellInstance.Streams.Error.ToString() + "\n";
                    }
                    Console.ReadKey();
       }

the question is how to get output of this powershell script and show it on windows form application. I am not able to figure out how to convert this PS object and convert it to readable format.

please redirect me to the right direction.

Ameya Deshpande
  • 3,580
  • 4
  • 30
  • 46

1 Answers1

1

The problem you have is that you are getting back formatted data from your script:

"Get-PSDrive |Format-Table"

The data isn't in a nice table as you'd see in the console - you will need to extract it and display it yourself. A better option is to get the 'raw' objects and format these directly. For example, here's some basic console formatting:

string scriptToCheckDBServerMemorySpace = "Get-PSDrive";
            using (PowerShell PowerShellInstance = PowerShell.Create())
            {
                PowerShellInstance.AddScript(scriptToCheckDBServerMemorySpace);

                Collection<PSObject> PSObject = PowerShellInstance.Invoke();

                foreach (PSObject PSOutputItem in PSObject)
                {
                    if (PSOutputItem != null)
                    {

                        Console.WriteLine($"Drive: {PSOutputItem.Members["Name"].Value}, Provider: {PSOutputItem.Members["Provider"].Value}");
                    }
                }
                if (PowerShellInstance.Streams.Error.Count > 0)
                {
                    //TxtFirstStepResult.Text = PowerShellInstance.Streams.Error.ToString() + "\n";
                }

                Console.ReadKey();
            }
boxdog
  • 7,894
  • 2
  • 18
  • 27
  • I want to show memory space. how to show it. it is only showing provide name – Ameya Deshpande Oct 10 '18 at 10:51
  • @AmeyaDeshpande, you can use the same technique my example uses to access/display any of the properties of the drive. For example: `PSOutputItem.Members["Free"].Value`, `PSOutputItem.Members["Used"].Value`, etc. Use `Get-PSDrive | Get-Member` to see what is available. – boxdog Oct 10 '18 at 10:54
  • Looks like 'Free' and 'Used' are script properties, so are only evaluated at access time, which is after you've left your session. An option that springs to mind (but I don't have time to test), is to use `Add-Member` in your original script to add a couple of corresponding `NoteProperty` members to the objects when they are originally created. – boxdog Oct 10 '18 at 11:10
  • I am new to powershell and frankly I don't have any idea what you are talking about :| – Ameya Deshpande Oct 11 '18 at 09:45