header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Problems Exporting and creating mailboxes
Last Post 09 Aug 2010 01:54 AM by James. 13 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
JamesUser is Offline
Basic Member
Basic Member
Posts:374
Avatar

--
30 Jul 2010 01:33 AM

    Hello,

    I have the following script:

    Basically its trying to get the previous past days created users which it does fine. Then create them a mailbox which it sort of does (picks and chooses which accounts to do) and write the names out into a file and then do the same for renames however the change with this is it writes a new SMTP Address then writes them to a file. Once its created the two files it emails them. However its only E-Mailing one of them and I dont know why, and its not writing out the information in the text files.

    Does anyone know whats wrong?

    Many Thanks

    James

    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    30 Jul 2010 01:36 AM
    Script Posted:


    #Users Created x Days Ago

    $SchoolList = Import-Csv -Path "C:\Powershell\CSV\MailboxDatabaseswithUPN.csv"
    Get-QADUser -Sizelimit 0 `
                -Title teacher , staff `
                -IncludedProperties Name, dn, samaccountname `
                -CreatedAfter (Get-Date).AddDays(-1) |
    ForEach-Object{
    $DN = $_.DN
    $dn1 = $DN -replace "CN=.*,OU=foo2,OU=",""
    $School = $dn1 -replace ",OU=foo,DC=domain,DC=co,DC=uk",""
    $User = $_.Name

    $SchoolList |
    ForEach-Object {
    if ($School -eq $_.School){
    $Server = $_.ExchangeServer            
    $StorageGroup = $_.StorageGroup            
    $MailboxDatabase = $_.MailboxDatabase
    $Database = $Server + "\" + $StorageGroup + "\" + $MailboxDatabase

    Enable-Mailbox -Identity $User `
                   -Alias $User `
                   -Database $Database
     
    Start-Sleep -Seconds 10
                
    $mailbox = Get-Mailbox -Identity $User            
    $newaddress = $User + "@" + $School + ".domain.co.uk"           
    $mailbox.EmailAddresses += $newaddress

    Set-Mailbox -Identity $mailbox.alias `
                -EmailAddresses $mailbox.EmailAddresses                      
    Set-Mailbox -Identity $mailbox.alias `
                -PrimarySmtpAddress $newaddress -EmailAddressPolicyEnabled $false
    }

    } | Select-Object -Property Name, School |
    Out-File "C:\Scripts\YesterdaysCreate.txt"

    # Users Changed X Days Ago

    $Global:Date = Get-Date -Format d

    Get-QADUser -Sizelimit 0 `
                -Title teacher , staff `
                -LastChangedOn (Get-Date).AddDays(-1) `
                -IncludedProperties Name, dn, whenchanged, samaccountname |
    ForEach-Object{
    $DN = $_.DN
    $dn1 = $DN -replace "CN=.*,OU=foo1,OU=",""
    $School = $dn1 -replace ",OU=foo,DC=domain,DC=sch,DC=uk",""
    $User = $_.Name

    Start-Sleep -Seconds 10
                
    $mailbox = Get-Mailbox -Identity $User            
    $newaddress = $User + "@" + $School + ".domain.co.uk"           
    $mailbox.EmailAddresses += $newaddress

    Set-Mailbox -Identity $mailbox.alias `
                -EmailAddresses $mailbox.EmailAddresses                      
    Set-Mailbox -Identity $mailbox.alias `
                -PrimarySmtpAddress $newaddress -EmailAddressPolicyEnabled $false
    } | Select-Object -Property  Name , School |
    Out-File "C:\Scripts\YesterdaysRenames.txt"

    $File = "C:\Scripts\YesterdaysCreate.txt"
    $File1 = "C:\Scripts\YesterdaysRenames.txt"
    $smtpServer = "smtpserver"

    $msg = New-Object Net.Mail.MailMessage
    $att = New-Object Net.Mail.Attachment($File , $File1)
    $smtp = New-Object Net.Mail.SmtpClient($smtpServer)

    $msg.From = "user@domain.co.uk"
    $msg.To.Add("user@domain.co.uk")
    $msg.Subject = "Latest Teacher And Staff Updates From ADP."
    $msg.Body = "Attatched are the list of Teachers and Staff from Yesterday, Please Setup the E-Mail Accounts As Required."
    $msg.Attachments.Add($att)

    $smtp.Send($msg)

    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    30 Jul 2010 01:58 AM
    Hello,

    I have also just found it seems to be trying to set an SMTP Address for the previous user to the current user does anyone know why this is happening as I cant see why..

    Many Thanks

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    30 Jul 2010 04:04 AM
    Hello,

    I am concentrating on the top part of the code for now to get that working I have modified it to:

    $SchoolList = Import-Csv -Path "C:\Powershell\CSV\MailboxDatabaseswithUPN.csv" Get-QADUser -Sizelimit 0 ` -Title teacher , staff ` -IncludedProperties Name, dn, samaccountname ` -CreatedAfter (Get-Date).AddDays(-1) | ForEach-Object{ $DN = $_.DN $dn1 = $DN -replace "CN=.*,OU=foo1,OU=","" $School = $dn1 -replace ",OU=foo,DC=domain,DC=co,DC=uk","" $User = $_.Name $SchoolList | ForEach-Object { if ($School -eq $_.School){ $Server = $_.ExchangeServer $StorageGroup = $_.StorageGroup $MailboxDatabase = $_.MailboxDatabase $Database = $Server + "\" + $StorageGroup + "\" + $MailboxDatabase Enable-Mailbox -Identity $User ` -Alias $User ` -Database $Database Start-Sleep -Seconds 10 $mailbox = Get-Mailbox -Identity $User $newaddress = $User + "@" + $School + ".domain.co.uk" $mailbox.EmailAddresses += $newaddress Set-Mailbox -Identity $mailbox.alias ` -EmailAddresses $mailbox.EmailAddresses Set-Mailbox -Identity $mailbox.alias ` -PrimarySmtpAddress $newaddress -EmailAddressPolicyEnabled $false } } } | Select-Object @{n='User';e={ $User }} , @{n='School';e={ $School }} | Sort-Object -Property Name, School | Out-File "C:\Scripts\YesterdaysCreate.txt"

    However its still not exporting out the information...

    Does anyone have any ideas?

    Many Thanks

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    02 Aug 2010 12:27 AM
    Does anyone have any ideas on what is happening?

    Many Thanks

    James
    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    03 Aug 2010 02:59 PM

    Looks like the select-object that captures $User and $School is not running at the right time so it's generating no output.

    $SchoolList = Import-Csv -Path "C:\Powershell\CSV\MailboxDatabaseswithUPN.csv" Get-QADUser -Sizelimit 0 ` -Title teacher , staff ` -IncludedProperties Name, dn, samaccountname ` -CreatedAfter (Get-Date).AddDays(-1) | ForEach-Object { $DN = $_.DN $dn1 = $DN -replace "CN=.*,OU=foo1,OU=","" $School = $dn1 -replace ",OU=foo,DC=domain,DC=co,DC=uk","" $User = $_.Name $SchoolList | ForEach-Object { if ($School -eq $_.School) { $Server = $_.ExchangeServer $StorageGroup = $_.StorageGroup $MailboxDatabase = $_.MailboxDatabase $Database = $Server + "\" + $StorageGroup + "\" + $MailboxDatabase Enable-Mailbox -Identity $User ` -Alias $User ` -Database $Database Start-Sleep -Seconds 10 $mailbox = Get-Mailbox -Identity $User $newaddress = $User + "@" + $School + ".domain.co.uk" $mailbox.EmailAddresses += $newaddress Set-Mailbox -Identity $mailbox.alias ` -EmailAddresses $mailbox.EmailAddresses Set-Mailbox -Identity $mailbox.alias ` -PrimarySmtpAddress $newaddress -EmailAddressPolicyEnabled $false } } New-Object PSObject -Property @{ User = $User School = $School } } | Sort-Object -Property Name, School | Out-File "C:\Scripts\YesterdaysCreate.txt"
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    04 Aug 2010 03:50 AM
    Hello,

    Many Thanks for that it seems to have worked a treat!

    Many Thanks Once again

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    04 Aug 2010 04:27 AM
    Hello,

    I have made a slight change to my code:

    #Users Created x Days Ago $ErrorActionPreference = "SilentlyContinue" $Global:SchoolList = Import-Csv -Path "C:\Powershell\CSV\MailboxDatabaseswithUPN.csv" Get-QADUser -Sizelimit 0 ` -Title teacher , staff ` -IncludedProperties Name, dn, samaccountname ` -CreatedAfter (Get-Date).AddDays(-1) | ForEach-Object { $DN = $_.DN $dn1 = $DN -replace "CN=.*,OU=foo3,OU=","" $School = $dn1 -replace ",OU=foo1,DC=foo,DC=co,DC=uk","" $User = $_.Name $SchoolList | ForEach-Object { if ($School -eq $_.School){ $Server = $_.ExchangeServer $StorageGroup = $_.StorageGroup $MailboxDatabase = $_.MailboxDatabase $Database = $Server + "\" + $StorageGroup + "\" + $MailboxDatabase Enable-Mailbox -Identity $User ` -Alias $User ` -Database $Database do{ $Global:mailbox = Get-Mailbox -Identity $User } until($mailbox.RecipientType -eq "UserMailbox") $User1 = $User -replace "\d","" $newaddress = $User1 + "@" + $School + ".foo.co.uk" $mailbox.EmailAddresses += $newaddress Set-Mailbox -Identity $mailbox.alias ` -EmailAddresses $mailbox.EmailAddresses Set-Mailbox -Identity $mailbox.alias ` -PrimarySmtpAddress $newaddress -EmailAddressPolicyEnabled $false } } } | Select-Object @{n='School';e={ $School }}, @{n='User';e={ $User }} | Sort-Object -Property School , User | Out-File "C:\Scripts\YesterdaysCreate.txt" # Users Changed X Days Ago $Global:Date = Get-Date -Format d Get-QADUser -Sizelimit 0 ` -Title teacher , staff ` -LastChangedOn (Get-Date).AddDays(-1) ` -IncludedProperties Name, dn, samaccountname | ForEach-Object{ $rDN = $_.DN $rdn1 = $rDN -replace "CN=.*,OU=foo2,OU=","" $RenameSchool = $rdn1 -replace ",OU=foo1,DC=foo,DC=co,DC=uk","" $RenameUser = $_.Name $Global:renamemailbox = Get-Mailbox -Identity $RenameUser $PriSMTP = $mailbox.PrimarySMTPAddress $RenameUser1 = $RenameUser -replace "\d","" $Rnewaddress = $RenameUser1 + "@" + $RenameSchool + ".foo.co.uk" $renamemailbox.EmailAddresses += $Rnewaddress Set-Mailbox -Identity $renamemailbox.alias ` -EmailAddresses $renamemailbox.EmailAddresses Set-Mailbox -Identity $renamemailbox.alias ` -PrimarySmtpAddress $Rnewaddress -EmailAddressPolicyEnabled $false If($PriSMTP -ne $Rnewaddress){ New-Object PSObject -Property @{ RenamedUser = $RenameUser RSchool = $RenameSchool } | Sort-Object -Property RSchool , RenamedUser | Out-File "C:\Scripts\YesterdaysRenames.txt" } } $File = "C:\Scripts\YesterdaysCreate.txt" $File1 = "C:\Scripts\YesterdaysRenames.txt" $smtpServer = "Serverinfo here" $msg = New-Object Net.Mail.MailMessage $att = New-Object Net.Mail.Attachment($File) $att1 = New-Object Net.Mail.Attachment ($File1) $smtp = New-Object Net.Mail.SmtpClient($smtpServer) $msg.From = "mailhere" $msg.To.Add("mailhere") $msg.Subject = "Latest Teacher And Staff Updates From ADP." $msg.Body = "Attatched are the list of Teachers and Staff from Yesterday, Please Setup the E-Mail Accounts As Required." $msg.Attachments.Add($att) $msg.Attachments.Add($att1) $smtp.Send($msg)

    We have found that its pulling everything changed on so were after actual Renamed people so they would require a new SMTP Address and then just pull them one out. So I am getting the primary SMTP Address before making changes and then setting a new one (if it errors then no changes are made) and then I am comparing the new SMTP Address against the old one (grabbed before changes) and if they are diffrent write it out and if not then dont bother.

    Now the code works however if it runs and the file is in place it does not replace the file for some reason does anyone know why?

    Many Thanks

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    04 Aug 2010 07:31 AM
    Oh and if it finds nothing then it just create a blank file so it overwrites the file...

    So would I have to change the If Statement or is it all just wrong?

    Many Thanks

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    05 Aug 2010 01:14 AM
    Hello,

    It does not appear to be working as I had first planned as it only seems to be outputting one person however that one person does not have more than 1 SMTP Address so I am not too sure if its working does anyone know what I can do to make it work?

    Many Thanks

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    05 Aug 2010 03:01 AM
    Hello,

    I have found there was someone else in the directory which was using it so its fine so it qualified.

    However it is only pulling 1 result which I think is correct.

    I will post back if it is overwriting each time but if someone can see any issues with the code above please let me know :) and I would be most appriciative.

    Many Thanks

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    06 Aug 2010 12:56 AM
    Hello,

    I have left it to test and I got the results I expected out of the New Users part except it duplicates the information. The renames is only ever pulling one person when I know there are more. So it must be overwriting each time however I dont know why.

    Does anyone have any idea's on this?

    Many Thanks

    james
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    06 Aug 2010 02:47 AM
    Ok I have sorted out the issue with the duplication however I cant see any issue with the overwriting...

    Can anyone shed any light on this?

    Many Thanks

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    09 Aug 2010 01:54 AM
    Does anyone have any idea's on this its driving me nuts!

    Any help greatly appreciated.

    James
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer