Post on 20-Jun-2015
description
transcript
Introduction to PowerShell
Learn it before it become URGENT
Salaudeen.Rajack@Gmail.com
Blog: http://www.Salaudeen.Blogspot.com
Agenda
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
What is 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
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
PowerShell’s Execution Policy
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
Problems with Existing scripting languages
“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.
Cmdlets
Basic Tour
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
Snap-In
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>
Basic cmd-lets for process, services
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>
Basic cmd-lets for process, services (Cont.)
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
Variables
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
Pipelines
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
Operators
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
Sort – Measure –Select - filter
Sort◦ Get-process | sort-object VM –desc◦ Get-service |sort status
Measure◦ Get-service |measure-object◦ Get-service |measure-object –property VM –sum –
min-max –average Select
◦ Get-service | select-object displayname,status◦ Get-process | select-object –first 10
Export-Import and compare
Export-CSV◦ Get-process |export-csv
Export-CSV◦ $process=import-csv c:\sa.csv
Compare:◦ $p1=get-process
Now open a new process, say calc◦ $p2=get-process◦ Compare-object $p1, $p2 –property name
Logical constructs
IF, Switch, For, While◦ IF, Switch – Decision◦ For, while – looping◦ Eg IF($var –gt 100) { write-host “yes”}◦ Elseif()◦ {◦ }◦ Else◦ {◦ }
Logical constructs
Switch◦ eg$company =“Honeywell”Switch($var){“Wipro” {write-host “wipro”}“Honeywell” {write-host “wipro”}Default {write-host “Not in list”}}
While
While, Do…Until, Do..while
$var=1 While($var – lt 10)
{write-host $var
$var++} For-each
◦ Eg. $services=get-service Foreach($x in $services) { write-host $x.name.toupper() }
Script Block
Executes the block of code from file, variable $b={write-host “Hello”} $b >>write-host “hello” To Execute : &$b
Functions:◦ Function sayHello()
{ write-host “Hello”}sayHello
Functions Cont.
◦ Function sayHello($SenderName){ write-host “Hello” + $senderName}sayHello “Honeywell”
• SayHello “honeywell” -> write-host “Hello” $args[0]
Return statement:function determine
{ if($var – gt 10){return $true}Else{return $false}}
Regular expression
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}”
Strings, Arrays, Hash tables
◦ $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”]
XML
◦ $MyXML=[XML] @”◦ <addressBook>
<Person type=“personal”> <name>ABC</name> <Phone>123</phone>
</person> </addressbook> “@
$myXML.AddressBook $myXML.Person $myXML.Person[0]
Resources 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
Thank you
Salaudeen.Rajack@Gmail.com
Blog: http://www.Salaudeen.Blogspot.com