New-ADUser OtherAttributes var from CSV

问题描述 投票:0回答:1

我正在使用下面的Powershell脚本从CSV文件创建新的AD帐户。我最近为$ extensionAttribute1和$ extensionAttribute2添加了变量。我还添加了以下内容-OtherAttributes = @ {'extensionAttribute1'= $ extensionAttribute1;'extensionAttribute2'= $ extensionAttribute2}

如何纠正以下错误?

New-ADUser:无法验证参数'OtherAttributes'上的参数。参数为null或参数集合的元素包含空值。在D:\ OneDrive \ Element Care \ Powershell \ SACRequest中-通过CSV.ps1:62 char:30 + ... -OtherAttributes @ {'extensionAttribute1'= $ extensionAttribute1}

创建帐户

ps脚本如下:

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

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv "\\server\path\file.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.'First Name:'
    $Lastname   = $User.'Last Name:'
    $OU         = 'OU=CONTRACTORS,OU=ACCOUNTS,OU=organization,DC=domain,DC=lan'
    $Descritpion = $User.'Account Type'
    $company    = $User.'Employer:'
    $extensionAttribute1 = $User."Submitter Name" # The employee who originally submitted the request.
    $extensionAttribute2 = $User."Submitter email" # The email for who originally submitted the request.

   # $email      = $User.email
   # $streetaddress = $User.streetaddress
   # $city       = $User.city
   # $zipcode    = $User.zipcode
   # $state      = $User.state
   # $country    = $User.country
   # $telephone  = $User.telephone
   # $jobtitle   = $User.jobtitle
   # $department = $User.department

    #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 "[email protected]" `
            -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 `
            -Description $Descritpion `
            -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True `
            -OtherAttributes @{'extensionAttribute1' = $extensionAttribute1;'extensionAttribute2'= $extensionAttribute2}
    }
    }
powershell csv var
1个回答
0
投票

您收到的错误是IMO来自您在原始代码中所做的错字引起的。除此之外,我建议您对Splatting这样的cmdlet使用New-ADUser,它可以具有很多参数。这样,您就可以保持代码的可读性和可维护性,并且无需使用容易被忽略的反引号字符来继续行。

提供您的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 "\\server\path\file.csv" | ForEach-Object {
    #Check to see if the user already exists in AD
    if ((Get-ADUser -Identity $_.username -ErrorAction SilentlyContinue)) {  
         #If user does exist, give a warning
         Write-Warning "A user account with username $($_.username) already exist in Active Directory."
         continue
    }
    # only store these in variables as they are used in multiple properties
    $firstName = $_.'First Name:'
    $lastName  = $_.'Last Name:'
    # create a Hashtable with all properties you want to set for the new user
    $properties = @{
        'SamAccountName'        = $_.username
        'UserPrincipalName'     = '{0}@domain.com' -f $_.username
        'Name'                  = '{0} {1}' -f $firstName, $lastName
        'GivenName'             = $firstName
        'Surname'               = $lastName
        'Enabled'               = $true
        'DisplayName'           = '{0}, {1}' -f $lastName, $firstName
        'Path'                  = 'OU=CONTRACTORS,OU=ACCOUNTS,OU=organization,DC=domain,DC=lan'
        'City'                  = $_.city
        'Company'               = $_.'Employer:'
        'State'                 = $_.state
        'StreetAddress'         = $_.streetaddress
        'OfficePhone'           = $_.telephone
        'EmailAddress'          = $_.email
        'Title'                 = $_.jobtitle
        'Department'            = $_.department
        'Description'           = $_.'Account Type'
        'AccountPassword'       = (ConvertTo-SecureString $_.password -AsPlainText -Force) 
        'ChangePasswordAtLogon' = $true
        'OtherAttributes'       = @{'extensionAttribute1' = $_.'Submitter Name';'extensionAttribute2'= $_.'Submitter email'}
        # you can comment out any properties you do not need or are missing in the CSV
        # 'PostalCode'          = $_.zipcode
        # 'Country'             = $_.country
    }
    # create the new user using the properties Hashtable (splat)
    New-ADUser @properties
}
© www.soinside.com 2019 - 2024. All rights reserved.