简体   繁体   中英

Can I retrieve a list of AD OUs using a variable?

I am trying make a form to allow me to enter site name in a textbox and get all of the OUs for that specific site. I can get the powershell command to work if I enter the site name directly without the variable but I have 80+ sites and would rather not have to configure for that many sites.

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()

I believe your .Click event could be simplified to this, which, should work properly:

$Button_Get.Add_Click({
    Get-ADOrganizationalUnit -Filter "Name -Like '*$($TextBox_Site.Text.Trim())*'" |
        Select-Object Name, Distinguishedname |
        Out-GridView -PassThru -Title "Select the OU"
})

The issue with your code is that, first the Filter is incorrect, it will not allow the variable expansion:

'Name -Like "$Site_Location"'

Should be:

"Name -Like '$Site_Location'"

And the second issue is that $Site_Location is being assigned outside the event instead of inside, this assignment happens only once when the form is loaded instead of each time there is a clicking of the button .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM