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
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