PowerShell in 10 Minutes

 Ownership of Microsoft· Scripting language designed especially for system administrators.

· Targeted for management and administration systems

· Object Oriented based pipelines

· Perform C #, WMI, XML, Active Directory and. NET

WORK ENVIRONMENT

console
• Lets you run PowerShell cmdlets

http://powershellpt.files.wordpress.com/2013/01/clip_image0044_thumb.jpg?w=244&h=84&h=84

Editors Scripts
· Allow execute PowerShell cmdlets

· Provide intellisense

· They add colors for easier reading of code

· Information about cmdlets available

· Examples: PowerGUI and PowerShell ISE (Native)

http://powershellpt.files.wordpress.com/2013/01/clip_image0064_thumb.jpg?w=244&h=132&h=132

Run external scripts
• The default policy has PowerShell Script Execution strict, not allowing any external script.

• We need to change the execution policy scripts

One. Open the console in administrator mode

2nd. Run the following command:

Set-ExecutionPolicy-ExecutionPolicy Unrestricted

*** ***Different types of policies

Restricted Do not run any script allows external PowerShell. Default behavior is PowerShell.
AllSigned Only run scripts that contains a digital signature, whether internal or from the internet.
RemoteSigned Recommended. Scripts from the internet need to contain a digital signature. When not contain, asking if you really want to run the script from that user.
Unrestricted Does not require any authorization (digital signature) to run the script.
Bypass The responsibility for safety is totally up to the user.

SYNTAX OF POWERSHELL

variables
Defined with the prefix $

example:

$ var1 = “Rui”

pipelines
• Allow use the output of an invocation in PowerShell (cmdlet, function) as input to another invocation.

• Greatest strength of PowerShell

• Simplify code

• Represented as “|”

• Allow access to the element that is in the pipe through command $ _

http://powershellpt.files.wordpress.com/2013/01/clip_image0094_thumb.png?w=244&h=49&h=49

Cmdlets (command-lets)
• Functions that encapsulate code

• Simple syntax, verb-name (Get-Process, Set-Partition, etc. ..)

• Ability to contain parameters

• Parameters defined prefix “-”, which may be positional ie after a specific position.

example:

http://powershellpt.files.wordpress.com/2013/01/clip_image0124_thumb.png?w=244&h=62&h=62

Alias ​​Cmdlet
• Names simplified for faster writing a cmdlet.

• Example:

http://powershellpt.files.wordpress.com/2013/01/clip_image0174_thumb.png?w=244&h=54&h=54

Print to console
• Allows you to print a variable, return a cmdlet or function to the PowerShell console.

http://powershellpt.files.wordpress.com/2013/01/clip_image0194_thumb.jpg?w=244&h=40&h=40http://powershellpt.files.wordpress.com/2013/01/clip_image0214_thumb.jpg?w=244&h=42&h=42

Read console
• Allows you to read a value from the PowerShell console.

http://powershellpt.files.wordpress.com/2013/01/clip_image0234_thumb.jpg?w=244&h=37&h=37

get Help
• Cmdlet which gives help on the commands and their respective parameters.

• Write in the console followed by the Get-Help command

example:

http://powershellpt.files.wordpress.com/2013/01/clip_image0254_thumb.jpg?w=244&h=33&h=33

Comparison operators

Operator PowerShell
equals -eq
diferent -ne
greater than -gt
lower than -lt
greater than equal -ge
less than equaligual -le
Compare with wilcards -like ou -notlike
compare string withregex -match ou -notmatch
If array has value (or string) -contains

Logical operators

Operator PowerShell
and -and
or -or
exclusive or -xor
not !  -nor
http://powershellpt.files.wordpress.com/2013/01/clip_image0274_thumb.jpg?w=188&h=244&h=244

Booleans

Arrays e Hashmaps

http://powershellpt.files.wordpress.com/2013/01/image_thumb.png?w=644&h=72&h=72

Arrays

http://powershellpt.files.wordpress.com/2013/01/clip_image0334_thumb.jpg?w=244&h=79&h=79

Hashmaps
$nomes= @{"First" = "Rui"; "Middle" = "Pedro"; "Last" =  "Machado"}

If-then-else

logical routine that allows you to create a scenario with two possible executions due to the validation of a condition.

example:

http://powershellpt.files.wordpress.com/2013/01/clip_image0364_thumb.jpg?w=244&h=159&h=159

foreach

logical routine that lets you scroll through all objects stored in a variable with one or more dimensions

example:

http://powershellpt.files.wordpress.com/2013/01/clip_image0384_thumb.jpg?w=244&h=92&h=92

foreach a pipeline

logic that allows traverse all objects stored in a variable in one or more dimensions passed into a pipeline

example:

NOTE: This cmdlet has an alias often used “%” ie we can replace the foreach by%http://powershellpt.files.wordpress.com/2013/01/clip_image0404_thumb.jpg?w=244&h=92&h=92

REUSE OF CODE

functions

• encapsulate code

• Create custom cmdlets

• Ability to contain parameters

http://powershellpt.files.wordpress.com/2013/01/clip_image0424_thumb.jpg?w=244&h=63&h=63

Saving a Script
• Easy to edit scripts

�� Enable reuse scripts

• Extension. ps1

• Allow import of scripts for later reuse functions

http://powershellpt.files.wordpress.com/2013/01/clip_image0444_thumb.jpg?w=86&h=80&h=80http://powershellpt.files.wordpress.com/2013/01/clip_image0454_thumb.png?w=68&h=71&h=71http://powershellpt.files.wordpress.com/2013/01/clip_image0474_thumb.jpg?w=244&h=64&h=64

Reuse a script

  1. Import the script that has the function you want to use
  2. Use it as any other cmdlet

• Allow you to reuse code

http://powershellpt.files.wordpress.com/2013/01/clip_image0494_thumb.jpg?w=244&h=40&h=40

Chamar função .net

• Add the DLL namespace between square brackets

• At C++ style, just add :: followed by the function you want to call

Example:

http://powershellpt.files.wordpress.com/2013/01/clip_image0514_thumb.jpg?w=244&h=38&h=38http://powershellpt.files.wordpress.com/2013/01/clip_image0534_thumb.jpg?w=244&h=29&h=29

More Info

• http://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx

• http://technet.microsoft.com/pt-br/library/bb978526.aspx

• http://technet.microsoft.com/pt-br/library/dd367858(v=WS.10).aspx

• http://poshcode.org/3654

• http://powershellbooks.com/

• http://www.windowsitpro.com/topics/powershell-scripting/don-jones-on-powershell

See Also

Aboute me

http://powershellpt.files.wordpress.com/2013/01/clip_image0554_thumb.jpg?w=113&h=113&h=113

Rui Pedro Machado

Graduation in Technologies and Information Systems

Post Graduation in Business Intelligence

Software Engineer at DevScope (www.devscope.net)

Blog: http://rpmachado.wordpress.com

Facebook: http://facebook.com/ruimachado89

Linkedin: http://www.linkedin.com/in/rpmachado