PowerShell:NEW-ADGroup /在Active Directory中创建多个组/ -路径参数问题

6tqwzwtp  于 2023-04-30  发布在  Shell
关注(0)|答案(1)|浏览(117)

简而言之,这就是错误:Error,下面是脚本:

#
Import-Module ActiveDirectory
 
#
$ADGroups = Import-Csv C:\temp\NewGroups.csv -Delimiter ";"

#Create group in "Users"
$OU = "CN=Users"
$Path2 = "CN=Users,DC=master,DC=int"

# Loop through each row containing group details in the CSV file
foreach ($Group in $ADGroups) {

    #Read group data from each field in each row and assign the data to a variable as below
    $groupName = $Group.groupname
    $SAM = $Group.SAMName
    $gCategory = $Group.grouptype
    $gScope = $Group.groupscope
    $dispName = $Group.teamname
    $Path = $Group.path
    $desc = $Group.description
    # Check to see if the user already exists in AD
    if (Get-ADGroup -F { SamAccountName -eq $groupName }) {
        
        # Group existance warning
        Write-Warning "A $groupName group  already exists in Active Directory."
    }
    else {
    Write-Host "This is SAM: $SAM"
    Write-Host "This is Path: $Path"
        
        New-ADGroup `
            -Path $Path `
            -Name "$groupName" `
            -GroupScope $gScope `
            -SamAccountName $SAM `
            -GroupCategory $gCategory `
            -DisplayName $dispName `
            -Description  $desc
                                    
        # If group is created, show message.
        Write-Host "The $groupName group is created." -ForegroundColor Cyan
    }
}

Read-Host -Prompt "Press Enter to exit"

The script png version,这里是。csv文件:CSV file
问题出在“Path”参数上。如果我只是跳过它,则会添加组,显然CN=Users将是默认值,这与MS Web上的描述完全相同:https://learn.microsoft.com/en-us/powershell/module/activedirectory/new-adgroup?view=windowsserver2022-ps。但是我已经尝试了很多方法来使用“Path”变量,甚至不止一个来划分CN=Users和DC=。..但是它总是倾向于将“Name”参数中的任何内容添加到Path作为CN。它结合了“名称”与“路径”,具有2x CN,如错误屏幕中:“CN=设计者,CN=用户,DC=主控者,DC=int”。我试过使用双引号或无引号传递变量到Path参数-结果相同。我还手动输入了路径,省略了使用变量向路径传递值-相同。我不明白为什么?它在阅读。csv文件不正确?如果我使用下面的简单脚本:Working simpler script完全没有问题。..我发现了一篇文章,其中用户以实际相同的方式将值传递给Path参数:Powershell with Active Directory creating groups,没有问题。..

3ks5zfa0

3ks5zfa01#

看看您在评论中回复的内容,我相信错误源于您使用New-ADGroupcmdlet的方式,使用了所有这些反引号。(尤其是第一个反勾号)。
尝试在需要大量参数的cmdlet上使用Splatting
这样,您就不需要那些可怕的反引号,同时代码仍然清晰且易于维护。

Import-Module ActiveDirectory

$ADGroups = Import-Csv -Path 'C:\temp\NewGroups.csv' -Delimiter ";"

# Loop through each row containing group details in the CSV file
foreach ($Group in $ADGroups) {
    $groupName = $Group.Groupname  # for convenience

    if (Get-ADGroup -Filter "SamAccountName -eq '$groupName'") {
        # Group existance warning
        Write-Warning "A $groupName group already exists in Active Directory."
    }
    else {
        # build a Hashtable for splatting the parameters 
        # (no need to put all in separate variables first)
        $groupParams = @{
            Path           = $Group.Path
            Name           = $groupName
            DisplayName    = $Group.TeamName
            SamAccountName = $Group.SAMName
            Description    = $Group.Description
            GroupScope     = $Group.Groupscope
            GroupCategory  = $Group.Grouptype
            ErrorAction    = 'Stop'
        }
        # create the new group
        try {
            New-ADGroup @groupParams
            Write-Host "The $groupName group is created." -ForegroundColor Cyan
        }
        catch {
            # write the error to console and proceed with the next group
            Write-Warning "Error: $($_.Exception.Message)"
        }
    }
}

相关问题