+ All Categories
Home > Documents > The Power of PowerShell: Advanced

The Power of PowerShell: Advanced

Date post: 07-Nov-2014
Category:
Upload: microsoft-technet-belgium-and-luxembourg
View: 1,084 times
Download: 1 times
Share this document with a friend
Description:
More info on http://techdays.be.
Popular Tags:
21
The Power Of PowerShell: Advanced Kurt Roggen, Technical Consultant, Devoteam http://TryCatch.be/blogs/ roggenk @roggenk [email protected] #techdays be
Transcript
Page 1: The Power of PowerShell: Advanced

The Power Of PowerShell: Advanced

Kurt Roggen, Technical Consultant, Devoteam

http://TryCatch.be/blogs/roggenk

@[email protected]

#techdaysbe

Page 2: The Power of PowerShell: Advanced

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

Page 3: The Power of PowerShell: Advanced
Page 4: The Power of PowerShell: Advanced

PS Cmdlet Syntax

verb-<ID>SingularNoun

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

Page 5: The Power of PowerShell: Advanced

3 cmdlets to know/remember!!

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

Page 6: The Power of PowerShell: Advanced

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

Page 7: The Power of PowerShell: Advanced

2. Get-Command

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

Page 8: The Power of PowerShell: Advanced

3. Get-Member

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

Page 9: The Power of PowerShell: Advanced

Modules

Page 10: The Power of PowerShell: Advanced

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

Page 11: The Power of PowerShell: Advanced

Functions

Page 12: The Power of PowerShell: Advanced

Creating Functions

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

gwmi Win32_OperatingSystem -ComputerName $ComputerName}

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

Page 13: The Power of PowerShell: Advanced

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

Page 14: The Power of PowerShell: Advanced

Comment Based Help

Page 15: The Power of PowerShell: Advanced

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

)}

Page 16: The Power of PowerShell: Advanced

Accepting Pipeline Input

Page 17: The Power of PowerShell: Advanced

Accepting Pipeline Input

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

<blabla> } }

Page 18: The Power of PowerShell: Advanced

Creating PS Objects

Page 19: The Power of PowerShell: Advanced

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

Page 20: The Power of PowerShell: Advanced

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!

Page 21: The Power of PowerShell: Advanced

Recommended