powershell - Trying to rename several account types at once based on current displayName -


this morning awesome people helped me make script move user accounts based on displayname ou. tested , worked. cannibalized script make 1 rename same accounts based off of same criteria. i've gone through several errors boils down "i having identity crisis!". can't seem figure out need input $identity. here have:

import-module activedirectory  $renames = @(   @{     filter = 'displayname -like "*supply*"'     newname = "supplies"   },   @{     filter = 'displayname -like "*accountant*"'     newname = "accounting"   } ) | foreach-object {new-object -typename pscustomobject -property $_}  $originou = "ou=test,ou=standard users,ou=domain users,dc=com"  foreach ($rename in $renames) {   get-aduser -searchbase $originou -filter $rename.filter -properties displayname |     where-object {($_.enabled -eq 'true') -and ($_.distinguishedname -notlike '*donttouch*')} |     %{set-aduser $_ -displayname {$_.displayname -replace '(.epsilon ).+',"`$1$rename.newname"}} } 

you can't use current object variable ($_) if have set-aduser read directly pipeline. , since set-aduser apparently doesn't play nice scriptblock arguments, have put statement in loop:

... | % { set-aduser $_ -displayname ($_.displayname -replace '(.epsilon ).+',"`$1$($rename.newname)") } 

note if want expand object properties inside string have put $rename.newname in subexpression ($()), otherwise whole object $rename stringified , string ".newname" appended it.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -