Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try
- {
- if (Get-Module -ListAvailable -Name ActiveDirectory) {
- Write-Host "Module exists"
- } else {
- Write-Host "Module does not exist"
- get-module -listavailable
- Import-Module ServerManager
- Add-WindowsFeature RSAT-AD-PowerShell
- }
- $erroractionpreference = 'Stop'
- import-module ActiveDirectory
- $domianUsername = 'Set_Led@geemoneymgmt.com'
- $domainPassword = 'Qwerty_123'
- $domainSecurePassword = convertto-securestring $domainPassword –AsPlainText –Force
- $domainCread = new-object -Typename System.Management.Automation.PSCredential -ArgumentList $domianUsername, $domainSecurePassword
- $trustedLdapSession = new-pssession -ComputerName 'RGEE-DC2.corp.geemoneymgmt.com' -Credential $domainCread -ErrorAction 'Stop';
- $foundDomain = invoke-command $trustedLdapSession -Scriptblock{
- return get-addomain -Identity 'corp.geemoneymgmt.com'
- }
- if(!$foundDomain)
- {
- write-error 'Incorrect domain name.' -ErrorAction 'Stop'
- };
- <#
- Domain: corp.geemoneymgmt.com
- DomainController: RGEE-DC2 ? RGEE-DC2.corp.geemoneymgmt.com
- DomainUsername: Set_Led@geemoneymgmt.com
- Password Qwerty_123
- #>
- }
- catch [System.Management.Automation.Remoting.PSRemotingTransportException]
- {
- if($_.Exception.ErrorCode)
- {
- if($_.Exception.ErrorCode -eq 53)
- {
- throw [System.Management.Automation.RuntimeException] 'Incorrect server name.'
- }
- elseif($_.Exception.ErrorCode -eq 1326)
- {
- throw [System.Management.Automation.RuntimeException] 'Incorrect username or password.'
- }
- elseif($_.Exception.ErrorCode -eq 5)
- {
- throw [System.Management.Automation.RuntimeException] 'Access is denied.'
- }
- else
- {
- throw [System.Management.Automation.RuntimeException] ('There was an error connecting to the server.|' + $_.Exception.Message)
- }
- }
- else
- {
- throw [System.Management.Automation.RuntimeException] ('There was an error connecting to the server.|' + $_.Exception.Message)
- }
- }
- catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
- {
- throw [System.Management.Automation.RuntimeException] $_.Exception.Message
- }
- catch [Microsoft.PowerShell.Commands.WriteErrorException]
- {
- throw [System.Management.Automation.RuntimeException] $_.Exception.Message
- }
- catch [System.Management.Automation.ApplicationFailedException]
- {
- throw [System.Management.Automation.ApplicationFailedException] $_.Exception.Message
- }
- catch [System.Management.Automation.RemoteException]
- {
- throw [System.Management.Automation.RuntimeException] $_.Exception.Message
- }
- catch
- {
- throw [System.Management.Automation.RuntimeException] ('There was an error processing your request. Please contact support.|' + $_.Exception.Message)
- }
- finally
- {
- $erroractionpreference = 'SilentlyContinue'
- remove-pssession $exchangeSession
- $erroractionpreference = 'Stop'
- }
Add Comment
Please, Sign In to add comment