2013-03-21 58 views
0

我想將我創建的現有powershell腳本包裝到GUI中,但沒有在它上面發送日子試圖找出它我想我在這裏發佈。我已經創建了一個來自Primal形式的劇本應該看起來像什麼,但需要援助來轉換它。Powershell - 幫助在GUI中包裝腳本

我運行的腳本將計算機帳戶添加到給定3個選項的安全組。對本地主機,計算機列表或手動輸入主機名運行腳本。此外,我想選擇一個組並對選擇進行操作,而不是在可能的情況下手動輸入組名。

我的主要腳本

Import-Module ActiveDirectory 
    cls 
    $OU = "OU=TESTScripts,OU=Groups,OU=Company,DC=Company,DC=local" 
    $AppGroupList = Get-ADGroup -Filter {GroupCategory -eq "security"} -SearchBase "$OU" 
    $AppGroupList | Select-Object @{Name="AD Security Group";Expression={$_.Name}} 

    $title = "Add manually entered computer, localhost or list of computers to SCCM  Application Install Group" 
    $message = "Please select an option from below:-" 

    $1 = New-Object System.Management.Automation.Host.ChoiceDescription "Enter &Hostname" 

    $2 = New-Object System.Management.Automation.Host.ChoiceDescription "Add &List of Computers" 

    $3 = New-Object System.Management.Automation.Host.ChoiceDescription "&Add localhost" 

    $options = [System.Management.Automation.Host.ChoiceDescription[]]($1, $2, $3) 
    $result = $host.ui.PromptForChoice($title, $message, $options, 0) 


switch ($result) 
    { 
     0 {$Computers = Read-Host "Enter Hostname>"}       

     1 {$Computers = get-content "c:\scripts\Computers.csv"}     

     2 {$Computers = $env:computername} 

    } 

    $AppGroup = Read-Host "Application Security Group Name>" 
    $Group = Get-ADGroup -Identity $AppGroup  

foreach ($Hostname in $Computers){ 

     $member = Get-ADComputer -Identity "$Hostname" -Properties MemberOf | Select-Object MemberOf 
     $pc = Get-ADComputer "$Hostname" 

      if ($member.Memberof -like "$Group"){ 
       Write-Host "" 
       Write-Host $Hostname is already a member of $AppGroup -ForegroundColor "Yellow"  
      } 

      else { 
       Add-ADGroupMember $AppGroup $pc 
       Write-Host "" 
       Write-Host $Hostname has been added to $AppGroup -ForegroundColor "Green" 


      } 
} 

