Powershell - 从CSV文件向AD组添加用户

时间:2012-03-16 05:33:33

标签: powershell powershell-v2.0

我知道这已经完成了死亡,但我将此作为与PowerShell的学习经验。有人可以看看我的代码并告诉我哪里出错了吗? Noob代码警告!

# 
# Add User to an AD Group
# 
# 

# get arguements and quit if they dont exist 
$CSV = $args[0]
$GROUP = $args[1]
if (! $CSV) {
Write-Host "Please format this command as 'AddUsersToGroup <csv file> <AD group>'"
Write-Host "CSV file must have the header 'UserName' with AD usernames following"
exit
}


# Read csv file for users and add to AD group
Import-module ActiveDirectory  
Import-CSV "$CSV" | % {  

# Get existing users from AD group
$ExistingGroup = "Get-ADGroupMember $GROUP | Select-Object SamAccountName"

# create new array removing existing users from the csv
$NewGroup = $ExistingGroup | where {$CSV -notcontains $_}

# add the users to the AD Group from the new array
Add-ADGroupMember -Identity $NewGroup -Member $_.UserName
exit
}

1 个答案:

答案 0 :(得分:1)

尝试删除双引号:

$ExistingGroup = Get-ADGroupMember $GROUP | Select-Object SamAccountName

带引号你将字符串值分配给变量而不是命令的结果

相关问题