INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Create user Mailbox script no working

Create user Mailbox script no working

(OP)
Hi All,

Thanks in advance for any help given, I have recently taken over a new job role from a collegue who has a powershell script that create mailboxes from a csv file and for some unknown reason has stopped working, I am new to windows powershell and I was wondering if anyone had any ideas on what could be wrong

Here is the script as it stands at the moment

# create new email account for existing student AD user
# Can only handle 1 csv file in directory at once
# multiple csv files will result in command failure
#
#
# Workout what the date is

$mmdd = get-date -format %M%d
$File = "C:\newusers\logs\newstudent_$mmdd.log"

copy c:\newusers\logs\log.txt c:\newusers\logs\newstudent_$mmdd.log

#import the CSV file and add the user to the correct database
import-csv "c:\newusers\newstudent_*.csv" | foreach-object {

$userAlias = "$($_.username)".TrimEnd()


#work out which student DB to put the user in.
if ($userAlias -match '[0-1]$')
{$dbPart2 = "0-1"}

if ($userAlias -match '[2-3]$')
{$dbPart2 = "2-3"}

if ($userAlias -match '[4-5]$')
{$dbPart2 = "4-5"}

if ($userAlias -match '[6-7]$')
{$dbPart2 = "6-7"}

if ($userAlias -match '[8-9]$')
{$dbPart2 = "8-9"}

#error handling
trap {
"ERROR: " + $_.exception.message >> c:\newusers\logs\errorlog_$mmdd.log
exit
}

# enable the mailbox
Enable-Mailbox -Identity "$userAlias" -Database "servername\SG Student $dbpart2\MBX Student $dbPart2" -ErrorAction Stop | out-File $File -append

#start-sleep -seconds .25

#$value="CN=Students,CN=All Address Lists,CN=Address Lists Container,CN=South Devon College,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=sdc,DC=internal"

#$user = $NULL

#$User = Get-User -identity $userAlias

#$userDN=$user.distinguishedname

#$student = [ADSI]"LDAP://$userDN"
#$student.psbase.InvokeSet('msExchQueryBaseDN',$value)
#$student.setinfo()


}


#Clean up the variables
$dbPart2 = $NULL
$userAlias = $NULL
#$users = $NULL
#$value = $NULL
#$student = $NULL
$File = $NULL

#move the CSV to the archive
copy "c:\newusers\newstudent_*.csv" "c:\newusers\archive\"
Del "c:\newusers\newstudent_*.csv"



Any help would be most appreciated

Many Thanks

Diehippy

RE: Create user Mailbox script no working

What error message(s) do you get?http://www.tek-tips.com/


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Create user Mailbox script no working

Sorry about the link on the end of the post. I hate it when that happens.


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Create user Mailbox script no working

(OP)
Hi Thanks for replying,

I need to give you some more details, this script runs from the following batch file that is scheduled to run overnight on a microsoft windows server 2008 r2 with exchange 2007

batch file reads

"c:\windows\system32\windowspowershell\v1.0\powershell.exe" -psconsolefile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command "& {c:\newusers\scripts\mailenablenewstudentuser.ps1}"

I don't get any error message and the csv file moves across fine to the archive , but I have run it manually and I get the following error

Enable-mailbox -identity <<<< "$userAlias" - database "servername\sgstudent $dbpart2\MBX Student $dbpart2 : out-file $file -append
categoryinfo : invalidArgument: (:) [enable-mailbox1, parameterBindingException
FullyQualifiedErrorId : CannotCovertArgumentNoMessage,Microsoft.Exchange.Management.RecipientTasks.EnableMailbox

Many Thanks Diehippy

RE: Create user Mailbox script no working

I'm still pretty new at Powershell, myself, but 1 thing I would try is to trim both front and back of the username not just the end. Also I don't know what the first $ is for in the second half of that command -- "$($_.username)".TrimEnd().


Your command:
$userAlias = "$($_.username)".TrimEnd()

I would try this:
[string]$userAlias = $_.username
$userAlias = $userAlias.trim()


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Create user Mailbox script no working

Well first off I would state that you need to make sure that your powershell script is first loading the Exchange modules or run the script from the exchange management shell. Yes they are both powershell and you can open powershell and run ps1's from the mailserver (if you're on the mailserver console/RDP) but it doesn't load the Exchange Powershell modules automatically unless you configure it to. The Exchange Management Shell will however automatically load the modules. You need those modules for the Enable-Mailbox command to become available.
To add to ps script from regular ps
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

Windows Haiku:

Serious error.
All shortcuts have disappeared.
Screen. Mind. Both are blank.

RE: Create user Mailbox script no working

w33mhz has a good point. Making sure you're running the correct snap-ins, especially for batch files, is always something to keep in mind.

I would think, however, if you're running without the proper snap-ins loaded it would give some type of an unknown cmdlet error.

I'm not that proficient with PowerShell, so I could be mistaken.

Here's another suggestion: change the enable-mailbox command.

Your Current Command

CODE

Enable-Mailbox -Identity "$userAlias" -Database "servername\SG Student $dbpart2\MBX Student $dbPart2" -ErrorAction Stop | out-File $File -append 

1. Remove the quotation marks around $userAlias
2. Set the database to a variable before using it in the command

Result

CODE

$db_name = "servername\SG Student $dbpart2\MBX Student $dbPart2"
Enable-Mailbox -Identity $userAlias -Database $db_name -ErrorAction Stop | out-File $File -append 

3. Add debug lines to out-put the variables so you know what it's trying to do. You could even comment out the enable-mailbox command, the copy/delete lines pertaining to the csv file at the end of the script so that you can run this multiple times if it still errors.

CODE

[string]$userAlias = $_.username
$userAlias = $userAlias.trim()
.
.
.
$db_name = "servername\SG Student $dbpart2\MBX Student $dbPart2"
write-host "Alias:$userAlias"
write-host "DB:$db_name"
#Enable-Mailbox -Identity $userAlias -Database $db_name -ErrorAction Stop | out-File $File -append
.
.
.
#move the CSV to the archive
# copy "c:\newusers\newstudent_*.csv" "c:\newusers\archive\"
# Del "c:\newusers\newstudent_*.csv" 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Create user Mailbox script no working

diehippy,
Did you get this resolved? I'm curious to know what was causing the issue.


Light travels faster than sound. That's why some people appear bright until you hear them speak.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close