The Power of PowerShell: Advanced

Post on 07-Nov-2014

1,084 views 1 download

Tags:

description

More info on http://techdays.be.

transcript

The Power Of PowerShell: Advanced

Kurt Roggen, Technical Consultant, Devoteam

http://TryCatch.be/blogs/roggenk

@roggenkkurt.roggen@devoteam.be

#techdaysbe

Agenda

• PowerShell Basics• PS Syntax• Pipeline• 3 cmdlets to know/remember

• PowerShell Advanced• Functions• Modules• Comment Based Help• Accepting PS Objects from the pipeline• Creating PS Objects

PS Cmdlet Syntax

verb-<ID>SingularNoun

Get-Service, New-Mailbox, Get-ADUser, Get-Disk, Get-VM, Start-Process, Stop-Process, New-NALun, Get-AzureVM, ...

3 cmdlets to know/remember!!

PowerShell is self-discoverable using1. Get-Command2. Get-Help3. Get-Member

1. Get-Help

Displays help informationGet-HelpGet-Help get-serviceGet-Help get-service -examplesGet-Help get-service -detailedGet-Help get-service -fullGet-Help get-service -onlineGet-Help get-service -parameter ComputerNameGet-Help * -parameter ComputerNameGet-Help about_wildcardsAlias: help

2. Get-Command

Displays all commands (cmdlet, function, alias)Get-CommandGet-Command -verb getGet-Command -noun serviceGet-Command -module ActiveDirectoryGet-Command -CmdType cmdletAlias: gcm

3. Get-Member

Displays all MemberTypes (Property,Methods,...)Get-Service | Get-MemberGet-Member –InputType (Get-Service)Get-Member –MemberType MethodGet-Member –MemberType PropertyAlias: gm

Modules

Modules

Collections of cmdletsPortableStored in file system

%windir%\System32\WindowsPoweshell\1.0\Modules

Module Cmdlets: New-Module, Get-Module, Import-Module, Remove-Module

Module Manifest Cmdlets: New-ModuleManifest , Test-ModuleManifest

Functions

Creating Functions

function Get-OS{ param( [string[]]$ComputerName = $env:COMPUTERNAME )

gwmi Win32_OperatingSystem -ComputerName $ComputerName}

PS C:\> Get-OS -ComputerName “MyClient1”, ”MyClient2”

Creating Modules

Modules Location:%windir%\System32\WindowsPoweshell\1.0\Modules

Create a PS module folder (eg: MyModule)

Save PS module with extension .PSM1 instead of .PS1 (MyModule.psm1)• PS module folder name must have same name as PS module file

basename

Comment Based Help

Comment Based Help

Function Get-OS{

<# .SYNOPSIS

<toktok>.DESCRIPTION

<toktok>.PARAMETER ComputerName

<toktok>.EXAMPLE

Get-OS.LINK

http://mysite.com/get-os/#> Param (

[string]$ComputerName,[switch]$Details

)}

Accepting Pipeline Input

Accepting Pipeline Input

function Get-OS{ param( [Parameter(ValueFromPipeLine=$true)] [string[]]$ComputerName = $env:COMPUTERNAME ) PROCESS {

<blabla> } }

Creating PS Objects

Create PS Object$Obj = New-Object –Type PSObject$Obj | Add-Member –Name ComputerName –Value $ComputerName –Type NoteProperty$Obj | Add-Member –Name OS –Value $OS –Type NoteProperty $Obj | Add-Member –Name ServicePack –Value $ServicePack –Type NoteProperty $Obj | Add-Member –Name Version –Value $ComputerName –Type NoteProperty

Return PS Object (to pipeline)$Obj

Conclusion

PowerShell is the future presentPowerShell is Self-discoverable Remember 3 basic commands

Reuse code using functions in modules

It’s easy - Try it, you’ll see!