Angel

Angel

  • NA
  • 6
  • 721

How to display PowerShell Results in Textboxes

Aug 19 2022 4:58 PM

Hello all. I am brand new to C# and am trying to run the following code into a button's click event.

using (PowerShell powerShell = PowerShell.Create())
            {
                powerShell.AddScript("Get-ComputerInfo | Select-Object CsDNSHostName,WindowsProductName, OSVersion, CSDomainRole, CSProcessors, OsProductType");
                powerShell.AddCommand("Out-String");
              
              
                Collection<PSObject> PSOutput = powerShell.Invoke();
                StringBuilder stringBuilder = new StringBuilder();
                foreach (PSObject pSObject in PSOutput)
                stringBuilder.AppendLine(pSObject.ToString());
                Tab2TxtBox2.Text = stringBuilder.ToString(); }

 

I willl not be using Get-Computerinfo in my final script, I'm just using that commandlet to learn what I need to know. I will be using Get-AdComputer in my actual program. In this example I want to display   CsDNSHostName,WindowsProductName, OSVersion, CSDomainRole, CSProcessors, and OsProductType in whichever textbox I want (I have six total boxes). I can display one by using this,

powerShell.AddScript("Get-ComputerInfo | Select-Object CsDNSHostName -expand CsDNSHostName -unique);

I hope that someone here can help me do this. Thanks to anyone that is able to help. 


Answers (1)