Link to home
Start Free TrialLog in
Avatar of bobby010606
bobby010606Flag for India

asked on

Disable user exchange mailbox from AD using VB.NET

Hi Experts,
     
    I need to disable the exchange mailbox from AD using .NET ...

For this i am getting all the users in a listbox1 ...so when selected a user from the listbox and when i click on the button i need to disable the exchange Mailbox from AD....

For this i added the referances like CDOEXM and CDO

Please find the below code .....i am getting an error like "CDOEXM.IMailRecipient is unable to cast "...

can any one help me is there any better way to do this.....i really appriciate if any one suggest me in different way to disable the Exchange Mailbox from AD
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim UserName As String
            Dim objRecip As CDOEXM.IMailRecipient

            UserName = "CN=" + ListBox1.SelectedItem + "," + oupath

            Dim objuserpath As DirectoryEntry
            objuserpath = New DirectoryEntry("LDAP://" + UserName + "")

            objRecip = DirectCast(objuserpath, CDOEXM.IMailRecipient)
                       objRecip.MailDisable()
            objuserpath.CommitChanges()
        Catch ex As Exception
            MessageBox.Show(ex.Message + " " + ex.StackTrace)
        End Try



    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Hilal1924
Hilal1924
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is a simpler script
I found it at:
http://www.scriptinganswers.com/forum2/forum_posts.asp?TID=2915
Hilal

$users = (c:\old_users.txt)foreach ($user in $users) { Disable-QADUser "$user" }
foreach ($user in (gc c:\old_users.txt)){(Get-QADUser $user).memberOf | Get-QADGroup | where {$_.name -notmatch '^users|domain users}
foreach ($user in $users) { Set-QADUser "$user" -oa @{'msExchHideFromAddressLists'=$True}}
foreach ($user in $users) { Move-QADObject "$user" -NewParentContainer 'domainname.com/disabled_users' }

Open in new window

Avatar of bobby010606

ASKER

soory i forget to mension i am working on exchange server 2003...will the 1st post code work on Exchange server 2003
powershellcommand will not work but the vbscript will.

bol
Hilal
is there any other solution Hilal1924
Yes Of course there are. What exactly do you need aprt from Disabling Mailbox.
 
1. Do you want to disable the user account ?
2. Do you want to delete the account as well ?
3. Do you need to disable this for many users or just a few ?
 
Hilal
i really appriciate about your previous post
i just need to disable Mailbox of the user account.....any .NEt script will work for me Hilal1924

Hilal any luck ...plz help me
Again Sorry for the delay: By Disabling, do you mean just remove the access or delete the mailbox. Removing access would mean that they will not be able to log in via Outlook, Webmail, ActiveSync etc. While as Deleting will delete the entire Mailbox.
 
Hilal
Yaa i just want to remove the access the user should not able to log in via Outlook, Webmail, ActiveSync etc.