Guest User

Untitled

a guest
Dec 27th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. param( [Parameter(Mandatory=$true)] [String] $TargetHost ,
  2. [Parameter(Mandatory=$true)] [String] $TargetUserName ,
  3. [String] $User ,
  4. [String] $Password)
  5.  
  6. # Set up a trap to properly exit on terminating exceptions
  7. trap [Exception] {
  8. write-error $("TRAPPED: " + $_)
  9. exit 1
  10. }
  11.  
  12. function DeactivateAccount($TargetHost , $TargetUserName ,$User , $Password){
  13.  
  14. $TargetHost = $TargetHost #Target Host on which windows account deactivation will be done.
  15. $TargetUserName = $TargetUserName #User Name of Target.
  16. $Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() #Domain name of the localhost.
  17. $localHost = [System.Net.Dns]::GetHostName()
  18. $localIP = [System.Net.Dns]::GetHostAddresses("$localHost")
  19.  
  20. #if TargetHost and LocalHost are same.
  21. if($localHost -like $TargetHost -OR $localIP -like $TargetHost) {
  22.  
  23. if($Domain -eq [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()){
  24. $process = net user $TargetUsername /domain /active:no #Performs the operation on the domain controller in the computer's primary domain.
  25. } else {
  26. $process = net user $TargetUsername /active:no
  27. }
  28. Write-host " $TargetUsername account deactivated "
  29. }
  30.  
  31. #If TargetHost is remote Host.
  32. else {
  33. $User = $User #Creds to perform admin function.
  34. $Password = $Password
  35. $SecurePassword = new-Object System.Security.SecureString #Convert password into secure string.
  36. $Password.ToCharArray() | % { $SecurePassword.AppendChar($_) }
  37. $Cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist "$User",$securePassword
  38.  
  39. $newSession = New-PSSession -ComputerName "$TargetHost" -credential $Cred #Used PSSession for persistent connection and credentials to Specify a user account that has permission to perform this action.
  40.  
  41. $export_username = Invoke-Command -Session $newSession -ScriptBlock {$username=args[1]} # Invoke-Command command uses the Session parameter(here newSession) to run the commands in same session.
  42. if($Domain -eq [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()){
  43. $process = Invoke-Command -Session $newSession -ScriptBlock {net user $username /domain /active:no}
  44. } else {
  45. $process = Invoke-Command -Session $newSession -ScriptBlock {net user $username /active:no}
  46. }
  47. Write-host " $TargetUsername account deactivated "
  48. Remove-PSSession $newSession # Closes Windows PowerShell sessions.
  49. }
  50.  
  51. if(-not $?) { # Returns true if last command was successful.
  52. Write-Error "Windows Deactivation Failed!!"
  53. exit 1
  54. }
  55. }
  56.  
  57. DeactivateAccount($TargetHost , $TargetUserName ,$User , $Password)
Add Comment
Please, Sign In to add comment