Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

PowerShell

Avatar for Margarita Margarita
January 27, 2017

 PowerShell

Basics

Avatar for Margarita

Margarita

January 27, 2017
Tweet

More Decks by Margarita

Other Decks in Programming

Transcript

  1. Commandlets Special commands (known as cmdlets) are typed in after

    the prompt, and are executed when you press the Enter key or execute your PowerShell script.
  2. Variables $myVar = 2017 [string] $myString = "any string“ $myString

    = $myString + " was changed" $myString = "Create $($myString)" [int[]] $myArray = 12,64,8,64,12 foreach($num in $myArray){Write-Host $num} $myArray = @(1,"Hello",3.5,"World")
  3. Print in the Host • Write-Host; • Write-Debug; ($DebugPreference =

    "SilentlyContinue" | "Continue" ) • Write-Error; • Write-Warning;
  4. Execution Policy • Before you can write and run scripts,

    you should be aware that Windows is not configured to allow the execution of unsigned scripts because they can be used to damage the system. • To configure your execution policy so that you can run scripts you have written, you can use the command set-executionpolicy. • To execute your command without warning or restriction use -Force
  5. PipeLine A pipeline occurs when one takes the output of

    one command and directs it to the input of another command.
  6. Scripts and Modules • Scripts are text files that contain

    sequences of calls to cmdlets, and these files have the extension .ps1. • Unlike working in the console, each operation does not have to be typed in and executed immediately. • A script module is essentially any valid PowerShell script saved in a .psm1 extension. • You can use the module cmdlets, such as Import-Module MyModule
  7. Include modules/script • To include PS module we can add

    directory with modules into the environmental variable or specify full path in Import-Module cmdlet • To include PS scripts we have to use “.” symbol with a path
  8. Comments • In PowerShell single line comments start with a

    hash symbol, everything to the right of the # will be ignored. • In PowerShell 2.0 and above multi-line block comments can be used: <# comment #>
  9. Functions • To create a function, call the function keyword

    followed by a name for the function, then include your code inside a pair of curly braces. • Function can return value or just execute some actions • Function can accept some input params or not
  10. Call a functions/script • A block of code may be

    contained within a function for easy re-use. • When we have connecting to a module we can easily work with functions inside it (example Import-Module "sqlpsx") • We can call an execution of a scripts mostly the same way as a function
  11. Parameters • You’ll often write a script or function that

    needs to accept some kind of input. • You can tell Windows PowerShell to expect these parameters, collect them from the command line, and put their values into variables within your script or function. • Full-fledged syntax lets you define parameters as mandatory, specify a position and more. ([Parameter(Mandatory=$True)])
  12. Filtering • One of the best things about using an

    object-based pipeline is that you can filter objects out of the pipeline at any stage using the Where-Object cmdlet.
  13. Sorting • To sort just retrieve your data and then

    pipe it to the Sort-Object cmdlet, telling Sort-Object which property to sort on.
  14. For loop • For loops are typically used to iterate

    through a set of commands a specified number of times, either to step through an array or object, or just to repeat the same block of code as needed. • For loops can be used to step through array values by setting the initial value to the initial index of the array and incrementally increasing the value until the array length is met.
  15. For each loop • ForEach-Object has two aliases, ForEach and

    %, and also supports shorthand syntax beginning in PowerShell 3.0. The following three examples are identical in function.
  16. While, Do-While • While and Do-While loops are both used

    to perform an action while the condition evaluates to $true • Do-Until loops have similar syntax to Do-While, but stop processing once the condition statement is met.
  17. Try Catch • Try, Catch, Finally blocks are used for

    error handling in Windows PowerShell.
  18. Remote execution You can use several ways to execute any

    PS command on remote machine; • Execute command inside ScriptBlock in Invoke-Command; • Use paexec.exe utility; • Start PS session with using Enter-PSSession ;
  19. Export to CSV | TXT • The Export-Csv cmdlet makes

    it easy to export data as a commaseparated values (CSV) file • The Out-File cmdlet sends output to a file. You can use this cmdlet instead of the redirection operator (>) when you need to use its parameters.
  20. Work with XML • To read data from xml file

    use Get-Content cmdlet • To get element use *.SelectSingleNode(/elem) • To add new element we should use *.CreateElement and then add it to xml via *.AppendChild • To add Attribute for element use SetAttribute(“name”,”value”)
  21. Work with SDK • For connecting to SDK we should

    provide a path to *.dll lib • Then we are able to work with SDK Namespace Class Name Static method Parameters
  22. Send email • To Send emails you can use default

    commandlet Send-MailMessage; • For this command you should specify SMTP server parameter or set $PSEmailServer variable;