繁体   English   中英

有没有办法使用 ou 规范名称将 GPO 与 ou 联系起来?

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

有没有办法通过规范名称将 GPO 与 OU 链接起来?

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

根据我的阅读,我们只能使用专有名称。 也许有办法解决它? 我用它把它们都保存在一个变量中

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

现在使用gui我为用户打开一个window到select一个ou从那里

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

所以现在我可以通过使用来捕捉用户的选择:

$catch = $listbox.selected.item

所以如果我现在想使用链接 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

没有使用 forms 测试上述内容,但它应该按预期工作。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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