Date post: | 11-Apr-2017 |
Category: |
Documents |
Upload: | fahad-noaman |
View: | 259 times |
Download: | 20 times |
Welcome to PowerShell Training
Name : Pardha Sai (Pardhu)Role : Automation Engineer
Course Duration : 10 Hours
Agenda
• What do you know about PowerShell ?
• How Powershell help’s IT ?
• Little History of Powershell
• Variables
• Data Types
• Conditions
• Loops
• What is command let ?
• How to find Cmdlets in PowerShell ?
• 3 important Cmdlets
• What is alias ?
• What is Pipeline and use of where and select object ?
• What is PowerShell profile and how it is used ?
• What is WMI Object ?
Agenda
• What is PowerShell Remoting and How we can remote ?
• What is function ?
• What is module ?
• How to find and use Modules ?
• What is RSAT ?
• Client VS Server RSAT ?
• Getting Started with AD PS Module ?
• AD Module Version History
• How to import & export CSV data in PS ?
• How to Create Bulk User’s by using CSV File ?
• How to copy a user across AD?
• What is Execution Policy ?
• How to build PowerShell Script ?
What do you know about PowerShell ???
How Powershell helpful to IT Administrators ??? &
History of powershell
Jeffrey Snover
He is a Distinguished Engineer and the Lead Architect for the Windows Server Division at Microsoft. He is the inventor of Windows PowerShell, an object-based distributed automation engine, scripting language, and command line shell.
Version of PowerShell
PowerShell V1.0 PowerShell V2.0 PowerShell V3.0 PowerShell V4.0 PowerShell V5.0
• Released in November 2006.
• Supports in Windows XP SP2,Vista,2003.
• optional component of Windows Server 2008.
• Supports in Windows 7,2008 R2.
• Also released for Windows XP SP3,2003 SP2 and Vista SP1.
• PowerShell Remoting
• Modules Supports
• Windows PowerShell Integrated Scripting Environment (ISE)
• Exception Handling Supports
• APIs Supports
• New Cmdlets
• Supports in Windows 8,2012.
• Also released for win 7 SP1,2008 SP1 and 2008 R2 SP1.
• Help update (Man Pages)
• Automatic module detection
• Session connectivity
• New Cmdlets and Modules
• Supports in Windows 8.1,2012 R2
• Also released for win 7 SP1,2008 R2 SP1 and 2012 R2 SP1.
• Desired State Configuration
• New Default Execution Policy
• Enhanced debugging
• workflow PowerShell
• New Cmdlets and Modules
• Supports in Windows 10,2016 and Nano server
• Also released for win 7,8,8.1 SP1,2008 R2 SP1 and 2012 R2 SP1.
• Oneget/PowerShell get
• PowerShell class definitions (properties, methods)
• Desired State Configuration V2.0
• New Cmdlets and Modules
@source : Wikipedia
What is variable ???
Variables
Variables store information temporarily so you can take the information contained in a variable and process it in further steps.
PowerShell creates new variables automatically so there is no need to specifically "declare" variables. Simply assign data to a variable. The only thing you need to know is that variable names are always prefixed with a "$".
You can then output the variable content by entering the variable name, or you can merge the variable content into text strings. To do that, just make sure the string is delimited by double-quotes. Single-quoted text will not resolve variables.
$
• Declare Variables
• Selecting Variable Names
• Assigning and Returning Values
• Using Special Variable Cmdlets
• Default Variables
Variables
What is Data Types ???
Data TypesThere is an additional and important reason to assign data types manually because every data type has its own set of special commands. For example, a date can be stored as text in a String data type. And that's just exactly what PowerShell does: it's not clever enough to automatically guess that this really is a date or time:
$date = "November 12, 2004“;$date
November 12, 2004
If you store a date as String, then you'll have no access to special date functions. Only DateTime objects make them available. So, if you're working with date and time indicators, it's better to store them explicitly as DateTime:
[datetime]$date = "November 12, 2004" $date
Friday, November 12, 2004 00:00:00
The output of the variable will now immediately tell you the day of the week corresponding to the date, and also enable comprehensive date and time calculation commands. That makes it easy, for example, to find the date 60 days later:
$date.AddDays(60)
Tuesday, January 11, 2005 00:00:00
What are Conditions ???
Conditions
• If Condition
• if-Else Condition
• If-ElseIf-Else Condition
• Switch Condition
#Example : If-elseif-else Condition
$value = 1If ($value -eq 1) {
" Number 1"}ElseIf ($value -eq 2) {
" Number 2" }ElseIf ($value -eq 3) {
" Number 3" }else{
" Number does not exists"}
#Example : Switch Condition
$value = 6 Switch ($value) {
1 { "Number 1“ }2 { "Number 2“ }3 { "Number 3“ }
default{
"Number does not exists" }
}
What is Loop ???
Loops
• For Loop
• While Loop
• Do While loop
• For Each loop
• Switch Loop
#Example : For Loop
For ($i = 0 ;$i -lt 5;$i++) {
$i}
#Example : While Loop
$i = 0While ($i -lt 5) {
$i++$i
}
#Example : Do While Loop
$i = 0 Do {
$i++$i
} While ($i -lt 5)
#Example : For Each Loop
$array = 1..5 Foreach ($element in $array) {
"Current element: $element" }
#Example : Switch Loop
$array = 1..5 Switch ($array) {
Default { "Current element: $_" } }
Break..Continue
• Break
• Continue
#Example : For Loop with break and continue
For ($i=1; $i -lt 5; $i++) {
$password = Read-Host "Enter password ($i. try)"
If ($password -eq "secret") {
break} if ($password -ne "secret") {
continue}
}
What is command ???
What is Cmdlet ?
Cmdlet Function Get-Location get the current directory Set-Location change the current directory Copy-Item copy files Remove-Item remove a file or directory Move-Item move a file Rename-Item rename a file New-Item create a new empty file or directory
A cmdlet (pronounced "command-let") is a lightweight Windows PowerShell script that performs a single function.
A command, in this context, is a specific order from a user to the computer's operating system or to an application to perform a service, such as "Show me all my files" or "Run this program for me." Although Windows PowerShell includes more than two hundred basic core cmdlets, administrators can also write their own cmdlets and share them.
How to find cmdlets in PowerShell ??? The folks at Microsoft made several design strategies
when designing PowerShell cmdlets. First is the ability to easily infer cmdlet names, or at the very least make them easy to discover. PowerShell cmdlets are also designed to be easy to use with standardized syntax, making them easy to use interactively from the command line or to create powerful scripts.PowerShell cmdlets use the Verb-Noun format
Example : Cmdlet Name Verb NounGet-Service Get ServiceStart-Service Start Service
The verb portion of the cmdlet name indicates the action to be performed on the noun.
Three Important CmdletsIf you don’t remember anything else
Get-Help : PowerShell has help info backed right in, Just like “Man” pages in Unix
Get-Member : Since PowerShell is object aware, This cmdlet helps explore the methods and Properties within PowerShell
Get-Command : PowerShell’s dictionary lookup, this will help you to find cmdlets you can use
What is alias ???An alias is an alternate name or nickname for a cmdlet or for a command element, such as a function, script, file, or executable file. You can use the alias instead of the command name in any Windows PowerShell commands.PowerShell (Cmdlet) PowerShell (Alias) CMD.EXE / COMMAND.COM Unix shell Description
Get-ChildItem gci, dir, ls dir ls List all files / directories in the (current) directory
Test-Connection/ping N/A ping pingSends ICMP echo requests to specified machine from the current machine, or instructs another machine to do so
Get-Content gc, type, cat type cat Get the content of a file
Get-Command gcm help type, which, compgen List available commands
Get-Help help, man help apropos, man Help on commandsClear-Host cls, clear cls clear Clear the screen[b]
Copy-Item cpi, copy, cp copy cp Copy one or several files / a whole directory tree
Move-Item mi, move, mv move mv Move a file / a directory to a new location
Remove-Item ri, del, erase, rmdir, rd, rm del, erase, rmdir, rd rm, rmdir Delete a file / a directory
Rename-Item rni, ren, mv ren, rename mv Rename a file / a directory
Get-Location gl, cd, pwd cd pwdDisplay the current directory/present working directory.
@source : Wikipedia
Pipeline
You can use pipelines to send the objects that are output by one command to be
used as input to another command for processing. And you can send the output of
that command to yet another command. The result is a very powerful command
chain or "pipeline" that is comprised of a series of simple commands.
Example : Get-Process notepad | stop-process
Get-Service | Where-Object {$_.name -Match "win"}
Get-Service | Where-Object {$_.name -Match "win"} | select Name
A pipeline is a series of commands connected by pipeline operators (|) (ASCII 124) .
Each pipeline operator sends the results of the preceding command to the next
command.
What is PowerShell Profile ???
What is WMI ???
WMI
WMI represents the insides of your computer in the form of classes. WMI provides classes for nearly everything: processor, BIOS, memory, user accounts, services, etc. The name of a class usually consists of the "Win32" prefix.
For example, the Win32_Service describes services.
Get-WmiObject Win32_BIOSGet-WmiObject Win32_Process -filter 'name = "powershell.exe"‘[wmi]"Win32_Service.Name='netman'“Get-WmiObject win32_service | Where-Object {$_.name -eq "netman"}Get-WmiObject win32_computersystem | gmGet-WmiObject win32_computersystem | Select-Object domain
Powershell Remoting
What is Function ???
FunctionsFunctions are self-defined new commands consisting of general PowerShell building blocks. They have in principle three tasks:
• Shorthand: very simple shorthand for commands and immediately give the commands arguments to take along
• Combining: functions can make your work easier by combining several steps
• Encapsulating and extending: small but highly complex programs consisting of many hundreds of statements and providing entirely new functionalities
The basic structure of a function is the same in all three instances: after the Function statement follows the name of the function, and after that the PowerShell code in braces.
Example : Shorthand
Function myPing {
ping localhost }
Example : Combining
Function NextFreeDrive{
For ($x=67; $x -le 90; $x++)
{ $driveletter =
[char]$x + ":" If (!(Test-Path
$driveletter)) {
$driveletterbreak
} } }
What is Module ???
ModulesA module is a package that contains Windows PowerShell commands, such as cmdlets, providers, functions, workflows, variables, and aliases.
People who write commands can use modules to organize their commands and share them with others. People who receive modules can add the commands in the modules to their Windows PowerShell sessions and use them just like the built-in commands.
BUILT-IN MODULES :Microsoft.PowerShell.Core Microsoft.PowerShell.Diagnostics Microsoft.PowerShell.Host Microsoft.PowerShell.ManagementMicrosoft.PowerShell.Security Microsoft.PowerShell.Utility Microsoft.WSMan.Management PSScheduledJob PSWorkflow PSWorkflowUtility
HOW TO USE A MODULE
To use a module, perform the following tasks:
1. Install/Import the module. (This is often done for you.)
2. Find the commands that the module added.
3. Use the commands that the module added.
What is RSAT ???
RSAT (Remote Server Administration Tools)
RSAT (Remote Server Administration Tools) is a Windows Server component for remote management of other computers also running that operating system. RSAT was introduced in Windows Server 2008 R2.
RSAT allows administrators to run snap-ins and tools on a remote computer to manage features, roles and role services. The software includes tools for cluster-aware updating, Group Policy management and Hyper-V management, as well as the Best Practices Analyzer.
RSAT runs on Windows 7, Windows 8, Windows Server 2008, Windows Server 2008 R2 and Windows Server 2012.
Client RSAT AD VS Server RSAT AD Client
Windows 7Windows Server 2008 R2
Windows 8Windows Server 2012
Windows 8.1Windows Server 2012 R2
Server
Windows Server 2003 or 2008
AD Management Gateway Service
Windows Server 2008 R2
Windows Server 2012
Windows Server 2012 R2
Port 9389
AD CmdletFeature Level
Getting Started with AD PS Module
Active Directory PowerShell HistoryFormer Scripting Technologies
WMIADODBCMD utilitiesADSI (.NET foundation)PowerShell AD Module
2008 R2 (v1) 76 cmdlets
Forest, DomainUsers, Groups, Computers, OUsService AccountsPassword PoliciesRecycle BinEtc.
2012 (v2) +69 cmdlets
ReplicationTrustsDC CloningDynamic Access ControlADDS Deployment Module
2012 R2 (v3) +12 cmdlets
Authentication Policies
How to import & Export CSV data ???
Bulk User Creation using CSV File
Copy-aduser
* Import the Copy-aduser Function in PowerShell Console* Once Copy-aduser Function Loaded type below command to Execute itExample : copy-aduser -fristName "PowerShell" -LastName "Trainer" -userLogonName "Powershell" -SampleUserName "Pardhu" -password "Test@123"
Function is placed in below txt file
copy-aduser.txt
What is Execution Policy ???
Execution Policy
• Restricted
• AllSigned
• RemoteSigned
• Unrestricted
• Bypass
• Undefined
Windows PowerShell execution policies let you determine the conditions under which Windows PowerShell loads configuration files and runs scripts.
You can set an execution policy for the local computer, for the current user, or for a particular session. You can also use a Group Policy setting to set execution policy for computers and users.
Execution policies for the local computer and current user are stored in the registry. You do not need to set execution policies in your Windows PowerShell profile. The execution policy for a particular session is stored only in memory and is lost when the session is closed.
How to build Powershell Script ???
Thank You
To Learn Powershell you have use it moreHere are Some reference Links :To Learn More : Use Microsoft Virtual Academy For Practice : Use Microsoft Virtual LabsFor Cmdlets : Use TechNetKeep up-to-date : Use https://powershell.org/
Name : Pardha Sai (Pardhu)Role : Automation Engineer Phone : 9885572999Email ID : [email protected]