introduction to powershell

25
[email protected] Blog: http://www.Salaudeen.Blogspot.com

Upload: salaudeen-rajack

Post on 24-May-2015

1.349 views

Category:

Technology


3 download

DESCRIPTION

Introduction to PowerShell

TRANSCRIPT

Page 1: Introduction to PowerShell

[email protected]

Blog: http://www.Salaudeen.Blogspot.com

Page 2: Introduction to PowerShell

What is PowerShell ? Problems with existing scripting language (VB script) How PowerShell solves the security issues Basic commands in Powershell GUI (IDE) for Powershell How to get help in PowerShell Alias Snap-ins Cmd-lets in PowerShell Variables Understanding the pipe line Operators in PowerShell

◦ Logical Operators Sorting, Measuring, Select, Filter and compare Export, Import, Convert Functions Regular expressions Arrays and Hash Table XML handling

Page 3: Introduction to PowerShell

NEW scripting platform for Microsoft products One scripting language – Multiple products

◦ Windows Desktop OS, Server OS◦ SharePoint◦ SQL Server◦ SCOM/SCDPM/SVCMM◦ Exchange Server◦ VMWARE/Citrix

Runs on top of .net framework, 2.0+ Automate almost every thing you can do with GUI (some

times, things which are not possible with GUI) Not just command prompt or Script language, But

Command-Shell. It’s the Microsoft Way… Shell prompt, just like DOS shell, But more powerful

Page 4: Introduction to PowerShell

Restricted - No scripting allowed unrestricted - You can any scripting

• no signing required Remote signed – good for test, dev environments

◦ only files from internet need to be signed◦ default setting

All signed - local, remote script, it should be signed.◦ user must agree to run script

Page 5: Introduction to PowerShell

No common scripting for all the products◦ .Net code◦ COM Model◦ Exe◦ VBScript

Scripts are really security concern, because they do have lot of power◦ Echo “Welcome”◦ Del *.* ???

Top Concerns:◦ Integrity◦ Identity◦ Double click Run◦ Command Hijacking

PowerShell addresses this issue by introducing Executing Policy

Page 6: Introduction to PowerShell

“built-in” commands for PowerShell◦ “verb-noun” names

eg. get-childitem (= ls) but: new-alias, new-object

◦ extensible set: can write own cmdlets Heart and Soul of PowerShell Engine that make powershell work. They

are the small units of functionality that perform the operations.

Page 7: Introduction to PowerShell

Shell prompt Help system

◦ Getting help:

Get-help Get-help –verb get Get-help –noun file

Get-help stop-process –examples Get-help stop-process –full

Get-help “sp*

◦ Out-file Ps>file1.txt ps>>file2.txt Ps |out-file process.txt

◦ Get-content

Page 8: Introduction to PowerShell

Powershell snap-in provides a mechanism for registering sets of cmdlets◦ Example: similar to MMC

Set of cmd-lets for a specific product◦ Eg. SharePoint

Get-pssnapin ◦ Lists the core functionality

Get-Pssnapin – registered◦ Shows the installed cmd-Lets

To Add a new PS Snapin:◦ Add-Snapin <snap-in-Name>

Page 9: Introduction to PowerShell

Ask Help: help *process* Get-process > Alias ps

◦ Eg. Get-process –name calc How to get the –name parameter?

◦ Get-process | get-member◦ Stop-process -> Alias Kill◦ Stop-process –name calc ◦ Stop-process –name calc –whatif

Services◦ Get-service <service name>◦ Restart-service <service name>◦ Stop-service <service name>

Page 10: Introduction to PowerShell

Get-service –include “Sharepoint*” Get-service –exclude “Sharepoint*”

Event log:◦ Get-eventlog

Eg. get-eventlog system –newest 10 Get-eventlog | -newest 10 format-list

IDE PowerGUI - Open source yet powerfull, FREE Powershell + Primal script ISE – PowerShell 2.0

Page 11: Introduction to PowerShell

Powershell assigns best suited data type for variables when assigned◦ New-variable -name var –value 10

