![]() |
|
|
|||||||
| Petri.co.il is happy to award auglan the title of Most Valuable Member !!! |
| Register | Calendar |
Search |
Today's Posts |
Mark Forums Read |
| Notices |
|
|
Disable or Enable user account via powershell scriptthis thread has 8 replies and has been viewed 40730 times
|
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
#1
|
||||||||
|
||||||||
|
Hi,
Looking for a script that enable or disable a user account via PowerShell v1 the script should be given 2 parameters username and state (enable/disable) so far I have a little code that will search for the user in ldap Code:
function get-dn ($SAMName)
{
$root = [ADSI]''
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.filter = "(&(objectClass=user)(sAMAccountName= $SAMName))"
$user = $searcher.findall()
if ($user.count -gt 1)
{
$count = 0
foreach($i in $user)
{
write-host $count ": " $i.path
$count = $count + 1
}
$selection = Read-Host "Please select item: "
return $user[$selection].path
}
else
{
return $user[0].path
}
}
$Name = $args[0]
$path = get-dn $Name
"'" + $path + "'"
|
|
#2
|
||||||||
|
||||||||
|
figured it out
here is the code: Code:
function get-dn ($SAMName)
{
$root = [ADSI]''
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.filter = "(&(objectClass=user)(sAMAccountName= $SAMName))"
$user = $searcher.findall()
if ($user.count -gt 1)
{
$count = 0
foreach($i in $user)
{
write-host $count ": " $i.path
$count = $count + 1
}
$selection = Read-Host "Please select item: "
return $user[$selection].path
}
else
{
return $user[0].path
}
}
$Name = $args[0]
$status = $args[1]
$path = get-dn $Name
"'" + $path + "'"
if ($status -match "enable")
{
# Enable the account
$account=[ADSI]$path
$account.psbase.invokeset("AccountDisabled", "False")
$account.setinfo()
}
else
{
# Disable the account
$account=[ADSI]$path
$account.psbase.invokeset("AccountDisabled", "True")
$account.setinfo()
}
|
|
#3
|
||||||||||
|
||||||||||
|
Thanks for posting the corrected code.
__________________
Server 2000 MCP Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com ** Remember to give credit where credit is due and leave reputation points |
|
#4
|
||||||||
|
||||||||
|
I played around with this a little. I added some minor error checking in case no users are found, and added automatically hiding/showing in the Exchange Global Address List. Save it as Disable-User.ps1
Code:
# http://www.petri.co.il/forums/showthread.php?p=109975
# usage: Disable-User [accountname] [enable/disable]
function get-dn ($SAMName) {
$root = [ADSI]''
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.filter = "(&(objectClass=user)(sAMAccountName= $SAMName))"
$user = $searcher.findall()
if ($user.count -gt 1) {
$count = 0
foreach($i in $user) {
write-host $count ": " $i.path
$count = $count + 1
}
$selection = Read-Host "Please select item: "
return $user[$selection].path
} else {
return $user[0].path
}
}
$Name = $args[0]
$status = $args[1]
$path = get-dn $Name
if ($path -ne $null) {
"'" + $path + "'"
if ($status -match "enable") {
# Enable the account
$account=[ADSI]$path
$account.psbase.invokeset("AccountDisabled", "False")
$account.setinfo()
Set-Mailbox "$Name" -HiddenFromAddressListsEnabled $False
} else {
# Disable the account
$account=[ADSI]$path
$account.psbase.invokeset("AccountDisabled", "True")
$account.setinfo()
Set-Mailbox "$Name" -HiddenFromAddressListsEnabled $True
}
} else {
write-host "No user account found!" -foregroundcolor white -backgroundcolor red
}
Quote:
__________________
Pat Richard Exchange MVP contributing author "Microsoft Exchange Server 2007: The Complete Reference" |
|
#5
|
||||||||||
|
||||||||||
|
Much respect Pat
__________________
cheers Andy Please read this before you post: http://support.microsoft.com/kb/555375 Quis custodiet ipsos custodes? |
|
#6
|
||||||||
|
||||||||
|
I'd be willing to add more if anyone has requirements.
__________________
Pat Richard Exchange MVP contributing author "Microsoft Exchange Server 2007: The Complete Reference" |
|
#7
|
||||||||
|
||||||||
|
This is exactly what i have been looking for. Is there any way to get it to read from a file? I have a list of about 40 users that i need to disable, and hide from Exchange in one fell swoop if possible. Having a script to do that would be ideal!!!
Thanks much app |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Enable User in Live Communications Server Fails (Was:enable lcp user failed) | h7h | Windows Server 2000 / 2003 | 1 | 1st May 2007 12:07 |
| I script was made by on of our administrator to disable any computer account | noway | Active Directory | 3 | 18th July 2006 12:16 |
| Disable Active Directory user account | Lucide | General Scripting | 2 | 17th November 2005 16:30 |
| Problems w/ unlock user account script in Active Directory | Shane | General Scripting | 1 | 25th July 2005 23:29 |
| Disable.Group.User.Account | azmantek | Active Directory | 3 | 28th October 2004 19:52 |