Powershell For Developers

Post on 21-Dec-2014

280 views 0 download

Tags:

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 of Powershell For Developers

Ido Flatow

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

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

Introducing PowerShell

DEMO

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

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

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

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

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

Tools of the Trade

PowerShell Command Line

PowerShell ISE

SAPIENS PowerShell Studio

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

PowerShell and .NET Integration

DEMO

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

Platform-specific Cmdlets (IIS)

Platform-specific Cmdlets (MS-SQL)

PowerShell for Developers

DEMO

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 idof@sela.co.il http://blogs.microsoft.co.il/idof

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

Questions

Thank You