Or◦ $var=10◦ Remove-variable –name varIt supports Int, Datetime, Bool, string, Char, byte, decimal,

array, xml Variables are actually .net objects

◦ $test=“honeywell”◦ Can say $test.toUpper()◦ User get-member to retrieve all the member of the object

Can force the data type by ◦ [string]$var=5

$var.gettype().fullname

Page 12: Introduction to PowerShell

Commands manipulates and passes objects from One to another

Eg: Get the list of process -> filter > stop ->format

Get-process | where-object {$_.status –eq “Stopped”} |format-list

Get-process | out-file C:\process.txt Get-process | out-Printer <Name of the printer> Write-output vs write-host

◦ First one sends output to the pipeline, Second doesn’t Write-output “Hello” |where-object {$_.length – gt 2}

◦ We have some additional options like –foregroundcolor

A D C D

Page 13: Introduction to PowerShell

All Basic math operations: +, -, *, /, %◦ 5+5; 10-1; 8*2; 10%3; 5+(5*5)

Comparison◦ EQ

10 –eq 5◦ LT, -GT, -GE, -LE◦ String comparison: not case sensitive

“Hello” – eq “HELLO” > true Forcing case sensitive:

“Hello” – ceq “HELLO” > true

Logical operators AND OR NOT

Page 14: Introduction to PowerShell
Page 15: Introduction to PowerShell
Page 16: Introduction to PowerShell
Page 17: Introduction to PowerShell
Page 18: Introduction to PowerShell
Page 19: Introduction to PowerShell
Page 20: Introduction to PowerShell
Page 21: Introduction to PowerShell

Standard for Pattern matching◦ Use –Match◦ Eg. “Honeywell” –match “Honey”◦ . (dot) – one char◦ * - Zero or more match “A” match “t*”◦ + - one or more match “TTT” match “^T+”◦ ? – Zero or one match◦ [AB] – either A or B◦ ^ - start ◦ $ - end eg. “Sala” –match “^s..A$”◦ \w – any word character -W –Non word◦ \s – space -S◦ \d -D◦ (n,m) eg. “TTTT” –match “^T{4, 6}”

Page 22: Introduction to PowerShell

◦ $H=“Honeywell”◦ $h.length◦ Say “hello” >> “Say “”hello”””

Array: ◦ $arr=1,2,3 or $arr=@(1,2,3)◦ $arr2=@((1,1),(2,2),(3,3))

Get : $arr2[1][1]

◦ Hash table: $Hash=@{No=1;”CName“=“Honeywell”} $hash.no $hash[“Cname”]

Page 23: Introduction to PowerShell

◦ $MyXML=[XML] @”◦ <addressBook>

<Person type=“personal”> <name>ABC</name> <Phone>123</phone>

</person> </addressbook> “@

$myXML.AddressBook $myXML.Person $myXML.Person[0]

Page 24: Introduction to PowerShell

Download powershell through Microsoft.com Videos

◦ http://channel9.msdn.com/Media/?TagID=163 Blogs

◦ http://blogs.msdn.com/powershell◦ http://thepowershellguy.com◦ http://keithhill.spaces.live.com◦ http://www.leeholmes.com/blog

PowerShell Installation Instructions: http://shrinkster.com/rpy PowerTab by MoW - http://shrinkster.com/rpx “MSH Logo” by Lee Holmes - http://shrinkster.com/rpw PowerShell Community Extensions

◦ http://www.codeplex.com/PowerShellCX MSDN - http://shrinkster.com/rpu

◦ How to create a cmdlet: http://shrinkster.com/rpv Blogs

◦ PowerShell Team Blog - http://blogs.msdn.com/powershell/◦ Lee Holmes - http://www.leeholmes.com/blog/◦ David Aiken - http://blogs.msdn.com/daiken/◦ The PowerShell Guy (MoW) - http://thepowershellguy.com/

Popular Newsgroup◦ microsoft.public.windows.powershell

Page 25: Introduction to PowerShell

[email protected]

Blog: http://www.Salaudeen.Blogspot.com