有什么方法可以使用 ou 规范名称将 GPO 与 ou 链接起来吗?

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

有什么方法可以 link GPO 和 OU 的规范名称?

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

据我所知,我们只能使用专有名称。也许有办法解决它? 我用它把它们全部保存在一个变量中

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

现在使用图形用户界面,我打开一个 window 供用户从那里 select 一个 ou

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

所以现在我可以通过以下方式捕捉用户选择:

$catch = $listbox.selected.item

所以如果我现在想link gpo 使用

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

我会得到一个错误。 任何帮助将不胜感激!

这样试试:

# 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

没有使用表单测试以上内容,但它应该可以正常工作。

好的,使用您自己的表单代码:

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