windows powershell. what is windows powershell? a command-line interface (cli) a new way of...

Download Windows PowerShell. What is Windows PowerShell? A command-line interface (CLI) A new way of developing Windows and other products to be more manageable

If you can't read please download the document

Upload: mark-russell

Post on 08-Jan-2018

232 views

Category:

Documents


0 download

DESCRIPTION

Why CLI? A GUI is great for doing something once. Performing anything multiple times in a GUI becomes boring and error-prone. A CLI is inherently better when it comes to automating repetitive tasks.

TRANSCRIPT

Windows PowerShell What is Windows PowerShell? A command-line interface (CLI) A new way of developing Windows and other products to be more manageable An interactive shell * as well as a simplified scripting language. PowerShell connects to existing administrative functionality: WMI.NET Framework COM ADSI Allows you to partially administer non-PowerShell technologies today Over time, more and more products will be fully exposed to PowerShell. Why CLI? A GUI is great for doing something once. Performing anything multiple times in a GUI becomes boring and error-prone. A CLI is inherently better when it comes to automating repetitive tasks. The PowerShell Advantage Bring all functionality together in one spot. Expose the functionality to be used by a GUI or a script. Create that functionality in a consistent fashion (e.g. learn one way to do everything) Using PowerShell Introducing Cmdlets (Command-lets) Cd, Ls, Copy, and Cp are all cmdlets. Technically, these are aliases, or nicknames, to the actual cmdlets. Cmdlets are written in a.NET Framework language. Cmdlets are bundled into DLL files called snap-ins or PSSnapIns. NOTE: cmdlets are not case sensitive. So Help Get-ChildItem is the same as help get-childitem. Aliases Aliases are just short names for cmdlets. Theyre easier to type. They provide consistency with old-style command names. Many aliases come built in. You can create your own! Thats why dir /s does not work Dir is an alias for Get-ChildItem. The alias only covers the cmdlet nameit doesnt change the parameters the cmdlet uses. Get-ChildItem uses a different parameter to recurse subdirectories. Dir recurse or Dir -r will do the trick. NOTE: To break any command that is taking forever, press CTRL + C. Getting help Well, expect you cant memorize all of the commands, how it works, or its syntax. Heres when help comes in handy. Help is an alias for the cmdlet Get-Help. Usage: Get-Help If you hate reading help in the console, you can always search the web! Cmdlet Consistency Cmdlet names might seem long *, but theres a reason. Consistent verb-singular noun naming means you can guess what a cmdlet name would be based on the functionality you want. This makes learning to use the shell easier. Examples: Get-Command Get-Process Get-Service New-Alias New-Service Parameter Rules Parameters are documented in Help. Parameters names are preceded by a dash. A space separates the parameter name and its value. Some commands have positional parameters dont need the name at alljust type the values in the correct order. Example: New-Alias [-name] [-value] New-Alias -name "d" -value "Get-ChildItem" New-Alias "d" "Get-ChildItem" PowerShell Pipeline Or the passage of the process of the commands Through the process Similar to database queries pipelines. So far the cmdlets youve run have seemed to produce text lists as their output. But behind the scenes, theyre actually working with objects *. When you run a cmdlet, it generally produces objects as its output. These objects are actual functioning pieces of Windows. Get-Service doesnt product a list of services, it grabs all the actual services which are installed on the computer. Service You can check its.NET Framework class on https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx. https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx This object has the following properties: Name IsStarted DisplayName ServiceType Status And so on The text list you see consists some of these properties values. The Pipeline All cmdlets run in a pipeline. The objects a cmdlet outputs go into the pipeline and the next cmdlet in the pipeline picks up whatever objects are in the pipeline. At the end of the pipeline is a special cmdlet called Out-Default (no need to specify). Out-Default takes whatever object are in the pipeline, and uses their properties (at least one) to construct a text output. A simple pipeline example Why Objects? You dont need to use text as an intermediary. You can work directly with the built-in functionality of whatever object you are manipulating. No parsing required! Easier output formatting. TIP: Dont know properties of an object? Try using Get-Member command at the end of the pipeline. Example: Get-Service Get- Member returns the methods and properties of a service object. Another example of the pipeline Get-EventLog "Windows PowerShell" | Select -first 10 | Sort InstanceID | ConvertTo-HTML | Out-file "C:\Users\EarlPeterJ\Desktop\test.html" The cmdlet separator is a vertical bar line |. You can use as many commands as you want. Scripts Script files contains commands you execute in PowerShell. Why? To reduce redundant typing of a long or complex command. Allows you to share it with your co-workers, friends, and any other people. File extension is.PS1. NOTE: Be careful when executing PS1 scripts since they may contain functions that access critical Windows components. Further research: PowerShell Security TIP: Multiple lines to type in PowerShell? Press Shift + Enter to enter new line instead of executing it. Operators & Filtering Comparisons The purpose of a comparison is to generate True or False result. PowerShell provides the $True and $False variables to represent these Boolean values. All comparisons result in either $True or $False. Comparison Operators: -eq, -ne, -gt, -lt, -ge, and le Logical Operators When you need to compare more than one expressions, you can use these logical operators: -and, -or, and -not I think you already know how to use them, so I dont have to explain them. Comparison Examples $_ represents the current object being handled. So we can use it in the following statements: $_.UserName -eq EarlPeterJ $_.EntryType nq Information We can use this expressions on Where cmdlets (much like your DB queries) after your command. Example: Get-EventLog "Windows PowerShell" | where {$_.EntryType -eq "Warning"} NOTE: Make sure you place curly braces between your queries and parenthesis between each expression. Multiple Expressions Example: Get-EventLog "Windows PowerShell" | where {($_.EntryType -eq Information) -and ($_.InstanceID -gt 400)} Select cmdlet As you noticed, there is a default format on how PowerShell outputs objects. Select-Object allows you to customize what is passed down the pipeline. Usage: Select separate by comma Or: -first / -last Sort cmdlet and Group cmdlet Sort-Object allows you to sort the list of output based on one or more properties. Usage: sort separate by comma [-descending optional ] Example: get-process | sort name Group-Object groups objects that contain the same value for specified properties. NOTE: Sorting before grouping has no effect on the grouping cmdlet. Example: get-service | group status Working with each object ForEach-Object (or foreach) allows you to loop through each object. Much like where cmdlet, use $_ as the variable for the current object. Get-Process | ForEach-Object {Write-Host $_.name -foregroundcolor cyan} Variable declaration You can declare variables preceded by dollar sign $. Follow same variable naming convention as in any other programming languages Example: $url = 'http://earlpeter.com/' $wc = New-Object System.Net.WebClient $wc.DownloadString($url) get-content "C:\Users\EarlPeterJ\Desktop\test.txt" | foreach { (new-object System.Net.WebClient).DownloadString($_) }