【问题标题】:Is there any way linking GPO with ou using the ou canonical name?有没有办法使用 ou 规范名称将 GPO 与 ou 联系起来?
【发布时间】:2020-05-11 08:06:46
【问题描述】:

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

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

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

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

现在使用 gui 我打开一个窗口供用户从那里选择一个 ou

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

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

$catch = $listbox.selected.item

所以如果我现在想使用链接 gpo

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

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

【问题讨论】:

    标签: windows powershell gpo


    【解决方案1】:

    试试这个方法:

    # 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
    

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

    【讨论】:

    • 第一行 - 如果这没有管道到 select-object 那么列表框将没有规范名称但会有专有名称,其余代码不会这样做..谢谢你
    • Get-AdOrgansationalUnit 默认返回 DN,如果您指定 CanonicalName 属性参数,它会在默认属性的基础上返回。如果您发布您的完整代码,那么也许我们可以让您正确 - 以上应该可以工作。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      相关资源
      最近更新 更多