简体   繁体   中英

Powershell setting up a new user with a specific attribute

I have a script that creates new users that can be used for different operations.

Param(
[Parameter(Mandatory=$True,Position=1)]
  [string]$GivenName, #=givenName

[Parameter(Mandatory=$True,Position=2)]
  [string]$Name, # =sn

[Parameter(Mandatory=$True,Position=9)]
  [string]$ADuser, 

[Parameter(Mandatory=$True,Position=4)]
  [string]$Description, #=title


[Parameter(Mandatory=$True,Position=4)]
  [string]$AdministrationUser,

[Parameter(Mandatory=$True,Position=4)]
  [string]$SamAccManager
)

after these parameters I have a new-user -name and so on, But I want to user the last Parameter SamAccManager to be added to the adminDisplayName, so I can search who is in charge of that AD user as there will be users that have no logon rights, will be used only for test purposes.

new-aduser -name $DisplayName -DisplayName $DisplayName -Description $Description -GivenName $GivenName -Surname $Name -SamAccountName $usr -UserPrincipalName $UserPrincipalName -Path $OU_AD

How Can I integrate to add that info into that specific adminDisplayName field? for example, I want to add in the last section code -admindisplayname $samaccmanager, but I can not do that as it is an invalid parameter. Any ideas?

First thing I noticed is that you add duplicate values for Position to the parameters. Also, there is a parameter you do not seem to use: $AdministrationUser and personally, I would change the param names for some of them so it becomes much clearer what they stand for.

The code below uses Splatting to feed the parameters to the New-ADUser cmdlet. This is a nice readable and maintainable way of calling on cmdlets with lots of properties.

Param(
    [Parameter(Mandatory=$True,Position=0)]
    [string]$GivenName,    # =givenName

    [Parameter(Mandatory=$True,Position=1)]
    [string]$SurName,      # =SurName

    [Parameter(Mandatory=$True,Position=2)]
    [string]$AccountName,  # =SamAccountName

    [Parameter(Mandatory=$True,Position=3)]
    [string]$Description,  # =title

    [Parameter(Mandatory=$True,Position=4)]
    [string]$OU,           #= distinguishedName of the OU

    [Parameter(Mandatory=$True,Position=5)]
    [string]$SamAccManager #= AdminDisplayName
)

# create a hashtable for the New-ADUser parameters
$userParams = @{
    Name              = "$GivenName $SurName"
    DisplayName       = "$GivenName $SurName"
    Description       = $Description
    GivenName         = $GivenName
    Surname           = $SurName
    SamAccountName    = $AccountName
    UserPrincipalName = "$AccountName@yourdomain.com"
    Path              = $OU
    # add the AdminDisplayName attribute as hashtable
    OtherAttributes   = @{AdminDisplayName = $SamAccManager}
}

# create the user by splatting the parameters
New-ADUser @userParams

Of course, you can also set the AdminDisplayName property after creating the user by using

Set-ADuser -Identity $AccountName -Add @{AdminDisplayName = $SamAccManager}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM