简体   繁体   English

Powershell 设置具有特定属性的新用户

[英]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.在这些参数之后我有一个新用户名等等,但我想使用最后一个参数 SamAccManager 添加到 adminDisplayName,所以我可以搜索谁负责那个 AD 用户,因为会有用户拥有没有登录权限,仅用于测试目的。

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?如何集成以将该信息添加到特定的 adminDisplayName 字段中? 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.例如,我想在最后一段代码中添加 -admindisplayname $samaccmanager,但我不能这样做,因为它是无效参数。 Any ideas?有任何想法吗?

First thing I noticed is that you add duplicate values for Position to the parameters.我注意到的第一件事是您将Position的重复值添加到参数中。 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.此外,还有一个您似乎没有使用的参数: $AdministrationUser并且就个人而言,我会更改其中一些参数的名称,以便更清楚它们代表什么。

The code below uses Splatting to feed the parameters to the New-ADUser cmdlet.下面的代码使用Splatting将参数提供给 New-ADUser cmdlet。 This is a nice readable and maintainable way of calling on cmdlets with lots of properties.这是调用具有许多属性的 cmdlet 的一种很好的可读性和可维护性。

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当然,你也可以在创建用户后设置 AdminDisplayName 属性,使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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