powershell 'Description' field in AD

I am using powershell in Exchange 2010, I am trying to create multiple email addresses at once.  The script below does this but it doesn't populate the 'Description' field within AD.

 Description Field in AD
Import-Module activedirectory
 $password = (Get-Credential).Password

 Import-Csv c:\TLusers.csv | ForEach-Object {
     New-Mailbox -Name $_.Name `
     -FirstName $_.FirstName `
     -LastName $_.LastName `
     -UserPrincipalName $_.UserPrincipalName `
     -Password $password `
     -OrganizationalUnit $_.OU `
     -Database "RS_EXDB1" `
     | Set-ADUser -Company $_.Company -Office $_.Office -Description $_.Description}


I get the following error message:

Set-ADUser : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

csv file and powershell output
Can someone help me with this?
LVL 1
jkimzlgAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Will SzymkowskiSenior Solution ArchitectCommented:
Set-ADUser : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

As you can see in the error message Set-ADUser does not take pipeline input and you are trying to pipe the command to  | Set-ADUser. This will not work.

What you could do is move the Set-ADUser into its own ForEach statement and reference the CSV. See below...

$password = (Get-Credential).Password

$Import = Import-Csv c:\TLusers.csv
ForEach ($user in $import) {
New-Mailbox -Name $user.Name `
     -FirstName $user.FirstName `
     -LastName $user.LastName `
     -UserPrincipalName $user.UserPrincipalName `
     -Password $password `
     -OrganizationalUnit $user.OU `
     -Database "RS_EXDB1" `
}
ForEach ($description in $import) {
$user.UserPrincipalName
$user.descrition
Set-ADUser -Identity $user.UserPrincipalName -Description $user.description
}

Open in new window


Will.
jkimzlgAuthor Commented:
Getting this error message:
Capture.PNG
Will SzymkowskiSenior Solution ArchitectCommented:
When you are creating the mailboxes there might be replication delays also, you are using Set-ADUser cmdlet as well and it might not be picking up the changes because it may be pointing to another domain controller which has not yet received the updated info from the other DC.

For New-Mailbox - DomainController dc1.domain.com

For Set-ADUser -Server dc1.domain.com

If this does not work and the user was actually created what you could do is a start-sleep 15 between the commands which will pause for 15 seconds after the first block has run.

Will.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

jkimzlgAuthor Commented:
Do you mean like this?  Sorry, I'm not a programmer.

ForEach ($description in $import) {
start-sleep 15
$user.UserPrincipalName
$user.descrition
Set-ADUser -Identity $user.UserPrincipalName -Description $user.description
}

I ran this, it creates the users and it does pause but it now gives me this error message:

Set-ADUser : Cannot find an object with identity: 'bsmtih@abc.com' under: DC=abc, DC=com".  At C:\demo2.ps1:18 char:41
Will SzymkowskiSenior Solution ArchitectCommented:
No like below. I have modified the script below...
$password = (Get-Credential).Password

$Import = Import-Csv c:\TLusers.csv
ForEach ($user in $import) {
New-Mailbox -Name $user.Name `
     -FirstName $user.FirstName `
     -LastName $user.LastName `
     -UserPrincipalName $user.UserPrincipalName `
     -Password $password `
     -OrganizationalUnit $user.OU `
     -Database "RS_EXDB1" `
}

start-sleep 15

ForEach ($description in $import) {
$user.UserPrincipalName
$user.descrition
Set-ADUser -Identity $user.UserPrincipalName -Description $user.description
}

Open in new window


You need to make sure that it is actually creating the user and is being replicated to AD aswell. If you account is not being created then this would be why you are running into this issue.

Will.
jkimzlgAuthor Commented:
each time I run the script, the accounts are being created in both AD and Exchange.
Will SzymkowskiSenior Solution ArchitectCommented:
Ahhhhh.... I got a typo in the script. Sorry i have corrected this below...
$password = (Get-Credential).Password

$Import = Import-Csv c:\TLusers.csv
ForEach ($user in $import) {
New-Mailbox -Name $user.Name `
     -FirstName $user.FirstName `
     -LastName $user.LastName `
     -UserPrincipalName $user.UserPrincipalName `
     -Password $password `
     -OrganizationalUnit $user.OU `
     -Database "RS_EXDB1" `
}

start-sleep 15

ForEach ($user in $import) {
$user.UserPrincipalName
$user.descrition
Set-ADUser -Identity $user.UserPrincipalName -Description $user.description
}

Open in new window


That should do it.

Will.
jkimzlgAuthor Commented:
Same error message:

Set-ADUser : Cannot find an object with identity: 'bsmtih@abc.com' under: DC=abc, DC=com".  At C:\demo2.ps1:19 char:41

All users accounts in AD/Exchange were created from running the script
footechCommented:
Set-ADUser does take pipeline input, it's just that in your original code the output from New-Mailbox doesn't match up with the input required by Set-ADUser, and even if it did by the time you're in the Set-ADUser command $_.Company, $_.Office, $_.Description aren't valid.

You will need to provide either a GUID, samAccountName, distinguishedName, or SID to the identity parameter of Set-ADUser.  The UPN won't work, but you can search for a user filtering by UPN, and then pipe that to Set-ADUser.  But as Will mentioned, there might very well be delays between when the New-Mailbox command is run and when you can run Set-ADUser.

Import-Module activedirectory
$password = (Get-Credential).Password

Import-Csv c:\TLusers.csv | ForEach-Object {
    New-Mailbox -Name $_.Name `
        -FirstName $_.FirstName `
        -LastName $_.LastName `
        -UserPrincipalName $_.UserPrincipalName `
        -Password $password `
        -OrganizationalUnit $_.OU `
        -Database "RS_EXDB1"
    $upn = $_.UserPrincipalName
    #Might need to put a Start-Sleep here
    Get-ADUser -Filter {userPrincipalName -eq $upn} | Set-ADUser -Company $_.Company -Office $_.Office -Description $_.Description
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkimzlgAuthor Commented:
it's slow, but it works.  :)  Thank you, u guys are awesome....
jkimzlgAuthor Commented:
Fyi, don't even need the 'sleep-start' command.  works without it...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.