简体   繁体   English

用于将 Active Directory 用户添加到组的 Powershell 脚本

[英]Powershell Script for adding Active Directory users to groups

Hey guys in a bit of a pickle here with this script.嘿家伙在这里有点泡菜这个脚本。

Import-Module ActiveDirectory

$list = Import-csv "\\ca23fs1\IT\Scripts\Active Directory\New-Hire\Account Setup\Users-Groups.csv"
    ForEach ($item in $list) {
            $users = $item.Username
            $groups = $item.Group
        Add-ADPrincipalGroupMembership -Identity $users -MemberOf $groups -Whatif |
        Set-ADUser $users -HomeDirectory "Path" -Homedrive H -WhatIf
        }

When running $list , i get the output from the CSV file, but when running $users or $groups nothing shows up so i think I'm missing something in my syntax that should be grabbing the data from the "Username" and "Group" headers in the CSV and passing it to the loop.运行$list ,我从 CSV 文件中获取输出,但是在运行$users$groups没有显示任何内容,所以我想我的语法中缺少一些应该从“用户名”和“组”中获取数据的内容" CSV 中的标头并将其传递给循环。

Here is what my CSV looks like:这是我的 CSV 的样子:

CSV

And here is the error:这是错误:

错误

Any help would be greatly appreciated.任何帮助将不胜感激。

*edit *编辑

Thought it might be the CSV so I made one from a txt file like so:以为可能是 CSV,所以我从 txt 文件中制作了一个,如下所示:

Username,Group
User1,group1
,group2
,group3
,group4
,group5

Still didnt work :(仍然没有工作:(

I think it might be because I am using blank columns.我想这可能是因为我使用的是空白列。 Going to try separate CSV files for users and groups.将尝试为用户和组使用单独的 CSV 文件。

$item is not populated. $item 未填充。 You state that $users = $item.username but $item is null at that point.您声明 $users = $item.username 但此时 $item 为空。

Try this:尝试这个:

$list = Import-csv "PATH"

ForEach ($item in $list) {
$users = $item.Username
$groups = $item.Group


Add-ADPrincipalGroupMembership -Identity $users -MemberOf $groups -Whatif |
Set-ADUser $users -HomeDirectory "Path" -Homedrive H -WhatIf
}

This basically says: For each item in list, use the .username and .group syntax to populate the users and groups variable.这基本上是说:对于列表中的每个项目,使用 .username 和 .group 语法来填充用户和组变量。 Then, run the Add and Set commands.然后,运行添加和设置命令。 Remember, this is a loop that runs once for each $item in the $list.请记住,这是一个对 $list 中的每个 $item 运行一次的循环。

Test code:测试代码:

$list = Import-csv "PATH"

ForEach ($item in $list) {
$user = $item.Username
$group = $item.Group
Write-Host "$user is in $group"
}

The error comes from not specifying a valid Identity value for the user or possibly even not specifying a valid MemberOf value.该错误来自未为用户指定有效的Identity值,甚至可能未指定有效的MemberOf值。

According to the docs , both parameters want either a DistinguishedName , an ObjectGUID , a SID , or a SamAccountName to work with.根据docs ,这两个参数都需要使用DistinguishedNameObjectGUIDSIDSamAccountName By default, this cmdlet does not generate any output, unless you use the -PassThru parameter.默认情况下,此 cmdlet 不会生成任何输出,除非您使用-PassThru参数。

In your case, I think you are trying to use the SamAccountName and that should work just fine.在您的情况下,我认为您正在尝试使用SamAccountName并且应该可以正常工作。 However, the problem here seems to be the CSV file you are using.但是,这里的问题似乎是您使用的 CSV 文件。

When saving an Excel as CSV file, one might expect te result would be a comma separated values file as the name CSV implies.将 Excel 保存为 CSV 文件时,人们可能会期望结果将是逗号分隔值文件,正如名称 CSV 所暗示的那样。
Unfortunately, Excel uses the current systems locale to determine the delimiter to be used.不幸的是,Excel 使用当前系统区域设置来确定要使用的分隔符。 On my Dutch machine for instance, it will use a semi-colon ;例如,在我的荷兰机器上,它将使用分号; as delimiter.作为分隔符。

If you do not check that, the Import-Csv cmdlet without the -Delimiter parameter will assume a comma .如果您不检查,则不带-Delimiter参数的Import-Csv cmdlet 将假定为逗号 So, after exporting from Excel, open the csv file with Notepad or any other text editor to see what is created and which delimiter character is actually used.因此,从 Excel 导出后,使用记事本或任何其他文本编辑器打开 csv 文件以查看创建的内容以及实际使用的分隔符。 You may also overcome that problem by using the -UseCulture switch on the Import-Csv cmdlet, but this will only make sense if you are exporting from Excel and importing in PowerShell on the same machine (or at least one that uses the same culture)您也可以通过在Import-Csv cmdlet 上使用-UseCulture开关来解决该问题,但这只有在您从 Excel 导出并在同一台机器上的 PowerShell 中导入时才有意义(或至少一台使用相同文化的机器)

What you SHOULD have is a valid CSV file, something like this:您应该拥有一个有效的 CSV 文件,如下所示:

"UserName","Group"
"user1","group1"
"user2","group1"
"user3","group1"

Since your manually created CSV has empty values for the UserName column, it is not surprising you are getting the error.由于您手动创建的 CSV 的UserName列具有空值,因此您收到错误并不奇怪。

Then, with a few more changes to your code, this should work:然后,对您的代码进行更多更改,这应该可以工作:

Import-Module ActiveDirectory

# assuming the CSV uses commas as delimiter character. Otherwise, specify -Delimiter parameter
$list = Import-csv "\\ca23fs1\IT\Scripts\Active Directory\New-Hire\Account Setup\Users-Groups.csv"
foreach ($item in $list) {
    # if you are not sure the CSV actually contains valid data to work with, check it here first:
    if ([string]::IsNullOrWhiteSpace($item.UserName) -or [string]::IsNullOrWhiteSpace($item.Group)) {
        Write-Warning "Empty entry found in the CSV file"
    }
    else {
        Add-ADPrincipalGroupMembership -Identity $item.UserName -MemberOf $item.Group -Whatif
        # The DriveLetter must be a single, uppercase letter and the colon is required.
        Set-ADUser -Identity $item.UserName -HomeDirectory "Path" -HomeDrive 'H:' -WhatIf
    }
}

Hope that helps希望有帮助

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

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