powershell for developers

19
Ido Flatow PowerShell For Developers

Upload: ido-flatow

Post on 21-Dec-2014

287 views

Category:

Technology


0 download

DESCRIPTION

Let’s face it, when was the last time you opened a command prompt as part of your daily routine? Did you know you can save a lot of time by creating scripts that automate your daily tasks, such as altering your application’s configuration files and then deploy the application to a remote server? There are developers that still think that scripting is the IT department’s domain. But Windows PowerShell is a different story. PowerShell is a scripting language that is more coding than scripting, mostly because its commands return objects, not text. In this session we will learn what PowerShell is, the basics of coding with it, and how to call it from your .NET code. Most of the session will focus on how developers can benefit from using PowerShell in their day-to-day routine in order to work with XML and JSON, automate deployments, manage certificates, call HTTP services, manipulate file content, and more. Next time you are asked to deploy your application to the server, type, don’t click!

TRANSCRIPT

Page 1: Powershell For Developers

Ido Flatow

PowerShell For Developers

Page 2: Powershell For Developers

About Me• Senior Architect, Sela Group, Israel• Co-author of Microsoft courses – “WCF 4” and “Developing

Windows Azure and Web Services”• Co-author, “Pro .NET Performance” and “Pro Single Page

Application Development”• Microsoft ASP.NET/IIS MVP• Focus on server, web, cloud, and DevOps• Manager of the Israeli Web Developers User Group

Page 3: Powershell For Developers

What is PowerShell?

• A command line interface• A scripting environment• A tool to automate routine tasks• Something that will simplify your life• The next generation of command-line

Page 4: Powershell For Developers

Introducing PowerShell

DEMO

Page 5: Powershell For Developers

The Most Important Cmdlets• Get-Help (help)• Get-Command (gcm)• Get-Member (gm)

Page 6: Powershell For Developers

Some Cmdlet Examples

Basic functions:• Get-ChildItem (= dir)• Set-Location (= cd)• Copy-Item (= copy)• Remove-Item (= del)

Management:• Start-Service• Import-Certificate• Write-EventLog• New-WebAppPool

Object treatment:• Where-Object (?)• Select-Object (select)• Foreach-Object (%)• Compare-Object (diff)

Layout:• Format-List• ConvertFrom-Json• Out-GridView• ConvertTo-Xml

Page 7: Powershell For Developers

Why is PowerShell so Great?

• The output of a command is an object• Objects can be pipelined as input• Built on top of the .NET Framework• Easy to discover what you can do• Has both command-line and GUI • Supports debugging• Runs locally and remotely

Page 8: Powershell For Developers

Piping Examples

Get-ChildItem -Filter "*.cs" –File -Recurse | `Measure-Object Length -Sum

( Get-WmiObject -class win32_process | ` ? {$_.ProcessName -eq 'w3wp.exe'}).GetOwner() | `Select -property domain, user

Get-Process | ? {$_.PrivateMemorySize64 -gt 50MB} | `Out-Host -Paging

Page 9: Powershell For Developers

PowerShell Pop QuizGet-EventLog "Application" `| ? {$_.Source -like 'ASP.NET*' –and ` $_.Message -match 'Event code: (\d+).*'} `| ? {$matches[1] -eq '3005'} `| foreach { new-object PSObject -prop @{ ` Time = $_.TimeWritten.Date.ToString("yyyy/MM/dd"); Message = ($_.Message | Select-String `

'(Exception message: )(.*)' ).Matches.Groups[2].Value }} `| group Time, Message `| select Count, ` @{Name="Time";Expression={$_.Name.Split(",")[0].Trim()}}, ` @{Name="Exception";Expression={$_.Name.Split(",")[1].Trim()}} `| sort -desc Time `| out-gridview

Page 10: Powershell For Developers

Tools of the Trade

PowerShell Command Line

PowerShell ISE

SAPIENS PowerShell Studio

Page 11: Powershell For Developers

PowerShell Using .NET or .NET Using PowerShell?

• You can actually do both!• Call .NET code from PowerShell• Create new C# classes in PowerShell• Execute PowerShell script from .NET

Page 12: Powershell For Developers

PowerShell and .NET Integration

DEMO

Page 13: Powershell For Developers

PowerShell For Developers

• Work with XML and JSON content• Access system resources, such as registry, files, and

certificates• Transform text files using regular expressions• Control platforms, such as IIS, MS-SQL, and Azure• Cleanup TFS content from VS solutions and project• Create automatic deployment scripts• Interact with SOAP and REST services

Page 14: Powershell For Developers

Platform-specific Cmdlets (IIS)

Page 15: Powershell For Developers

Platform-specific Cmdlets (MS-SQL)

Page 16: Powershell For Developers

PowerShell for Developers

DEMO

Page 17: Powershell For Developers

Resources

Everything PowerShellhttp://PowerShell.org

Script Repositoryhttp://www.poshcode.orghttp://gallery.technet.microsoft.com/scriptcenter

Blogshttp://blogs.msdn.com/PowerShellhttp://blogs.technet.com/heyscriptingguy

@idoflatow [email protected] http://blogs.microsoft.co.il/idof

Presentation and Sampleshttp://1drv.ms/1n8w1nM

Page 18: Powershell For Developers

Questions

Page 19: Powershell For Developers

Thank You