更改组中用户的AD用户属性

时间:2015-03-19 08:40:12

标签: powershell

我复制并粘贴了一个查看某个组中所有成员的脚本,然后更改了所有公司名称。我现在想扩展这个脚本,以便能够询问您想要更改的用户的哪个属性。

Clear-Host

$GPNAME = Read-Host 'Provide the Group Name here'
#$whatwhat = Read-Host 'Which Attribute do you want to change | Put a - infront'
$attrib = Read-Host 'Give new company name here' #This should later change to "Give new info of Attribute you want to change"

$description = "-description"
$offeece = "-office"
$department = "-department"
$company = "-company"

$Userslist = Get-ADGroupMember $GPNAME

ForEach($User in $Userslist)
{
$user1 = $User.name
#Write-Host $user1
get-aduser -filter { name -eq $User1 } | set-aduser -company $attrib
}

在脚本的最后我想用$ whatwhat更改-company。 我已经尝试过,但无论如何,或者 - 或者没有,和#39;和"在 - 或者什么,它继续给我这个错误。

Set-ADUser : A positional parameter cannot be found that accepts argument 'newly'

任何人都可以通过向用户提供可供选择的选项或允许他/她输入:公司或描述或办公室来协助我扩展此脚本

1 个答案:

答案 0 :(得分:1)

您可以使用invoke-expression cmdlet

$cmd="get-aduser -filter { name -eq $User1 } | set-aduser $whatwhat $attrib -whatif"
invoke-expression $cmd   

测试:

PS>$what=read-host "attribute to change :"  attribute to change ::
company              
PS>$c="set-aduser test -$what 'testcpny' -whatif"         
PS>iex $c
WhatIf : Opération « Set » en cours sur la cible « CN=TEST,CN=Users,DC=....,DC=com ».