【问题标题】:Powershell select combobox and execute a mstsc.exePowershell 选择组合框并执行 mstsc.exe
【发布时间】:2019-06-10 21:30:54
【问题描述】:

我需要帮助,很抱歉,因为我是 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

感谢和抱歉我的英语不好

【问题讨论】:

    标签: powershell


    【解决方案1】:

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

    您需要遍历名称并将它们添加到组合框 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)
    

    【讨论】:

    • 我认为他想像您的代码一样用 AD 用户填充组合框,然后为所选用户打开 mstsc.exe。
    • 是的,我想要这个。用 AD Computer 填充组合框,并有一个按钮可以从此列表中打开 mstsc.exe。抱歉,我是 PS 新手
    • 很好,我刚刚添加了您需要放入按钮功能的代码。
    【解决方案2】:

    感谢德鲁帮助我!我进行了修改,但现在 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
    

    【讨论】:

      猜你喜欢
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多