Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Exchange Script running against all, not input

$
0
0

I haven’t worked with the Exchange PowerShell cmdlets before, so I don’t know if all the syntax in this answer will be right. There’s a lot going wrong in the code you posted from a general PowerShell standpoint, though:

  • $name is not a string. It’s an object representing a single row of your CSV file. If your posted contents of the CSV are accurate, the object should have a propery named “userid” that contains the data you want. In the code posted below, I’ve renamed this variable to $object (to avoid confusion), and replaced references to $name with $object.userid .
  • You should only make one call to Get-Mailbox, and store its result in a variable. Piping the results to Select-Object is unnecessary, in this case, and piping them to Format-Table (“ft” in the code you posted) is wrong. Whenever you use one of the Format-* cmdlets, you’ve just gone from using an object to displaying a text representation of that object on screen or in a file.
  • I’m not sure what you’re trying to accomplish in the foreach ($box in $addr) loop, but the syntax is definitely wrong. You can’t start a pipeline with Where-Object (alias “?” in the code above); something has to be piped into it.

Here’s a start at revising the code. Since I’m not sure what you intend to do with the user’s mailbox once you’ve found it, I’ve ignored all of that code for now:

$users=Import-Csv c:\temp\testuser.csv

foreach ($object in $users) {
    $mailbox = Get-Mailbox -Identity $object.userid

    # You can access $mailbox.EmailAddresses and $mailbox.PrimarySmtpAddress here,
    # in place of the $addr and $primary variables you were using.

    # Do something with the mailbox (not sure what you are trying to do with it yet).
}

Viewing all articles
Browse latest Browse all 13067

Trending Articles