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

clip_image004[4]

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)

clip_image006[4]

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 $ _

clip_image009[4]

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:

clip_image012[4]

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

• Example:

clip_image017[4]

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

clip_image019[4]clip_image021[4]

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

clip_image023[4]

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

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

example:

clip_image025[4]

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
clip_image027[4]

Booleans

Arrays e Hashmaps

image

Arrays

clip_image033[4]

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:

clip_image036[4]

foreach

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

example:

clip_image038[4]

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%clip_image040[4]

REUSE OF CODE

functions


• encapsulate code

• Create custom cmdlets

• Ability to contain parameters

clip_image042[4]

Saving a Script
• Easy to edit scripts

�� Enable reuse scripts

• Extension. ps1

• Allow import of scripts for later reuse functions

clip_image044[4]clip_image045[4]clip_image047[4]

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

clip_image049[4]

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:

clip_image051[4]clip_image053[4]

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

clip_image055[4]

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