我正在尝试制作一个 GUI PowerShell 脚本来使用用户的主要设备(资源名称)填充列表框。我有一个下拉列表,其中显示了来自 Active Directory 的不同组织单位。在同一个下拉列表中,我想显示用户的“主要设备”,该信息将从 SCCM 收集。由于它是一个 GUI PowerShell 脚本,我想在下拉列表中将主设备显示为数字 1 选项(默认),此外,我还计划调用他们“当前登录”的用户的设备编号”。到目前为止,我已经编写了一个列表框和下拉列表以及一个搜索文本框。用户名是从另一个 PowerShell 脚本调用的,它存储在 $username 变量中。
问题是 ListBox 没有从下拉列表中选择的任何选项中填充设备编号。请帮我解决这个问题。
# Load the System.Web assembly
Add-Type -AssemblyName "System.Web"
Add-Type -AssemblyName System.Windows.Forms
Function Get-UserResourceName ($username) {
$SiteCode = 'FEI'
Set-Location "$($SiteCode):"
$userDeviceaffinity = Get-CMUserDeviceAffinity -UserName $username | Select-Object ResourceName
$userDevice = $userDeviceaffinity | ForEach-Object { $_.PrimaryDevice}
return $userDevice
}
Function Draw_Main_Form
{
#Draw the main window
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Software Deployment"
$Form.Size = New-Object System.Drawing.Size(800, 525)
$Form.StartPosition = "CenterScreen"
#SELECT OU LABEL
$OULabel = New-Object System.Windows.Forms.Label
$OULabel.Location = New-Object System.Drawing.Point(25, 15)
$OULabel.Text = "Select OU:"
$OULabel.Font = New-Object System.Drawing.Font('Verdana Pro',10)
$OULabel.AutoSize = $true
#DROP DOWN LIST
$ComboBoxOU = New-Object System.Windows.Forms.ComboBox
$ComboBoxOU.Location = New-Object System.Drawing.Point(25, 35)
$ComboBoxOU.Size = New-Object System.Drawing.Size(350, 20)
$ComboBoxOU.Font = New-Object System.Drawing.Font('Verdana Pro',10)
$ComboBoxOU.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList
#To Show the last OU name in the dropdown list
foreach ($OU in $OUs) {
$LastOUName = [regex]::Match($OU, '(?<=OU=)[^,]+').Value
$ComboBoxOU.Items.Add($LastOUName)
}
$ComboBoxOU.Items.Add("PrimaryDevice")
$ComboBoxOU.SelectedIndex = 0 #To show the Default OU in the drop down list
#Search Computer Name: Label
$SearchCompLabel = New-Object System.Windows.Forms.Label
$SearchCompLabel.Location = New-Object System.Drawing.Point(25, 70)
$SearchCompLabel.Text = "Search Computer Name: "
$SearchCompLabel.Font = New-Object System.Drawing.Font('Verdana Pro',10)
$SearchCompLabel.AutoSize = $true
#Search Box - Computer
$SearchBoxComp = New-Object System.Windows.Forms.TextBox
$SearchBoxComp.Size = New-Object System.Drawing.Size(350, 20)
$SearchBoxComp.Location = New-Object System.Drawing.Point(25, 90)
$SearchBoxComp.Font = New-Object System.Drawing.Font('Verdana Pro',10)
#List Box Show the list of devices
$ListBoxComp = New-Object System.Windows.Forms.ListBox
$ListBoxComp.Size = New-Object System.Drawing.Size(350, 300)
$ListBoxComp.Location = New-Object System.Drawing.Point(25, 115)
$ListBoxComp.Font = New-Object System.Drawing.Font('Verdana Pro',10)
$ListBoxComp.SelectionMode = 'Multiextended' #Enables multi selection
$script:CompNames = @() #The CompNames Array is being initialised and $script means that it is accessible from variables outside the fucntions and can be called from anywhere in the script.
function UpdateCompNames {
$selectedIndex = $ComboBoxOU.SelectedIndex
if ($ComboBoxOU.SelectedItem.ToString() -eq "PrimanyDevice") {
$script:CompNames = Get-UserResourceName | ForEach-Object { @{ Name = $_ } }
} else {
$script:OUCOMP = $OUs[$selectedIndex]
if ($null -ne $OUCOMP) {
$script:CompNames = Get-ADComputer -LDAPFilter "name=FBC*" -SearchBase $OUCOMP | Sort-Object Name
} else {
$script:CompNames = @()
}
}
UpdateListBox
}
$ComboBoxOU.Add_SelectedIndexChanged({
UpdateCompNames
})
function UpdateListBox {
$SearchTextComp = $SearchBoxComp.Text #Allocating the Searched text to the variable
$WildcardSearchTextComp = "*$SearchTextComp*"
if ($ComboBoxOU.SelectedItem.ToString() -eq "PrimaryDevice") {
$FileredComps = $CompNames | Where-Object { $_ -like $WildcardSearchTextComp}
} else {
$FilteredComps = $CompNames | Where-Object { ($_.Name -like $WildcardSearchTextComp) } #String approximation
}
$ListBoxComp.Items.Clear() #refreshing the list
foreach ($FilteredComp in $FilteredComps) {
if ($ComboBoxOU.SelectedItem.ToString() -eq "PrimaryDevice") {
$ListBoxComp.Items.Add($FilteredComp.Name)
} else {
$ListBoxComp.Items.Add($FilteredComp.Name)
}
}
}
$SearchBoxComp.Add_TextChanged({
UpdateListBox
})
# Update CompNames for the initial population
UpdateCompNames
$Form.Controls.AddRange(@($OUlabel,$SearchCompLabel,$ComboBoxOU,$SearchBoxComp,$ListBoxComp))
#Show the Main Form
$result = $Form.ShowDialog()
}
# Add OUs to the ComboBox
$OUApp = "OU=System Center,OU=Groups,OU=Electric,DC=corp,DC=local"
$OUs = @(
"OU=Fresh PXE Workstations,OU=Workstations,OU=Electric,DC=corp,DC=local",
"OU=laptops,OU=Workstations,OU=Electric,DC=corp,DC=local",
"OU=Desktops,OU=Workstations,OU=Electric,DC=corp,DC=local"
"OU=Stale,OU=Workstations,OU=Electric,DC=corp,DC=local"
)
Draw_Main_Form