我编写了一个脚本,用于从CSV文件中获取信息,并且应该根据列中的变量创建AD帐户和MSOL帐户。该脚本仅适用于我测试过的一个用户。
$AccountList = import-csv "C:\File\path\input.csv"
ForEach ($Person in $AccountList) {
$Accountname = $firstname.Trim().ToLower() + "." + $lastname.Trim().ToLower()
$Firstname = $AccountList.pFirstName
$LastName = $AccountList.pLastName
$UPN = $Accountname + "@company.com"
$displayname = $firstname + " " + $Lastname
$Department = $AccountList.pDepartment
$Location = $AccountList.pLocation|
ForEach {
New-ADUser -SamAccountName $Accountname -Name $displayname -DisplayName $displayname -Surname $Lastname -GivenName $Firstname -UserPrincipalName $UPN -Department $Department -Office $Location -AccountPassword (ConvertTo-SecureString -AsPlainText "X" -Force) -ChangePasswordAtLogon $true -Enabled $true -PasswordNeverExpires $false -Path "CN=Users,DC=medsinmotion,DC=local"
}
ForEach ($Person in $AccountList) {
New-MsolUser -DisplayName $displayname -FirstName $Firstname -LastName $Lastname -UserPrincipalName $UPN -Department $Department -UsageLocation US -LicenseAssignment reseller-account:DESKLESSPACK -ForceChangePassword $true
if ($Department -match "Call Center" -or "Retail") {
Add-MailboxPermission -Identity CallCenter -User $Accountname -AccessRights FullAccess
Add-DistributionGroupMember -Identity callctr@company.com -Member $Accountname
}
Else {
Set-MsolUserLicense -UserPrincipalname $UPN -AddLicenses O365_BUSINESS_PREMIUM -RemoveLicenses reseller-account:DESKLESSPACK
}
Add-DistributionGroupMember -Identity all -Member $UPN
}
}
问题是当有超过2个用户时,它会为我尝试设置的所有变量发出错误;错误是:
"无法转换' System.Object []'到#System; System.String'参数' SamAccountName'需要。不支持指定的方法"
它会遍历所有变量(Displayname
,Samaccountname
和UPN
),但会出现错误。
-Raw
没有Import-Csv
,Get-Content
就像col4
一样。
有没有办法将其转换为系统字符串?请让我知道可能的解决方案
*必须重做它
答案 0 :(得分:1)
评论中提到的@boxdog的第一个也是最重要的问题是,您正在循环$AccountList
,每个元素都是$Person
。因此,如果您想要当前元素,则应使用$person
。
第二个问题是在您定义$AccountName
和$firstname
之前定义$lastname
。
第三,你只是将$AccountList.pLocation
换成了另一个循环。
第四,$Department -match "Call Center" -or "Retail"
始终为真,因为$Department -match "Call Center"
和"Retail"
是单独的条件,并且当字符串Retail
被强制转换为布尔值时,因为它不是空的它永远都是真的。 |
是正则表达式或运算符,否则您需要-match
之后的另一个-or
语句。
使用splatting在哈希表中定义参数,然后将哈希表作为命令的参数传递,也可以大大简化您的脚本。
$AccountList = import-csv "C:\File\path\input.csv"
ForEach ($Person in $AccountList) {
$NewUserParams = @{
GivenName = $Person.pFirstName
Surname = $Person.pLastName
SamAccountName = "$($Person.pFirstName.Trim().ToLower()).$($Person.pLastName.Trim().ToLower())"
UserPrincipalName = "$($Person.pFirstName.Trim().ToLower()).$($Person.pLastName.Trim().ToLower())@company.com"
Name = "$($Person.pFirstName) $($Person.pLastName)"
DisplayName = "$($Person.pFirstName) $($Person.pLastName)"
Department = $Person.pDepartment
Office = $Person.pLocation
AccountPassword = (ConvertTo- SecureString -AsPlainText "X" -Force)
ChangePasswordAtLogon = $True
Enabled = $True
PasswordNeverExpires = $False
Path = "CN=Users,DC=medsinmotion,DC=local"
}
New-ADUser @NewUserParams
$NewMsolUserParams = @{
FirstName = $NewUserParams.GivenName
LastName = $NewUserParams.Surname
UserPrincipalName = $NewUserParams.UserPrincipalName
DisplayName = $NewUserParams.displayname
Department = $NewUserParams.Department
UsageLocation = 'US'
LicenseAssignment = 'reseller-account:DESKLESSPACK'
ForceChangePassword = $True
}
New-MsolUser @NewMsolUserParams
if ($NewMsolUserParams.Department -match "Call Center|Retail") {
Add-MailboxPermission -Identity CallCenter -User $NewUserParams.SamAccountName -AccessRights FullAccess
Add-DistributionGroupMember -Identity callctr@company.com -Member $NewUserParams.SamAccountName
} Else {
Set-MsolUserLicense -UserPrincipalname $NewUserParams.UserPrincipalName -AddLicenses O365_BUSINESS_PREMIUM -RemoveLicenses reseller-account:DESKLESSPACK
}
Add-DistributionGroupMember -Identity all -Member $NewUserParams.UserPrincipalName
}