PowerShell objects to textboxes

angelman0401

New member
Joined
Aug 17, 2022
Messages
2
Programming Experience
5-10
Hello all. I am new to C# and am trying to run the following code into a button's click event.

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

What I need is to display each object into the desired textbox. I am able to display a single object into the desired checkbox with this code,

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

I have six textboxes for each of the objects I want to display, but I need help doing so. Thanks for any assistance that anyone can provide.
 
Ummm, CsDNSHostName is not an object. It is a property of an object.

Anyway, you can access the properties of each object in the script results and put them in each textbox. It's still unclear why you are putting stuff into textboxes. Why not use some kind of control that is meant to handle tabular data like a data grid view?

Also, why are you calling PowerShell to get this information instead of using the WMI wrappers that the .NET Framework provides? That's what that PowerShell cmdlet for getting the computer information is doing under the covers anyway: calling the .NET Framework WMI wrapper.
 
Ummm, CsDNSHostName is not an object. It is a property of an object.

Anyway, you can access the properties of each object in the script results and put them in each textbox. It's still unclear why you are putting stuff into textboxes. Why not use some kind of control that is meant to handle tabular data like a data grid view?

Also, why are you calling PowerShell to get this information instead of using the WMI wrappers that the .NET Framework provides? That's what that PowerShell cmdlet for getting the computer information is doing under the covers anyway: calling the .NET Framework WMI wrapper.
The Get-ComputerInfo commandlet is not what I will ultimately use and is just what I'm using for now so that I can implement what I learn for AD centric (such as Get-Adcomputer) commandlets that I will be using. I wrote a PowerShell program for my Helpdesk and decided to add additional functions along with designing a new GUI using XAML. The reason I used textboxes is because in the program that I wrote, Helpdesk personnel can update user information if they find that anything is incorrect while they are verifying the user. It worked really well for us, so I didn't see a reason to change that. Can you provide me with an example on how to access the properties of each object in the script results and put them in each textbox? Thanks for your help.
 

Latest posts

Back
Top Bottom