如何在 Powershell 上递归创建组织单位?

Mau*_*ota 5 powershell active-directory organizational-unit

我正在编写一个 Powershell 脚本,以将所有公司用户从 CSV 文件填充到 Active Directory。

该脚本使用 Powershell 命令New-ADUser,它应该知道,对于每个用户,添加它们的路径在哪里,例如:

"OU=IT Dept,OU=Users,DC=local,DC=contoso"
Run Code Online (Sandbox Code Playgroud)

问题是:这个 Active Directory 还没有任何用户,所以没有创建组织单位,每当我运行脚本时,如果路径不存在,则不会创建用户。

我已经查找了创建组织单位的命令,例如New-ADOrganizationalUnitdsadd,但是它们不支持递归创建,例如

"OU=Helpdesk,OU=IT Dept,OU=Users,DC=local,DC=contoso"    
Run Code Online (Sandbox Code Playgroud)

如果

"OU=IT Dept,OU=Users,DC=local,DC=contoso"    
Run Code Online (Sandbox Code Playgroud)

还不存在。

有没有办法使用New-ADOrganizationalUnit做到这一点

谢谢

Mat*_*sen 8

正如Mike在评论中提到的那样,您需要逐步遍历树中的每个级别并测试 OU 祖先是否存在,然后从最顶层和向下创建不存在的那些。

只要New-ADOrganisationalUnit没有 -Recurse 参数,这是我能想到的最优雅的方式:

function New-OrganizationalUnitFromDN
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        [string]$DN
    )

    # A regex to split the DN, taking escaped commas into account
    $DNRegex = '(?<![\\]),'

    # Array to hold each component
    [String[]]$MissingOUs = @()

    # We'll need to traverse the path, level by level, let's figure out the number of possible levels 
    $Depth = ($DN -split $DNRegex).Count

    # Step through each possible parent OU
    for($i = 1;$i -le $Depth;$i++)
    {
        $NextOU = ($DN -split $DNRegex,$i)[-1]
        if($NextOU.IndexOf("OU=") -ne 0 -or [ADSI]::Exists("LDAP://$NextOU"))
        {
            break
        }
        else
        {
            # OU does not exist, remember this for later
            $MissingOUs += $NextOU
        }
    }

    # Reverse the order of missing OUs, we want to create the top-most needed level first
    [array]::Reverse($MissingOUs)

    # Prepare common parameters to be passed to New-ADOrganizationalUnit
    $PSBoundParameters.Remove('DN')

    # Now create the missing part of the tree, including the desired OU
    foreach($OU in $MissingOUs)
    {
        $newOUName = (($OU -split $DNRegex,2)[0] -split "=")[1]
        $newOUPath = ($OU -split $DNRegex,2)[1]

        New-ADOrganizationalUnit -Name $newOUName -Path $newOUPath @PSBoundParameters
    }
}
Run Code Online (Sandbox Code Playgroud)

使用 WHATIF

# The desired resulting OU DN  
$DN = "OU=Helpdesk,OU=IT Dept,OU=Users,DC=local,DC=contoso"
New-OrganizationalUnitFromDN $DN -WhatIf
Run Code Online (Sandbox Code Playgroud)

没有什么

# The desired resulting OU DN  
$DN = "OU=Helpdesk,OU=IT Dept,OU=Users,DC=local,DC=contoso"
New-OrganizationalUnitFromDN $DN
Run Code Online (Sandbox Code Playgroud)

如果路径中有不存在的非 OU 容器,它将中断。