我的脚本遇到了这个奇怪的问题。它可以很好地创建用户,但是当我添加组成员身份时,它似乎无法正常工作。这很奇怪,我不知道为什么会出现空值错误。
$Path = "C:\T2\BulkUsers\AccountProvisioningTemplate.csv"
$CSVs = Import-CSV $Path
$Group1 = 'G1'
$Group2 = "G2"
$FirstNames = @()
$Initials = @()
$LastNames = @()
$FullNames = @()
$DisplayNames = @()
$EmployeeNumbers = @()
$TelephoneNumbers = @()
$SAMs = @()
$UPNs = @()
$Companys = @()
$Departments = @()
$Offices = @()
$Citys = @()
$States = @()
$Zips = @()
$Titles = @()
$Contractor = @()
ForEach($CSV in $CSVs) { $FirstNames += ($CSV.FirstName) }
ForEach($CSV in $CSVs) { $Initials += ($CSV.Initials) }
ForEach($CSV in $CSVs) { $LastNames += ($CSV.LastName) }
ForEach($CSV in $CSVs) { $FullNames += ($CSV.FullName) }
ForEach($CSV in $CSVs) { $DisplayNames += ($CSV.DisplayName) }
ForEach($CSV in $CSVs) { $EmployeeNumbers += ($CSV.EmployeeNumber) }
ForEach($CSV in $CSVs) { $TelephoneNumbers += ($CSV.TelephoneNumber) }
ForEach($CSV in $CSVs) { $SAMs += ($CSV.SAM) }
ForEach($CSV in $CSVs) { $UPNs += ($CSV.UPN) }
ForEach($CSV in $CSVs) { $Companys += ($CSV.Company) }
ForEach($CSV in $CSVs) { $Departments += ($CSV.Department) }
ForEach($CSV in $CSVs) { $Offices += ($CSV.Office) }
ForEach($CSV in $CSVs) { $Citys += ($CSV.City) }
ForEach($CSV in $CSVs) { $States += ($CSV.State) }
ForEach($CSV in $CSVs) { $Zips += ($CSV.Zip) }
ForEach($CSV in $CSVs) { $Titles += ($CSV.Title) }
ForEach($CSV in $CSVs) { $Contractor += ($CSV.Contractor) }
$FN = 0
$IN = 0
$LN = 0
$FN1 = 0
$DN = 0
$EN = 0
$TN = 0
$S = 0
$U = 0
$Comp = 0
$Dept = 0
$Off = 0
$C = 0
$S = 0
$Z = 0
$T = 0
$ConC = 0
$Con = "No"
$OU = "OU=General Users & Computers,DC=corp,DC=abc,DC=com"
$Password1 = ConvertTo-SecureString "Changeme123" -AsPlainText -Force
ForEach($CSV in $CSVs) { New-ADUser -GivenName $CSVs.FirstName[$FN] -Initials $CSVs.Initials[$IN] -Surname $CSVs.LastName[$LN] -name $CSVs.FullName[$FN1] -DisplayName $CSVs.DisplayName[$DN] -EmployeeID $CSVs.EmployeeNumber[$EN] -EmployeeNumber $CSVs.EmployeeNumber[$EN] -OfficePhone $CSVs.TelephoneNumber[$TN] -SamAccountName $CSVs.SAM[$S] -UserPrincipalName $CSVs.UPN[$U] -Path $OU -Company $CSVs.Company[$Comp] -Department $CSVs.Department[$Dept] -Office $CSVs.Office[$Off] -City $CSVs.City[$C] -State $CSVs.State[$S] -PostalCode $CSVs.Zip[$Z] -Title $CSVs.Title[$T] -AccountExpirationDate $null -AccountPassword $Password1 -CannotChangePassword $False -ChangePasswordAtLogon $False -Enabled $True; $FN++; $IN++; $LN++; $FN1++; $DN++; $EN++; $TN++; $S++; $U++; $Comp++; $Dept++; $Off++; $C++; $S++; $Z++; $T++ }
Start-Sleep 2
ForEach($CSV in $CSVs) { IF ($Contractor[$ConC] -Match $Con) { Add-ADGroupMember -Identity $Group1 -Members $CSVs.SAM[$S]; Add-ADGroupMember -Identity $Group2 -Members $CSVs.SAM[$S]; $S++ } ELSE { $S++ } ; $CSVs.SAM[$S]; $SAMs[$S]; $ConC++ }
我收到错误消息,但不知道为什么。它可以很好地创建用户,当我输入对 $SAMs[$] 的引用时,它会在 CSV 中为用户报告正确的 SamAccountName。
Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\T2\BulkUsers\BulkUserImport.ps1:69 char:120
+ ... roup1 -Members $SAMs[$S]; Add-ADGroupMember -Identity $Group2 -Members $SAMs[$S] ...
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
pMember
Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\T2\BulkUsers\BulkUserImport.ps1:69 char:176
+ ... roup2 -Members $SAMs[$S]; $S++ } ELSE { $S++ } ; $ConC++ }
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
pMember
Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\T2\BulkUsers\BulkUserImport.ps1:69 char:120
+ ... roup1 -Members $SAMs[$S]; Add-ADGroupMember -Identity $Group2 -Members $SAMs[$S] ...
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
pMember
Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\T2\BulkUsers\BulkUserImport.ps1:69 char:176
+ ... roup2 -Members $SAMs[$S]; $S++ } ELSE { $S++ } ; $ConC++ }
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
pMember
有趣的是,如果我注释掉用户创建部分,我会得到正确的 SAM 帐户名称,但如果它运行,我什么也得不到。