我正在尝试制作一个表单,允许我在文本框中输入站点名称,并获得该特定站点的所有OU。如果我直接输入站点名称而不使用变量,我可以让powershell命令工作,但我有80多个站点,不希望为那么多站点进行配置。
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);'
[Console.Window]::ShowWindow([Console.Window]::GetConsoleWindow(), 0)
<#
.NAME
Template
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400,400)
$Form.text = "Form"
$Form.TopMost = $false
$Site_Location = $TextBox_Site.text
$Site_Name = New-Object system.Windows.Forms.Label
$Site_Name.text = "Enter Site Name"
$Site_Name.AutoSize = $true
$Site_Name.width = 25
$Site_Name.height = 10
$Site_Name.location = New-Object System.Drawing.Point(18,10)
$Site_Name.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$TextBox_Site = New-Object system.Windows.Forms.TextBox
$TextBox_Site.multiline = $false
$TextBox_Site.Text = ""
$TextBox_Site.width = 100
$TextBox_Site.height = 30
$TextBox_Site.location = New-Object System.Drawing.Point(18,46)
$TextBox_Site.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button_Get = New-Object system.Windows.Forms.Button
$Button_Get.text = "Get Site OUs"
$Button_Get.width = 100
$Button_Get.height = 30
$Button_Get.location = New-Object System.Drawing.Point(18,138)
$Button_Get.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
#Controls
$Form.controls.AddRange(@($Site_Name,$Button_Get,$TextBox_Site))
#End Controls
#Actions
$Button_Get.Add_Click({ GetSiteOUs })
#End Actions
#Functions
$Site_Location = $TextBox_Site.text
function GetSiteOUs {Get-ADOrganizationalUnit -Filter 'Name -Like "$Site_Location"' | Select-Object -Property Name, distinguishedname | Out-GridView -PassThru -Title "Select the OU" }
#End Functions
[void]$Form.ShowDialog()
1条答案
按热度按时间dldeef671#
我相信您的
.Click
event可以简化成这样,这样应该可以正常工作:您的代码的问题是,首先筛选器不正确,它不允许变量扩展:
应道:
第二个问题是
$Site_Location
是在事件外部而不是内部赋值的,这种赋值只在加载表单时发生一次,而不是每次单击按钮时**发生。