Wednesday, June 20, 2018

How To Connect To Active Directory PowerShell


Overview

In this post / guide we will demonstrate, listing files on a remote server’s in a specific directory.
  • If the file doesn’t exist a message will be displayed.
  • If the user doesn’t key any file, an error message will be displayed.

Applies To

Tested on Windows 10, Windows 2008 R2, Windows 2012.

Pre-Requisites

To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or “Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.

Policy Purpose
Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned Only scripts signed by a trusted publisher can be run.
RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted No restrictions; all Windows PowerShell scripts can be run.

Current Execution Policy

To know the current run the PowerShell cmdlet; Get-ExecutionPolicy

To list execution policies that can be configured run the PowerShell cmdlet; Get-ExecutionPolicy -List



PowerShell Script – Connect AD

In this PowerShell script we will demonstrate only AD connection and list of user, when credentials is issued and connection to AD is successful, “administrator” account attributes will be listed.

Code Snippet – Connect to AD

This code snippet is for demonstrating “Connecting to Active Directory ”.

Powershell Code Snippet – Connect to AD


Clear-Host
Set-Location $env:USERPROFILE
Push-Location $env:USERPROFILE
#
# Import Active Directory Module
#
Import-Module ActiveDirectory
#
# Set PS Provider - Active Directory 
#
$ADConnection=New-PSDrive -Name ADDrive -PSProvider ActiveDirectory -Root "DC=ftech,DC=com" -Server f1ind.ftech.com:389 -Credential "ftech\vcp.muthukrishna" -ErrorAction Ignore
if ($ADConnection.Name -eq 'ADDrive') {
    if ($ADConnection.Name -eq 'ADDrive') {
        [System.Console]::Beep(600,500)
        Write-host "Connected to Active Directory"  -BackgroundColor Black -ForegroundColor Green 
        }
    Set-Location ADDrive:
    Get-ADuser administrator
    Set-Location c:\
    Push-Location c:\
    Remove-PSDrive -Name ADDrive
} else {
    Write-host "`nUnable to Connect to Active Directory" -BackgroundColor Black -ForegroundColor Red
    [System.Console]::Beep(600,500)
}

PowerShell Execution – Connect Active Directory (AD)

When script is executed; the script will ask for credentials and connect to Active Directory is established when the user’s credentials are correct.

This script will only list the user after connecting to Active Directory and list user’s attributes, however this script will not check or validate if the user exists or not.


PowerShell Output – Key-In Authentication Information

Key-In the username and password, who has access to active directory.



PowerShell Output – List AD User Attribute

When script is executed; once successfully establishing the connection with Active Directory, script will query and list active directory user’s attributes are listed.



PowerShell Output – List AD User Attribute

In this output the active directory user’s attributes are listed and the user belongs to a different Organization Unit (OU). In this example in the script we have modified the user to be connected and queried.




SlideShare Information

A step by step guide with screenshots is uploaded to slideshare.




No comments:

Post a Comment