AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 624279
Accepted
Maurício Mota
Maurício Mota
Asked: 2014-08-27 16:30:27 +0800 CST2014-08-27 16:30:27 +0800 CST 2014-08-27 16:30:27 +0800 CST

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

  • 772

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

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

"OU=IT Dept,OU=Users,DC=local,DC=contoso"

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

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

"OU=Helpdesk,OU=IT Dept,OU=Users,DC=local,DC=contoso"    

如果

"OU=IT Dept,OU=Users,DC=local,DC=contoso"    

还不存在。

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

谢谢

active-directory
  • 3 3 个回答
  • 7616 Views

3 个回答

  • Voted
  1. Best Answer
    Mathias R. Jessen
    2014-08-28T04:00:24+08:002014-08-28T04:00:24+08:00

    正如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
        }
    }
    

    使用 WHATIF

    # The desired resulting OU DN  
    $DN = "OU=Helpdesk,OU=IT Dept,OU=Users,DC=local,DC=contoso"
    New-OrganizationalUnitFromDN $DN -WhatIf
    

    没有 WHATIF

    # The desired resulting OU DN  
    $DN = "OU=Helpdesk,OU=IT Dept,OU=Users,DC=local,DC=contoso"
    New-OrganizationalUnitFromDN $DN
    

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

    • 8
  2. Jamie Thompson
    2017-02-17T14:46:44+08:002017-02-17T14:46:44+08:00

    的输出$NextOU.IndexOf("OU=")区分大小写,对于所有小写ou= 尝试都将返回 -1:$NextOU.IndexOf("OU=",[StringComparison]"CurrentCultureIgnoreCase")

    • 1
  3. Tomasz Szymon
    2021-09-17T05:06:04+08:002021-09-17T05:06:04+08:00

    试试这种方法;它更简单: https ://dimitri.janczak.net/2016/04/20/recursive-ou-creation-with-powershell/

    • 0

相关问题

  • 如果以域用户身份远程登录,PC 速度极慢

  • 如何在 Windows 2003 的 ou 级别应用策略

  • 允许用户更改其 Active Directory 密码的 Web 界面

  • MOSS 2007 无法使用 ActiveDirectoryMembershipProvider 配置表单身份验证

  • 通过 VPN 更改 Active Directory 密码

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve