Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create user Mailbox script no working

Status
Not open for further replies.

diehippy

Technical User
Jul 4, 2007
46
GB
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
 
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.
 
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
 
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.
 
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.
 
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 [b][COLOR=#EF2929]"[/color][/b]$userAlias[b][COLOR=#EF2929]"[/color][/b] -Database [b][COLOR=#EF2929]"servername\SG Student $dbpart2\MBX Student $dbPart2"[/b][/color] -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.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top