I'm trying to find a way of creating a filter on the username. Basically the exchange system that we use has multiple databases and we assign a user mailbox on to it by their first letter of their surname.
Now I have the commands to create a mailbox through PowerShell and in to the right database, but I'd like to be able to do this through a CSV file, but I do not know how I can get PowerShell to just look at the first character of the users surname so I can do some if
statement filtering on it.
So if a surname is Blogs, I want PowerShell to read out the B and then put it in a variable so I can work with it.
Any ideas?
After amending the script as supplied to me by CB, I now have a working PowerShell script for it. The thing that threw me for a while was the fact that the header in a CSV file throws an error unless you add your header in to the variable.
The error I kept getting was "Cannot process argument transformation on parameter 'Identity'. Cannot convert the "@{user=Blogs}" value of type "Deserialized.System.Management.Automation.PSCustomObject" to type "Microsoft.Exchange.Configuration.Tasks.UserIdParameter".
The resolution to this is instead of using: foreach ($user in $list)
you will need to use foreach ($list.user in list)
$list = import-csv c:\Operational.csv
$db = [string]::Empty
foreach ($list.user in $list)
{
switch -regex ($list.user) # I used a regex to find starting
# char of user and assign to $db
# variable the name of the
# mailboxdatabase
{
"^[a-d]" { $db = "Mailstore 501 (Operational A-D)" }
"^[e-j]" { $db = "Mailstore 502 (Operational E-J)" }
"^[k-p]" { $db = "Mailstore 503 (Operational K-P)" }
default { $db = "Mailstore 504 (Operational Q-Z)" }
}
#$username = $list.user Used for debugging
$identity = get-user $list.user
#$Database = $db Used for debugging
Enable-mailbox -Identity $identity -Alias $list.user -Database $db
#Echo "User : $username" Used for debugging
#Echo "Identity: $identity" Used for debugging
#Echo "Database : $db" Used for debugging
}
I've used this time ago (you have to edit based on your need)
$list = gc c:\UserList.txt
$db = [string]::Empty
foreach ($user in $list)
{
switch -regex ($user) # I used a regex to find starting char of user and assign to $db variable the name of the mailboxdatabase
{
'^[a-c]' { $db = "A-C" }
'^[d-l]' { $db = "D-L" }
'^[m-p]' { $db = "M-P" }
default { $db = "Q-Z" }
}
New-MoveRequest -Identity $user -TargetDatabase $db # I used a move but you can do whatever you want
}