2016-09-26 29 views
0

我创建了一个表单并动态添加了CheckBoxes和CheckBox名称。我怎样才能以编程方式从Get-LicenseDetails函数中检查一个特定的CheckBox?缺少支架已被添加。如何从函数访问复选框

import-module MSOnline 

Function Get-LicenseDetails { 
    Param ($upn) 
    $licenses = Get-MsolUser -UserPrincipalName $upn 
    ForEach ($license in $licenses.Licenses) { 
     If ($license.AccountSkuId -like '*ENTERPRISEPACK') { 
      $serviceName = $serviceStatus.ServicePlan.ServiceName 
      $checkBox.Checked = $true 
     } 
    } 
} 

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$System_Drawing_Point = New-Object System.Drawing.Point 
Add-Type -AssemblyName System.Windows.Forms 
$form = New-Object Windows.Forms.Form 
$form.Text = "Office 365 Licensing" 
$form.Name = "Form1" 
$form.Size = New-Object Drawing.Size @(316, 510) 

#SEARCH BUTTON 
$searchBtn = New-Object System.Windows.Forms.Button 
$System_Drawing_Point.X = 226 
$System_Drawing_Point.Y = 38 
$searchBtn.Location = $System_Drawing_Point 
$searchBtn.add_click({Get-LicenseDetails "[email protected]"}) 
$searchBtn.Size = New-Object System.Drawing.Size(67, 23) 
$searchBtn.Text = "Click Me" 
$form.Controls.Add($searchBtn) 

#CHECKBOXES 
$y = 80 
$Services = (Get-MsolAccountSku | Where-Object {$_.SkuPartNumber -eq "ENTERPRISEPACK"}).ServiceStatus.ServicePlan.ServiceName 
ForEach ($service in $Services) { 
    $checkbox = New-Object System.Windows.Forms.CheckBox 
    $checkbox.Text = $service 
    $checkbox.Name = "CheckBox_$service" 
    $checkbox.Size = New-Object System.Drawing.Size(260,17) 
    $checkbox.Location = New-Object System.Drawing.Size(10,$y) 
    $y += 25 
    $form.Controls.Add($checkbox) 
} 

$drc = $form.ShowDialog() 

回答

0

首先,您缺少一个装载System.Drawing组件的方形括号。

您可以访问Get-LicenseDetails中的复选框,或者将它们传递给函数,或者通过使用$global:来访问它们。但是,我不会将GUI元素传递给该函数。相反,我会创建一个模型(新对象),并将其传递给Get-LicenseDetails方法。

0

好吧,我已经想通了。我在功能中使用了这些行:

$checkBoxName = "CheckBox_" + $serviceName 
$checkbox = $form.Controls | Where-Object {$_.name -eq $checkBoxName} 
$checkbox.Checked = $true