Introduction to PowerShell


Published on

Introduction to PowerShell

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Objects mean it’s easy to get and process informationSyntax – while it can be “wordy” is consistent and usually easily readFully implemented help modelYou can call/run other scripts and executable from within PowerShell. You can access .net components, COM objects.It can do everything, but that doesn’t mean it’s the best way to do something. It’s pretty good at guessing what you want it to do.
  • While not case sensitive, there are conventions, and it’s best to be consistent.You can use an IDE – PowerGUI, or PrimalScript, or just Notepad++ for syntax highlightingDIR, CD, ECHO, >, CLS, LS are all aliased to the new PS commands. You can create new aliases. I try and avoid them in examples.
  • At the moment, we’re just looking at the Properties.Compare with CMD’s “date /T”DayOfYear, DayOfWeek
  • First we’ll have a look at the help for the Get-ServiceThe brackets mean it will evaluate the expression contained first
  • We can return only a couple of properties from the Get-Service resultsWe can sort by any of these propertiesWe can limit the results returned using Where-Object (this can be aliased as ?)
  • Some are pre-created (like $_)Note that when you assign a variable, it doesn’t get written to screenVariables are automatically typed. Automatic typing can be over-ruled
  • Write-Host is like EchoOut-File is like >Out-Gridview is new, handy for quick checksExport-CSV saves the file, ConvertTo-CSV just converts the object and writes the output to the consoleConvertTo-HTML is the basis for producing fancy reportsXML outputs are handy if you need to store the object for later
  • Arrays are a type of variable used to store groups of objectsYou can mix types $array = @(“Test",(Get-Date),1)Arrays are numbered from 0, so [1] is the second item (you get used to this)Ranges are separated with “..”You can index in backwards with negative numbers
  • Remember with –or that each side must be a fully qualified evaluation, i.e., ($date.DayOfWeek-eq"Saturday“ -or"Sunday")would not work.
  • So, how would we index into the array for specific elements?
  • Add-PSSnapinQuest.ActiveRoles.ADManagementAdd-PSSnapinVMware.VimAutomation.CoreImport-Module ActiveDirectoryImport-Module GroupPolicyProfiles
  • Introduction to PowerShell

    1. 1. Introduction to PowerShell
    2. 2. Introduction • Previewed in 2003 as “Monad” • Built on .net • Designed to be:– Object oriented – Consistent – Discoverable • It can work as framework to hold other components together
    3. 3. PowerShell fundamentals • • • • • Not case sensitive Can be edited in Notepad CMDLets – Verb-Noun Aliases help ease people into PS Line breaks are denoted by a grave character ` – Which is also an escape character • Variables are dynamically typed • Code comments are indicated by #
    4. 4. Objects – properties & methods • Get one object Get-Date • Look at it’s properties with Get-Member Get-Date | Get-Member • Access a particular property (Get-Date).Year
    5. 5. Objects – properties & methods • Properties tell you about something, whereas methods do something (Get-Date).AddDays(10) • Be careful – methods can make changes (Get-Service | Where-Object { $_.Name -eq "Fax"}).Stop()
    6. 6. Example – Get-Service Get-Help Get-Service Get-Service (Get-Service).Count Get-Service | Get-Member
    7. 7. Example – Get-Service Get-Service | Select-Object Name,Status Get-Service | Select-Object Name,Status | Sort-Object Name Get-Service | Where-Object {$_.Status -eq "Stopped"} (Get-Service | Where-Object {$_.Status -eq "Stopped"}).Count
    8. 8. Running a script • • • • • • Save a text file as .PS1 Doesn’t invoke when clicked Need to change security settings Relative / absolute paths Run in Dot-source for troubleshooting Pass a script as an argument to powershell.exe for automatic execution (i.e., for a scheduled task or shortcut)
    9. 9. Variables • Variable names are always preceded by a $ • Objects, strings, integers etc., can be assigned to variables $today = Get-Date • In the above example case, the object’s properties are now properties of the variable $today.Day • …and the object’s methods are methods of the variable $tomorrow = $today.AddDays(1)
    10. 10. Formatting output PowerShell contains a multitude of ways to export/format results:Write-Host Out-File Out-Gridview Export-CSV ConvertTo-CSV ConvertTo-HTML ConvertTo-XML Export-CliXML
    11. 11. Arrays Example: $films = @("The Terminator","Commando","Predator", "Alien") Arrays can contain any combination of types of PowerShell objects You can index into an array $films[1] $films[1..3] $films[-1]
    12. 12. IF statements $date = Get-Date If ($date.DayOfWeek -eq "Saturday"){Write-Host "Go Home"} If ($date.DayOfWeek -eq "Saturday" -or $date.DayOfWeek -eq "Sunday"){ Write-Host "Go Home" } Else { Write-Host "Don't go home" }
    13. 13. Loops - ForEach ForEach ($film in $films){ Write-Host $film } • The $film variable is automatically populated for each item in the array. • Instead of an array, you can use any collection of objects (like Get-Service)
    14. 14. Example – Loops and Conditions ForEach ($service in Get-Service){ If ($service.Status -eq "Running"){ $color = "Green" } Else { $color = "Red" } Write-Host $service.DisplayName -ForegroundColor $color }
    15. 15. Snap-Ins and Modules • Provide new CMDLets • Available for – Active Directory – Group Policy – SCCM – Exchange – SQL • These all work in a consistent way
    16. 16. Questions?
    17. 17. Useful resources • PowerGUI – PowerShell IDE • Mastering PowerShell - Free eBook • PowerCLI – VMware CMDLets
    18. 18. Comparison operators Operator -eq Description Equal to -lt -gt -ge Less than Greater than Greater than, or equal to -le -ne -not ! -and -or Less than or equal to Not equal to Not Not And Or