Powershell选择组合框并执行mstsc.exe

时间:2019-01-16 18:21:26

标签: powershell

我需要帮助,对不起,因为我是IT新手!

我想创建一个组合框,当我选择服务器时,该组合框具有一个打开mstsc.exe的按钮

我尝试使用以下查询在组合框中填充此列表:

$1= Get-ADComputer -Filter * -SearchBase "OU=Servers, OU=Computer, DC=example, DC=com" | select name

我尝试通过此示例进行一些修改,但我无法:s

[reflection.assembly]::LoadWithPartialName("System.Drawing") | Out-Null
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null

function Button_OnClick() {

  "`$combo.SelectedItem = $($combo.SelectedItem)" | Out-GridView

  if ($combo.SelectedItem -eq 'Google') {
    Start-Process -FilePath 'C:\Program Files\Internet Explorer\iexplore.exe' -ArgumentList 'http://www.google.com'
  } elseif ($combo.SelectedItem -eq 'Microsoft') {
    $IE = New-Object -ComObject 'InternetExplorer.Application'
    $IE.Navigate2('http://www.microsoft.com')
    $IE.Visible = $true
  }

}

$combo = New-Object -TypeName System.Windows.Forms.ComboBox
$combo.Location = New-Object -TypeName System.Drawing.Point -ArgumentList 5, 5
$combo.Size = New-Object -TypeName System.Drawing.Point -ArgumentList 100, 25
$combo.Items.Add('Google') | Out-Null
$combo.Items.Add('Microsoft') | Out-Null
$combo.SelectedIndex = 0

$button = New-Object -TypeName System.Windows.Forms.Button
$button.Location = New-Object -TypeName System.Drawing.Point -ArgumentList 5, 35
$button.Size = New-Object -TypeName System.Drawing.Point -ArgumentList 100, 25
$button.Text = 'Launch in IE'
$button.Add_Click({ Button_OnClick })

$form = New-Object -TypeName System.Windows.Forms.Form

$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.Size = New-Object -TypeName System.Drawing.Point -ArgumentList 60, 105
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen

$form.Controls.Add($combo)
$form.Controls.Add($button)

$form.ShowDialog() | Out-Null

感谢和抱歉我的英语不好

2 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,您想按一个按钮,它将运行查询,然后将服务器名称转储到组合框中。很简单。

您需要遍历名称并将其添加到组合框Item列表中,而不是SelectedItem列表中。

$comboBox1.Items.Clear()
$1 = Get-ADComputer -Filter * -SearchBase "OU=Servers, OU=Computer, DC=example, DC=com" -Properties Name | select name
Foreach($name in $1) {
    $comboBox1.Items.Add($Name.name)
}

不要忘记在运行ComboBox之前先清除它,否则您将得到重复的条目。

编辑:

要使用选定的代码运行mstsc.exe,请将其放入按钮功能中。

mstsc.exe /v:$($comboBox1.SelectedItem)

答案 1 :(得分:0)

谢谢你帮我!我进行了修改,但现在Powerbox仅填充了一台服务器。 此查询可以,因为我在Powershell控制台中进行操作并获得了完整列表:

$1 = Get-ADComputer -Filter * -SearchBase "OU=Servers, OU=xx, DC=xxx, DC=xxx" -Properties Name | select name

我复制了我尝试填写de combobox1的代码。再次感谢您的帮助!

[reflection.assembly]::LoadWithPartialName("System.Drawing") | Out-Null
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$comboBox1.Items.Clear()
$1 = Get-ADComputer -Filter * -SearchBase "OU=Servers, OU=xxx, DC=xxx, DC=xxx" -Properties Name | select name
Foreach($name in $1) {
    $comboBox1.Items.Add($Name.name)
}
$comboBox1 = New-Object -TypeName System.Windows.Forms.ComboBox
$comboBox1.Location = New-Object -TypeName System.Drawing.Point -ArgumentList 5, 5
$comboBox1.Size = New-Object -TypeName System.Drawing.Point -ArgumentList 100, 25
$comboBox1.Items.Add($name.name) | Out-Null
$form = New-Object -TypeName System.Windows.Forms.Form
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.Size = New-Object -TypeName System.Drawing.Point -ArgumentList 60, 105
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$form.Controls.Add($combobox1)
$form.Controls.Add($button)
$form.ShowDialog() | Out-Null