运行我的poweshell脚本会产生错误,并且不会加入新用户

时间:2019-06-27 15:15:57

标签: powershell

我正在尝试为正在使用的公司使用Powershell的入门用户,但是遇到一个问题,指出未找到目录对象。谁能协助我解决我的错误以及如何解决该错误?

我尝试删除城市,组织单位并尝试多次编辑excel csv文件,但所有测试均失败

# Import active directory module for running AD cmdlets
Import-Module activedirectory

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv C:\Users\padmin\Documents\users.csv

#Loop through each row containing user details in the CSV file 
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a variable as below

$Username   = $User.username
$Password   = $User.password
$Firstname  = $User.firstname
$Lastname   = $User.lastname
$OU         = $User.ou #This field refers to the OU the user account is to be created in
$email      = $User.email
$streetaddress = $User.streetaddress
#$city       = $User.city
$zipcode    = $User.zipcode
$state      = $User.state
$country    = $User.country
$telephone  = $User.telephone
$jobtitle   = $User.jobtitle
$company    = $User.company
$department = $User.department
$Password = $User.Password


#Check to see if the user already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username})
{
     #If user does exist, give a warning
     Write-Warning "A user account with username $Username already exist in Active Directory."
}
else
{
    #User does not exist then proceed to create the new user account

    #Account will be created in the OU provided by the $OU variable read from the CSV file
    New-ADUser `
        -SamAccountName $Username `
        -UserPrincipalName "$Username@greenkeyllc.com" `
        -Name "$Firstname $Lastname" `
        -GivenName $Firstname `
        -Surname $Lastname `
        -Enabled $True `
        -DisplayName "$Lastname, $Firstname" `
        -Path $OU `
        #-City $city `
        -Company $company `
        -State $state `
        -StreetAddress $streetaddress `
        -OfficePhone $telephone `
        -EmailAddress $email `
        -Title $jobtitle `
        -Department $department `
        -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True

}
}

预期结果是将用户添加到本地活动目录内的适当组织单位(不同的办公室位置)中。实际结果是以下错误。

New-ADUser : Directory object not found
At C:\Users\padmin\Documents\bulk_users1.ps1:41 char:3
+         New-ADUser `
+         ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (CN=Bob Jake,CN=...eenkey,DC=local:String) [New-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.NewADUser

-Company : The term '-Company' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
At C:\Users\padmin\Documents\bulk_users1.ps1:51 char:13
+             -Company $company `
+             ~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-Company:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

1 个答案:

答案 0 :(得分:1)

脚本中间的#注释行破坏了您的预期行继续:

    -Path $OU `
    #-City $city `
    -Company $company `

将参数放入哈希表中,并改为splat them

$NewADUserArgs = @{
    SamAccountName = $Username
    UserPrincipalName = "$Username@greenkeyllc.com"
    Name = "$Firstname $Lastname"
    GivenName = $Firstname
    Surname = $Lastname
    Enabled = $True
    DisplayName = "$Lastname, $Firstname"
    Path = $OU
    # City = $city
    Company = $company
    State = $state
    StreetAddress = $streetaddress
    OfficePhone = $telephone
    EmailAddress = $email
    Title = $jobtitle
    Department = $department
    AccountPassword = (convertto-securestring $Password -AsPlainText -Force)
    ChangePasswordAtLogon = $true
}

New-ADUser @NewADUserArgs

现在,您可以轻松地将参数表中的单个条目注释掉,而不必担心换行符和所有讨厌的反引号