如何在变量中使用变量?

如何在变量中使用变量?

此表单的目的是查找分配给离职员工的设备名称。目前,我们对用户/设备分配的唯一引用是在 AD 描述字段中。我无法将名称输入到字段中$Tbx_Leaver以传递给变量 $Leaver。如果我在 leaver 变量中输入名称而不是 $Field 名称,它就可以正常工作。我哪里做错了?

表单中的额外文本框将用于显示设备名称,我将添加一个按钮来在 AD 中禁用该设备。

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);'

[void][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

$Lbl_Leaver                      = New-Object system.Windows.Forms.Label
$Lbl_Leaver.Text                 = "Leaver Name"
$Lbl_Leaver.AutoSize             = $true
$Lbl_Leaver.width                = 25
$Lbl_Leaver.height               = 10
$Lbl_Leaver.location             = New-Object System.Drawing.Point(11,8)
$Lbl_Leaver.Font                 = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Tbx_Leaver                      = New-Object system.Windows.Forms.TextBox
$Tbx_Leaver.Text                 = ""
$Tbx_Leaver.multiline            = $false
$Tbx_Leaver.width                = 200
$Tbx_Leaver.height               = 20
$Tbx_Leaver.location             = New-Object System.Drawing.Point(15,43)
$Tbx_Leaver.Font                 = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Btn_Submit                      = New-Object system.Windows.Forms.Button
$Btn_Submit.Text                 = "Submit"
$Btn_Submit.width                = 60
$Btn_Submit.height               = 30
$Btn_Submit.location             = New-Object System.Drawing.Point(28,88)
$Btn_Submit.Font                 = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Btn_Submit.Add_Click({ Find_Leaver_Devices })

$Tbx_Device_Name                     = New-Object system.Windows.Forms.TextBox
$Tbx_Device_Name.Text                = ""
$Tbx_Device_Name.multiline           = $false
$Tbx_Device_Name.width               = 100
$Tbx_Device_Name.height              = 20
$Tbx_Device_Name.location            = New-Object System.Drawing.Point(13,192)
$Tbx_Device_Name.Font                = New-Object System.Drawing.Font('Microsoft Sans Serif',10)


$Form.controls.AddRange(@($Lbl_Leaver,$Tbx_Leaver,$Btn_Submit,$$Tbx_Device_Name))



Function Find_Leaver_Devices {
$Leaver = "*$Tbx_Leaver.Text*"
(Get-ADComputer -SearchBase "OU=abc,OU=def,OU=ghi,DC=123,DC=456" -Filter {Description -Like $Leaver} -Properties *) | Select-Object Name, Description | Out-GridView -Passthru
}


[void]$Form.ShowDialog()

答案1

我找到了答案。我只需要删除 $Leaver 变量中表单字段名称周围的引号。

相关内容