简体   繁体   中英

Is there any way linking GPO with ou using the ou canonical name?

Is there a way i can link GPO with an OU by its canonical name?

New-Gplink -gpo <gponame> -target <CANONICAL NAME>

From what I've read we can use only the distinguished name. maybe there's a way around it? Im using this to save all of them in a variable

$test=Get-ADOrganizationalUnit -Filter * -Properties CanonicalName | Select-Object -Property CanonicalName

now using a gui i open a window for the user to select an ou from there

foreach ($item in $test){ 
[void]$listbox.items.add($item)}

So now i can catch the user choise by using:

$catch = $listbox.selected.item

so if i now would like to link the gpo using

new-gplink -gpo <gponame> -target $catch 

I will get an error. Any help would be much appreciated!

Try it this way:

# Get the OU objects, which include the DN, plus the CN
$test = Get-ADOrganizationalUnit -Filter * -Properties CanonicalName

# Add the values to your listbox - there's an AddRange method for arrays
[void]$listbox.Items.AddRange($test.CanonicalName)

# Find the object that matches the currently selected Canonical Name
$CatchDN = $test | Where-Object { $_.CanonicalName -eq ($listbox.SelectedItem) }

# Because $CatchDN is an object, just link the GPO using the object.
New-GpLink -Name <gponame> -Target $CatchDN

No tested the above using forms, but it should work as intended.

Ok, using your own form code:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'GPO Connector V1.0'
$form.Size = '600,200'
$form.StartPosition = 'CenterScreen'

$button1 = New-Object System.Windows.Forms.Button
$button1.Location = '10,120'
$button1.Size = '75,23'
$button1.Text = 'Link'
$button1.Anchor = 'Left,Bottom'
$button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $button1

$button2 = New-Object System.Windows.Forms.Button
$button2.Location = '90,120'
$button2.Size = '75,23'
$button2.Text = 'UnLink'
$button2.Anchor = 'Left,Bottom'
$Button2.Add_Click($Button2_Click)

$label = New-Object System.Windows.Forms.Label
$label.Location = '80,20'
$label.Size = '480,20'
$label.Text = 'SELECT GPO And Corresponding OU (ONLY WORKSTATION OU)'

$form.Controls.AddRange(@($label,$button1,$button2))

$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = '10,40'
$listBox.Size = '260,80'
$listbox.Anchor = 'Top,Left,Bottom'

$listBox2 = New-Object System.Windows.Forms.ListBox
$listBox2.Location = '300,40'
$listBox2.Size = '260,80'
$listbox2.Anchor = 'Top,Left,Bottom,Right'

$form.Controls.AddRange(@($listBox,$listBox2))

$button1.Add_Click({
    # Find the object that matches the currently selected Canonical Name
    $CatchDN = $Script:OUlist | Where-Object { $_.CanonicalName -eq ($listbox2.SelectedItem) }
    Write-Host "Selected CN object: $CatchDN"
    # Because $CatchDN is an object, just link the GPO using the object.
    $LinkedGP = $Script:GPOlist | Where-Object { $_.DisplayName -eq ($listBox.SelectedItem) }
    Write-Host "Selected GPO: "$LinkedGP.DisplayName
    $LinkedGP | New-GpLink -Target $CatchDN -WhatIf
})

# Show form first, then load data to lists
$form.Add_Shown({
    # Retrieve GPO list
    $Script:OUlist = Get-ADOrganizationalUnit -Filter * -Properties CanonicalName
    # Add OU CN to listbox
    $listbox2.Items.AddRange($Script:OUlist.CanonicalName)
    $Script:GPOlist = Get-GPO -All # list of GPO
    $listbox.Items.AddRange($Script:GPOlist.DisplayName)
})

$form.Topmost = $true
$result = $form.ShowDialog()
$form.Dispose()

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