C# WIndows窗体文本框将文本值传递给Powershell运行脚本

C# WIndows窗体文本框将文本值传递给Powershell运行脚本,c#,winforms,powershell,variables,C#,Winforms,Powershell,Variables,我正在使用C#开发一个Win表单,以创建一个工具,在PowerShell运行空间中运行多个PowerShell脚本,并在文本文件中显示结果 现在我有以下几点 。。。带有按钮的Win表单,该按钮将返回以下PowerShell脚本的结果: txtUserInfo.Text = RunScript(@"Get-ADUser UserNameGoesHere -Properties * | Select-Object Enabled, @{Expression={$_.LockedOut};Label

我正在使用C#开发一个Win表单,以创建一个工具,在PowerShell运行空间中运行多个PowerShell脚本,并在文本文件中显示结果

现在我有以下几点

。。。带有按钮的Win表单,该按钮将返回以下PowerShell脚本的结果:

 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");
脚本正在工作,并在文本框中显示结果

这适用于硬编码到PowerShell脚本中的用户名:

RunScript(@“Get ADUserUsernameGoesher-属性*|

我正试图找到一种方法,使用文本框在Win表单上输入,并将该值作为变量传递给PowerShell运行空间。但我还没有找到任何对我有帮助的帖子

任何指点都很感激


关于,

除非我有误解,请将其添加到字符串中

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");

除非我有误解,否则就把它加到字符串中

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");

在Sorceri所说的基础上,你需要你的按钮来完成所有的工作,并使用从文本框中抓取的数据

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");

$yourbutton.Add_Click({
    #do all your stuff
})

在Sorceri所说的基础上,你需要你的按钮来完成所有的工作,并使用从文本框中抓取的数据

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");

$yourbutton.Add_Click({
    #do all your stuff
})

这很有效。谢谢你的解决方案。很简单。这很有效。谢谢你的解决方案。很简单。是的,我在表单上有一个按钮可以执行代码。是的,我在表单上有一个按钮可以执行代码。尊敬,