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-ExecutionPolicyTo list execution policies that can be configured run the PowerShell cmdlet; Get-ExecutionPolicy -List
Image here...
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: $GetUserResult=(Get-ADuser -Filter {SamAccountName -eq "administrator"}) if ($GetUserResult -eq $Null) { Write-Host "User does not exists!" -BackgroundColor Black -ForegroundColor Red [System.Console]::Beep(600,500) Set-Location c:\ Push-Location c:\ Remove-PSDrive -Name ADDrive } else { $GetUserResult 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 list the user after connecting to Active Directory and list user’s attributes, this script will also 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.Image here...
No comments:
Post a Comment