使用PowerShell以编程方式构建复选框列表

时间:2014-11-25 06:22:02

标签: powershell

我正在尝试从$domain = Get-MsolDomain中的值返回填充复选框,返回可用的域,然后根据值返回生成复选框,并从@mail中排除值。谢谢

这是我到目前为止的代码:

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} 
if ($snapin -eq $null) 
{
    Write-Host -foregroundcolor Green "Loading SharePoint PowerShell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
Import-Module MSOnline
$credentials = Get-Credential
Connect-MsolService -Credential $credentials

$unlicensedUsersBatch500 = Get-MsolUser -UnlicensedUsersOnly -MaxResults 500



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

$Form = New-Object System.Windows.Forms.Form    
$Form.Size = New-Object System.Drawing.Size(600,700) 
$Form.text ="Office 365 Licence Activation" 

############################################## Start group boxes

$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(240,20) 
$groupBox.size = New-Object System.Drawing.Size(200,100) 
$groupBox.text = "Availabe Office 365 Domains:" 
$Form.Controls.Add($groupBox)


$Checkboxes += New-Object System.Windows.Forms.CheckBox
$Checkboxes.Location = New-Object System.Drawing.Size(10,20) 


$domain = Get-MsolDomain



foreach ($a in $domain)
{    

    for ($i=1;$i -lt 6; $i++) 
    {
    $Checkboxes.Text = $a.Name
    }
}
$groupBox.Controls.Add($Checkboxes) 

1 个答案:

答案 0 :(得分:1)

我会这样做。请注意,我为了测试而创建并填充了$ domain,因此您需要将其替换为对Get-MsolDomain的调用。 小编,组框的大小将根据$ domain中的元素数量自动增长。

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

$Form = New-Object System.Windows.Forms.Form    
$Form.Size = New-Object System.Drawing.Size(600,700) 
$Form.text ="Office 365 Licence Activation" 

############################################## Start group boxes

$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(240,20)  
$groupBox.text = "Availabe Office 365 Domains:" 
$Form.Controls.Add($groupBox)


$Checkboxes += New-Object System.Windows.Forms.CheckBox
$Checkboxes.Location = New-Object System.Drawing.Size(10,20) 

#$domain = Get-MsolDomain
$domain = @()
$domain += @{"Name"="domain1"}
$domain += @{"Name"="domain2"}
$domain += @{"Name"="domain3"}

$Checkboxes = @()
$y = 20
foreach ($a in $domain)
{    
    $Checkbox = New-Object System.Windows.Forms.CheckBox
    $Checkbox.Text = $a.Name
    $Checkbox.Location = New-Object System.Drawing.Size(10,$y) 
    $y += 30
    $groupBox.Controls.Add($Checkbox) 
    $Checkboxes += $Checkbox
}
$groupBox.size = New-Object System.Drawing.Size(200,(40*$checkboxes.Count)) 
$form.ShowDialog()| Out-Null