`

我的表單代碼如下所示: -

#Generated Form Function 
    function GenerateForm { 
    ######################################################################## 
    # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0 
    # Generated On: 22/03/2013 11:17 AM 
    ######################################################################## 

    #region Import the Assemblies 
    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null 
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null 
    #endregion 

    #region Generated Form Objects 
    $form1 = New-Object System.Windows.Forms.Form 
    $label3 = New-Object System.Windows.Forms.Label 
    $Hostname = New-Object System.Windows.Forms.Label 
    $Hostname = New-Object System.Windows.Forms.TextBox 
    $Install = New-Object System.Windows.Forms.Button 
    $label1 = New-Object System.Windows.Forms.Label 
    $Browse = New-Object System.Windows.Forms.Button 
    $checkBox1 = New-Object System.Windows.Forms.CheckBox 
    $ListApps = New-Object System.Windows.Forms.ComboBox 
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState 
    #endregion Generated Form Objects 

    #---------------------------------------------- 
    #Generated Event Script Blocks 
    #---------------------------------------------- 
    #Provide Custom Code for events specified in PrimalForms. 
    $handler_textBox1_TextChanged= 
    { 
    #TODO: Place custom script here 

    } 

    $Browse_OnClick= 
    { 
    #TODO: Place custom script here 

    } 

    $handler_label2_Click= 
    { 
    #TODO: Place custom script here 

    } 

    $handler_form1_Load= 
    { 
    #TODO: Place custom script here 

    Import-Module ActiveDirectory 

    $OU = "OU=TESTScripts,OU=Groups,OU=Company,DC=Company,DC=local" 
    $AppGroupList = Get-ADGroup -Filter {GroupCategory -eq "security"} -SearchBase "$OU" 
    $AppGroupList | Select-Object @{Name="AD Security Group";Expression={$_.Name}} 


    } 

    $Install_OnClick= 
    { 
    #TODO: Place custom script here 

    } 

    $handler_checkBox1_CheckedChanged= 
    { 
    #TODO: Place custom script here 

    } 

    $OnLoadForm_StateCorrection= 
    {#Correct the initial state of the form to prevent the .Net maximized form issue 
$form1.WindowState = $InitialFormWindowState 
    } 

    #---------------------------------------------- 
    #region Generated Form Code 
    $System_Drawing_Size = New-Object System.Drawing.Size 
    $System_Drawing_Size.Height = 423 
    $System_Drawing_Size.Width = 284 
    $form1.ClientSize = $System_Drawing_Size 
    $form1.DataBindings.DefaultDataSourceUpdateMode = 0 
    $form1.Name = "form1" 
    $form1.Text = "Install Software" 
    $form1.add_Load($handler_form1_Load) 

    $label3.DataBindings.DefaultDataSourceUpdateMode = 0 

    $System_Drawing_Point = New-Object System.Drawing.Point 
    $System_Drawing_Point.X = 12 
    $System_Drawing_Point.Y = 76 
    $label3.Location = $System_Drawing_Point 
    $label3.Name = "label3" 
    $System_Drawing_Size = New-Object System.Drawing.Size 
    $System_Drawing_Size.Height = 20 
    $System_Drawing_Size.Width = 185 
    $label3.Size = $System_Drawing_Size 
    $label3.TabIndex = 7 
    $label3.Text = "Application Install Groups:-" 

    $form1.Controls.Add($label3) 

    $Hostname.DataBindings.DefaultDataSourceUpdateMode = 0 

    $System_Drawing_Point = New-Object System.Drawing.Point 
    $System_Drawing_Point.X = 206 
    $System_Drawing_Point.Y = 146 
    $Hostname.Location = $System_Drawing_Point 
    $Hostname.Name = "Hostname" 
    $System_Drawing_Size = New-Object System.Drawing.Size 
    $System_Drawing_Size.Height = 29 
    $System_Drawing_Size.Width = 66 
    $Hostname.Size = $System_Drawing_Size 
    $Hostname.TabIndex = 6 
    $Hostname.Text = "Enter Hostname" 
    $Hostname.add_Click($handler_label2_Click) 

    $form1.Controls.Add($Hostname) 

    $Hostname.DataBindings.DefaultDataSourceUpdateMode = 0 
    $System_Drawing_Point = New-Object System.Drawing.Point 
    $System_Drawing_Point.X = 91 
    $System_Drawing_Point.Y = 155 
    $Hostname.Location = $System_Drawing_Point 
    $Hostname.Name = "Hostname" 
    $System_Drawing_Size = New-Object System.Drawing.Size 
    $System_Drawing_Size.Height = 20 
    $System_Drawing_Size.Width = 102 
    $Hostname.Size = $System_Drawing_Size 
    $Hostname.TabIndex = 5 
    $Hostname.add_TextChanged($handler_textBox1_TextChanged) 

    $form1.Controls.Add($Hostname) 


    $Install.DataBindings.DefaultDataSourceUpdateMode = 0 

    $System_Drawing_Point = New-Object System.Drawing.Point 
    $System_Drawing_Point.X = 10 
    $System_Drawing_Point.Y = 246 
    $Install.Location = $System_Drawing_Point 
    $Install.Name = "Install" 
    $System_Drawing_Size = New-Object System.Drawing.Size 
    $System_Drawing_Size.Height = 38 
    $System_Drawing_Size.Width = 259 
    $Install.Size = $System_Drawing_Size 
    $Install.TabIndex = 4 
    $Install.Text = "Install" 
    $Install.UseVisualStyleBackColor = $True 
    $Install.add_Click($Install_OnClick) 

    $form1.Controls.Add($Install) 

    $label1.DataBindings.DefaultDataSourceUpdateMode = 0 

    $System_Drawing_Point = New-Object System.Drawing.Point 
    $System_Drawing_Point.X = 91 
    $System_Drawing_Point.Y = 206 
    $label1.Location = $System_Drawing_Point 
    $label1.Name = "label1" 
    $System_Drawing_Size = New-Object System.Drawing.Size 
    $System_Drawing_Size.Height = 18 
    $System_Drawing_Size.Width = 164 
    $label1.Size = $System_Drawing_Size 
    $label1.TabIndex = 3 
    $label1.Text = "Browse to add list of computers" 

    $form1.Controls.Add($label1) 


    $Browse.DataBindings.DefaultDataSourceUpdateMode = 0 

    $System_Drawing_Point = New-Object System.Drawing.Point 
    $System_Drawing_Point.X = 10 
    $System_Drawing_Point.Y = 201 
    $Browse.Location = $System_Drawing_Point 
    $Browse.Name = "Browse" 
    $System_Drawing_Size = New-Object System.Drawing.Size 
    $System_Drawing_Size.Height = 23 
    $System_Drawing_Size.Width = 75 
    $Browse.Size = $System_Drawing_Size 
    $Browse.TabIndex = 2 
    $Browse.Text = "Browse" 
    $Browse.UseVisualStyleBackColor = $True 
    $Browse.add_Click($Browse_OnClick) 

    $form1.Controls.Add($Browse) 


    $checkBox1.DataBindings.DefaultDataSourceUpdateMode = 0 

    $System_Drawing_Point = New-Object System.Drawing.Point 
    $System_Drawing_Point.X = 12 
    $System_Drawing_Point.Y = 135 
    $checkBox1.Location = $System_Drawing_Point 
    $checkBox1.Name = "checkBox1" 
    $System_Drawing_Size = New-Object System.Drawing.Size 
    $System_Drawing_Size.Height = 60 
    $System_Drawing_Size.Width = 75 
    $checkBox1.Size = $System_Drawing_Size 
    $checkBox1.TabIndex = 1 
    $checkBox1.Text = "Tick LocalHost" 
    $checkBox1.UseVisualStyleBackColor = $True 
    $checkBox1.add_CheckedChanged($handler_checkBox1_CheckedChanged) 

    $form1.Controls.Add($checkBox1) 

    $ListApps.DataBindings.DefaultDataSourceUpdateMode = 0 
    $ListApps.FormattingEnabled = $True 
    $System_Drawing_Point = New-Object System.Drawing.Point 
    $System_Drawing_Point.X = 12 
    $System_Drawing_Point.Y = 108 
    $ListApps.Location = $System_Drawing_Point 
    $ListApps.Name = "ListApps" 
    $System_Drawing_Size = New-Object System.Drawing.Size 
    $System_Drawing_Size.Height = 21 
    $System_Drawing_Size.Width = 260 
    $ListApps.Size = $System_Drawing_Size 
    $ListApps.TabIndex = 0 

    $form1.Controls.Add($ListApps) 

    #endregion Generated Form Code 

    #Save the initial state of the form 
    $InitialFormWindowState = $form1.WindowState 
    #Init the OnLoad event to correct the initial state of the form 
    $form1.add_Load($OnLoadForm_StateCorrection) 
    #Show the Form 
    $form1.ShowDialog()| Out-Null 

    } #End Function 

    #Call the Function 
    GenerateForm 

回答

0

這是將上述腳本轉換爲GUI的完整代碼。

#Generated Form Function 
function GenerateForm { 
######################################################################## 
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0 
# Generated On: 25/03/2013 4:56 PM 
# Generated By: MrMeaner 
######################################################################## 

Import-Module ActiveDirectory 

#region Import the Assemblies 
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null 
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null 
#endregion 

#region Generated Form Objects 
$form1 = New-Object System.Windows.Forms.Form 
$radioButton3 = New-Object System.Windows.Forms.RadioButton 
$radioButton2 = New-Object System.Windows.Forms.RadioButton 
$radioButton1 = New-Object System.Windows.Forms.RadioButton 
$label3 = New-Object System.Windows.Forms.Label 
$textHostname = New-Object System.Windows.Forms.Label 
$txtHostname = New-Object System.Windows.Forms.TextBox 
$Install = New-Object System.Windows.Forms.Button 
$label1 = New-Object System.Windows.Forms.Label 
$Browse = New-Object System.Windows.Forms.Button 
$ListApps = New-Object System.Windows.Forms.ComboBox 
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState 
#endregion Generated Form Objects 

#---------------------------------------------- 
#Generated Event Script Blocks 
#---------------------------------------------- 
#Provide Custom Code for events. 

$Browse_OnClick= 
{ 
#When Browse button is clicked 
$FileName = Select-FileDialog 
$Computers = Get-Content $FileName 
$Install.enabled = $true 

} 

$Install_OnClick= 
{ 
#Add computer to security group script 

    if (!$ListApps.SelectedItem) {[System.Windows.Forms.MessageBox]::Show("Please select an SCCM application install group name from the drop down menu") 

     } 
    else { 

     $AppGroup = $ListApps.SelectedItem.ToString() 

     $Group = Get-ADGroup -Identity $AppGroup 

      foreach ($Hostname in $Computers){ 

       $member = Get-ADComputer -Identity "$Hostname" -Properties MemberOf | Select-Object MemberOf 
       $pc = Get-ADComputer "$Hostname" 

       if ($member.Memberof -like "$Group"){ 
        [System.Windows.Forms.MessageBox]::Show("$Hostname is already a member of $AppGroup")  
       } 

       else { 
        Add-ADGroupMember $AppGroup $pc 
        [System.Windows.Forms.MessageBox]::Show("$Hostname has been added to $AppGroup") 

       } 
      } 
    } 

} 

$handler_radioButton1_CheckedChanged= 
{ 
#Localhost radio button 
$txtHostname.enabled = $false 
$Browse.enabled = $false 
$Computers = $env:computername 
$Install.enabled = $true 

} 

$handler_radioButton2_CheckedChanged= 
{ 
#Enter Hostname radiobutton 
$txtHostname.enabled = $true 
$Browse.enabled = $false 
} 

$handler_radioButton3_CheckedChanged= 
{ 
#Load list of computers radiobutton 
$Browse.enabled = $true 
$txtHostname.enabled = $false 

} 

$handler_textBox1_TextChanged= 
{ 
#Hostname textbox 
$Computers = $txtHostname.Text.ToString() 
$Install.enabled = $true 

} 

$handler_form1_Load= 
{ 
#Runs when script is loaded 

$Install.enabled = $false 
$OU = "OU=TESTScripts,OU=Groups,OU=Company,DC=Company,DC=local" 
$AppGroupList = Get-ADGroup -Filter {GroupCategory -eq "security"} -SearchBase "$OU" | Select -expand Name 


#Fill the list with the groupnames 
$AppGroupList | % { $ListApps.Items.Add($_) } 
} 


$OnLoadForm_StateCorrection= 
{#Correct the initial state of the form to prevent the .Net maximized form issue 
    $form1.WindowState = $InitialFormWindowState 
} 

#---------------------------------------------- 
#region Generated Form Code 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 271 
$System_Drawing_Size.Width = 284 
$form1.ClientSize = $System_Drawing_Size 
$form1.DataBindings.DefaultDataSourceUpdateMode = 0 
$form1.Name = "form1" 
$form1.Text = "Install Software" 
$form1.add_Load($handler_form1_Load) 


$radioButton3.DataBindings.DefaultDataSourceUpdateMode = 0 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 188 
$System_Drawing_Point.Y = 58 
$radioButton3.Location = $System_Drawing_Point 
$radioButton3.Name = "radioButton3" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 51 
$System_Drawing_Size.Width = 88 
$radioButton3.Size = $System_Drawing_Size 
$radioButton3.TabIndex = 10 
$radioButton3.TabStop = $True 
$radioButton3.Text = "Load List of Computers" 
$radioButton3.UseVisualStyleBackColor = $True 
$radioButton3.add_CheckedChanged($handler_radioButton3_CheckedChanged) 

$form1.Controls.Add($radioButton3) 


$radioButton2.DataBindings.DefaultDataSourceUpdateMode = 0 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 102 
$System_Drawing_Point.Y = 54 
$radioButton2.Location = $System_Drawing_Point 
$radioButton2.Name = "radioButton2" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 58 
$System_Drawing_Size.Width = 80 
$radioButton2.Size = $System_Drawing_Size 
$radioButton2.TabIndex = 9 
$radioButton2.TabStop = $True 
$radioButton2.Text = "Enter Hostname" 
$radioButton2.UseVisualStyleBackColor = $True 
$radioButton2.add_CheckedChanged($handler_radioButton2_CheckedChanged) 

$form1.Controls.Add($radioButton2) 


$radioButton1.DataBindings.DefaultDataSourceUpdateMode = 0 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 7 
$System_Drawing_Point.Y = 59 
$radioButton1.Location = $System_Drawing_Point 
$radioButton1.Name = "radioButton1" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 48 
$System_Drawing_Size.Width = 76 
$radioButton1.Size = $System_Drawing_Size 
$radioButton1.TabIndex = 8 
$radioButton1.TabStop = $True 
$radioButton1.Text = "LocalHost" 
$radioButton1.UseVisualStyleBackColor = $True 
$radioButton1.add_CheckedChanged($handler_radioButton1_CheckedChanged) 

$form1.Controls.Add($radioButton1) 

$label3.DataBindings.DefaultDataSourceUpdateMode = 0 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 7 
$System_Drawing_Point.Y = 9 
$label3.Location = $System_Drawing_Point 
$label3.Name = "label3" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 20 
$System_Drawing_Size.Width = 185 
$label3.Size = $System_Drawing_Size 
$label3.TabIndex = 7 
$label3.Text = "SCCM Application Install Groups:-" 

$form1.Controls.Add($label3) 

$textHostname.DataBindings.DefaultDataSourceUpdateMode = 0 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 140 
$System_Drawing_Point.Y = 113 
$textHostname.Location = $System_Drawing_Point 
$textHostname.Name = "textHostname" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 27 
$System_Drawing_Size.Width = 105 
$textHostname.Size = $System_Drawing_Size 
$textHostname.TabIndex = 6 
$textHostname.Text = "Enter Hostname" 
$textHostname.add_Click($handler_label2_Click) 

$form1.Controls.Add($textHostname) 

$txtHostname.DataBindings.DefaultDataSourceUpdateMode = 0 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 7 
$System_Drawing_Point.Y = 113 
$txtHostname.Location = $System_Drawing_Point 
$txtHostname.Name = "txtHostname" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 20 
$System_Drawing_Size.Width = 124 
$txtHostname.Size = $System_Drawing_Size 
$txtHostname.TabIndex = 5 
$txtHostname.add_Leave($handler_textBox1_TextChanged) 

$form1.Controls.Add($txtHostname) 


$Install.DataBindings.DefaultDataSourceUpdateMode = 0 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 7 
$System_Drawing_Point.Y = 181 
$Install.Location = $System_Drawing_Point 
$Install.Name = "Install" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 82 
$System_Drawing_Size.Width = 269 
$Install.Size = $System_Drawing_Size 
$Install.TabIndex = 4 
$Install.Text = "Install" 
$Install.UseVisualStyleBackColor = $True 
$Install.add_Click($Install_OnClick) 

$form1.Controls.Add($Install) 

$label1.DataBindings.DefaultDataSourceUpdateMode = 0 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 140 
$System_Drawing_Point.Y = 155 
$label1.Location = $System_Drawing_Point 
$label1.Name = "label1" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 23 
$System_Drawing_Size.Width = 115 
$label1.Size = $System_Drawing_Size 
$label1.TabIndex = 3 
$label1.Text = "Load computer list" 

$form1.Controls.Add($label1) 


$Browse.DataBindings.DefaultDataSourceUpdateMode = 0 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 7 
$System_Drawing_Point.Y = 150 
$Browse.Location = $System_Drawing_Point 
$Browse.Name = "Browse" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 23 
$System_Drawing_Size.Width = 124 
$Browse.Size = $System_Drawing_Size 
$Browse.TabIndex = 2 
$Browse.Text = "Browse" 
$Browse.UseVisualStyleBackColor = $True 
$Browse.add_Click($Browse_OnClick) 

$form1.Controls.Add($Browse) 

$ListApps.DataBindings.DefaultDataSourceUpdateMode = 0 
$ListApps.FormattingEnabled = $True 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 7 
$System_Drawing_Point.Y = 32 
$ListApps.Location = $System_Drawing_Point 
$ListApps.Name = "ListApps" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 21 
$System_Drawing_Size.Width = 269 
$ListApps.Size = $System_Drawing_Size 
$ListApps.TabIndex = 0 

$form1.Controls.Add($ListApps) 

#endregion Generated Form Code 

#Save the initial state of the form 
$InitialFormWindowState = $form1.WindowState 
#Init the OnLoad event to correct the initial state of the form 
$form1.add_Load($OnLoadForm_StateCorrection) 
#Show the Form 
$form1.ShowDialog()| Out-Null 

} #End Function 


#Select File Explorer Function 
function Select-FileDialog { 

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
$ofd = New-Object System.Windows.Forms.OpenFileDialog 
$ofd.InitialDirectory = "d:\scripts" 
$ofd.ShowHelp=$true 
if($ofd.ShowDialog() -eq "OK") { $ofd.FileName } 

} #End Function 


#Call the Function 
GenerateForm 
0

我也不好當它涉及到的Windows Presentation基礎的東西或WinForms。當PowerShell照顧我時,我喜歡它。我希望這是你可以使用的東西:

$OU = Get-ADOrganizationalUnit -Filter * | Out-GridView -PassThru -Title 'Choose OU' 
+0

感謝您的意見。我昨天玩過Out-GridView,但不幸的是它沒有幫助。乾杯。 – MrMeaner 2013-03-22 03:44:21

1

這個問題太寬。請詢問具體問題並嘗試自己。如果你知道基本的powerhsell和/或基本的C#你會更進一步。爲了幫助您填寫您想具體應用組列表,請嘗試:

$handler_form1_Load= 
{ 
#TODO: Place custom script here 
#I would move this import line out of the form and to the top of the script 
Import-Module ActiveDirectory 

$OU = "OU=TESTScripts,OU=Groups,OU=Metro Trains,DC=Metrotrains,DC=local" 
$AppGroupList = Get-ADGroup -Filter {GroupCategory -eq "security"} -SearchBase "$OU" | Select -expand Name 

#Fill the list with the groupnames 
$AppGroupList | % { $ListApps.Items.Add($_) } 

} 

你應該看看禁用某些控件當使用一個。防爆。如果選中本地主機複選框,則禁用文本框和瀏覽按鈕(如果不需要使用全部三項)。將一個click事件處理程序添加到安裝按鈕中,該按鈕從表單獲取當前值並運行「將計算機添加到組」代碼。

+0

感謝您的幫助。我知道這是一個廣泛的問題,我從未爲我的腳本創建過GUI,這是我的第一個。我將如何去從應用程序列表中選擇所選的組值?我在Add-ADGroupMember的腳本中出現錯誤。我想這是因爲它沒有選擇的值傳遞給$ AppGroup變量,因爲它沒有任何地方所定義。 – MrMeaner 2013-03-24 23:45:16

+1

沒關係我加了一行:$ AppGroup = $ ListApps.SelectedItem.ToString()到頂部$ Install_OnClick再次 – MrMeaner 2013-03-25 04:35:30

+1

感謝您的幫助,我改變了佈局了一下,把你的意見和禁用一些使用單選按鈕的控件。我已經完成了劇本,並張貼在這裏的完整代碼他人從benifit: - http://stackoverflow.com/questions/15627293/add-computers-to-security-groups-powershell-gui-complete-code – MrMeaner 2013-03-26 00:36:53