lmalave lmalave - 1 month ago 158
C# Question

WIndows Form textbox pass text value to a Powershell Runscript

I am developing a Win form with C# to create a tool that would run several PowerShell scripts within a PowerShell Run space and show the results on a text file.

Right now I have the following.

... a Win-form with a button that will return the results of the PowerShell script below:

txtUserInfo.Text = RunScript(@"Get-ADUser UserNameGoesHere -Properties * | Select-Object Enabled, @{Expression={$_.LockedOut};Label='Locked';}, DisplayName, GivenName, SurName, Mail, @{ Expression ={[DateTime]::FromFileTime($_.LastLogon)}; Label='Last Logon';}, @{Expression={$_.Created};Label='Date Created';}, passwordlastset, Passwordneverexpires | Format-list");


The script is working and is showing the results on a textbox.

enter image description here

This works with the Users name hard coded into the PowerShell Script:


RunScript(@"Get-ADUser UserNameGoesHere -Properties * |


I am trying to find a way to use a Textbox for input on the Win form and pass that value to the PowerShell Run space as a variable. But I have not been able to find anything posted that will assist me.

Any pointers are appreciated

Regards,

Answer Source

unless I am misunderstanding, just add it into the string

 txtUserInfo.Text = RunScript(@"Get-ADUser " + YourTextBox.Text + @" -Properties * | Select-Object Enabled, @{Expression={$_.LockedOut};Label='Locked';}, DisplayName, GivenName, SurName, Mail, @{ Expression ={[DateTime]::FromFileTime($_.LastLogon)}; Label='Last Logon';}, @{Expression={$_.Created};Label='Date Created';}, passwordlastset, Passwordneverexpires | Format-list");