Guest User

Untitled

a guest
Jan 12th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. try
  2. {
  3. if (Get-Module -ListAvailable -Name ActiveDirectory) {
  4. Write-Host "Module exists"
  5. } else {
  6. Write-Host "Module does not exist"
  7. get-module -listavailable
  8. Import-Module ServerManager
  9. Add-WindowsFeature RSAT-AD-PowerShell
  10. }
  11. $erroractionpreference = 'Stop'
  12.  
  13. import-module ActiveDirectory
  14.  
  15. $domianUsername = 'Set_Led@geemoneymgmt.com'
  16. $domainPassword = 'Qwerty_123'
  17. $domainSecurePassword = convertto-securestring $domainPassword –AsPlainText –Force
  18. $domainCread = new-object -Typename System.Management.Automation.PSCredential -ArgumentList $domianUsername, $domainSecurePassword
  19. $trustedLdapSession = new-pssession -ComputerName 'RGEE-DC2.corp.geemoneymgmt.com' -Credential $domainCread -ErrorAction 'Stop';
  20.  
  21. $foundDomain = invoke-command $trustedLdapSession -Scriptblock{
  22. return get-addomain -Identity 'corp.geemoneymgmt.com'
  23. }
  24. if(!$foundDomain)
  25. {
  26. write-error 'Incorrect domain name.' -ErrorAction 'Stop'
  27. };
  28.  
  29. <#
  30. Domain: corp.geemoneymgmt.com
  31. DomainController: RGEE-DC2 ? RGEE-DC2.corp.geemoneymgmt.com
  32. DomainUsername: Set_Led@geemoneymgmt.com
  33. Password Qwerty_123
  34. #>
  35. }
  36. catch [System.Management.Automation.Remoting.PSRemotingTransportException]
  37. {
  38. if($_.Exception.ErrorCode)
  39. {
  40. if($_.Exception.ErrorCode -eq 53)
  41. {
  42. throw [System.Management.Automation.RuntimeException] 'Incorrect server name.'
  43. }
  44. elseif($_.Exception.ErrorCode -eq 1326)
  45. {
  46. throw [System.Management.Automation.RuntimeException] 'Incorrect username or password.'
  47. }
  48. elseif($_.Exception.ErrorCode -eq 5)
  49. {
  50. throw [System.Management.Automation.RuntimeException] 'Access is denied.'
  51. }
  52. else
  53. {
  54. throw [System.Management.Automation.RuntimeException] ('There was an error connecting to the server.|' + $_.Exception.Message)
  55. }
  56. }
  57. else
  58. {
  59. throw [System.Management.Automation.RuntimeException] ('There was an error connecting to the server.|' + $_.Exception.Message)
  60. }
  61. }
  62. catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
  63. {
  64. throw [System.Management.Automation.RuntimeException] $_.Exception.Message
  65. }
  66. catch [Microsoft.PowerShell.Commands.WriteErrorException]
  67. {
  68. throw [System.Management.Automation.RuntimeException] $_.Exception.Message
  69. }
  70. catch [System.Management.Automation.ApplicationFailedException]
  71. {
  72. throw [System.Management.Automation.ApplicationFailedException] $_.Exception.Message
  73. }
  74. catch [System.Management.Automation.RemoteException]
  75. {
  76. throw [System.Management.Automation.RuntimeException] $_.Exception.Message
  77. }
  78. catch
  79. {
  80. throw [System.Management.Automation.RuntimeException] ('There was an error processing your request. Please contact support.|' + $_.Exception.Message)
  81. }
  82. finally
  83. {
  84. $erroractionpreference = 'SilentlyContinue'
  85. remove-pssession $exchangeSession
  86. $erroractionpreference = 'Stop'
  87. }
Add Comment
Please, Sign In to add comment