An Introduction to Windows PowerShell
Upcoming SlideShare
Loading in...5
×
 

An Introduction to Windows PowerShell

on

  • 36,736 views

In this presentation, I give an introduction to Windows PowerShell: ...

In this presentation, I give an introduction to Windows PowerShell:

- What is it, and how does it work?
- How can you extend it to provide support for administering your own product or project?

NOTES:
1) Some of the text in this presentation is a little small for reading in a 400 pixel flash viewer. I'd recommend downloading the presentation instead.
2) The slides might not make sense without the notes that go with them. I've added the notes as comments to each slide. They still might not make much sense, but that's a different problem :-)

Statistics

Views

Total Views
36,736
Slideshare-icon Views on SlideShare
36,611
Embed Views
125

Actions

Likes
16
Downloads
1,093
Comments
163

11 Embeds 125

http://www.slideshare.net 56
http://dalelane.co.uk 40
http://microtix.blogspot.com 10
http://paper.li 8
http://www.microtix.blogspot.com 3
http://www.microtix.blogspot.in 3
http://webcache.googleusercontent.com 1
http://us-w1.rockmelt.com 1
http://twitter.com 1
http://microtix.blogspot.in 1
http://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

110 of 163 Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Thank you.
    Are you sure you want to
    Your message goes here
    Processing…
  • Educational and Helpful
    Are you sure you want to
    Your message goes here
    Processing…
  • Please feel free to contact me if you have any questions.

    (For IBM audiences) I'm happy to help with any PowerShell projects – if you're looking to add PowerShell support to your product, please don't hesitate to get in touch.<br /><br/>
    Are you sure you want to
    Your message goes here
    Processing…
  • So – to recap.

    Windows PowerShell – an environment for system administrators, providing support for advanced scripting and an interactive shell, as well as a platform on which to build GUIs. An object-oriented shell which conforms to standard naming and function patterns, letting you move from product to product with a reduced learning curve. A .NET shell, giving access to anything in the .NET landscape from the command line.

    Finally, an extensible environment, with support for scripts, as well as custom functions (through cmdlets) and data types (through providers).<br /><br/>
    Are you sure you want to
    Your message goes here
    Processing…
  • Remember the different data sources that PowerShell supports.

    You might not want to extend the functions that PowerShell has, but to add support for a different data source. This is also possible, by writing a custom provider.

    There wasn't time in an (already packed!) hour presentation to talk about this, but if there is enough demand, I could go through this in a separate presentation.<br /><br/>
    Are you sure you want to
    Your message goes here
    Processing…

110 of 163

Post Comment
Edit your comment

An Introduction to Windows PowerShell An Introduction to Windows PowerShell Presentation Transcript

  • Windows PowerShell An introduction to...
  • Dale Lane [email_address] IBM Hursley Park
  • What is Windows PowerShell? How does PowerShell work? How can I 'hack' PowerShell so that it can be used with my product? Agenda
  • What is Windows PowerShell?
  • What is Windows PowerShell for?
  • What is Windows PowerShell for? ADMIN
  • What is Windows PowerShell for? ADMIN Building GUIs on top of PowerShell
  • What is Windows PowerShell for? ADMIN Interactive command shell
  • What is Windows PowerShell for? ADMIN Scripting
  • What is Windows PowerShell for? ADMIN COM Scripting for WMI
  • What is Windows PowerShell for? ADMIN
  • How does PowerShell work?
  •  
  •  
  • Get-Process
  • Get-Process
  • a verb a noun Get-Process
  • http://msdn2.microsoft.com/en-us/library/ms714428.aspx The verbs
  • The verbs Consistent Learnable Readable
  •  
  • Get-Process | Where { $_.Handles -gt 500 } | Sort Handles | Format-Table
  • HELP!
  • HELP! Get-Command Get-Help Get-PSDrive Get-Members
  •  
  • What command do I need?
  •  
  • How does it work?
  • How does it work?
  • How does it work?
  • How does it work?
  • How does it work?
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  • Consistent Get-Process | Where { $_.Handles -gt 500 } | Sort Handles | Format-Table
  •  
  • What data stores are available?
  •  
  • Windows Registry
  •  
  • Certificates
  •  
  • Environment variables
  •  
  • Variables
  • 'dir' ?
  •  
  • Aliases
  •  
  •  
  • Stopping a process kill -9 `ps -aef | grep 'notepad' | grep -v grep | awk '{print $2}'`
  • Stopping a process kill -9 `ps -aef | grep 'notepad' | grep -v grep | awk '{print $2}'` Why so complicated?
  • Stopping a process Get-Process notepad | Stop-Process
  • Stopping a process Get-Process notepad | Stop-Process Why so much easier?
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  • Select Where Sort Compare
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  • -Confirm -Verbose -WhatIf -Debug
  •  
  •  
  •  
  •  
  • Get-Process | Export-Csv
  •  
  •  
  •  
  •  
  •  
  •  
  • Using .NET
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  • ([xml](new-object Net.WebClient) .DownloadString ($bbc_news_rss_url)).rss.channel.item | Select title, Desc*, *date -first 8
  •  
  •  
  •  
  • How can I “ hack” PowerShell?
  • Why?
  • WebSphere MQ “ Queue” “ Message” “ Queue Manager”
  •  
  • a verb a noun Get-Process
  • a verb product name object type http://msdn2.microsoft.com/en-us/library/ms714657.aspx Get-ProdObject
  •  
  •  
  • “ Some of the queues used by the sales team are getting a bit full. Can you increase the amount of space in them?”
  • “ Some of the queues used by the sales team are getting a bit full. Can you increase the amount of space in them?”
  • “ Some of the queues used by the sales team are getting a bit full. Can you increase the amount of space in them?”
  • “ Some of the queues used by the sales team are getting a bit full. Can you increase the amount of space in them?”
  •  
  • Get-Command Get-*WMQ*Queue
  • Get-WMQQueue
  • Where {$_.Name -like “SALES.*” -and $_.CurrentDepth -gt ($_.MaximumDepth - 10)}
  • Select Name, CurrentDepth, MaximumDepth
  •  
  •  
  • ForEach-Object -process {Set-WMQQueue $_ -MaximumDepth ($_.MaximumDepth * 2)}
  • ForEach-Object -process {Set-WMQQueue $_ -MaximumDepth ($_.MaximumDepth * 2)}
  •  
  •  
  • Get-Member
  •  
  •  
  • Where {$_.CreationDateTime -ge $(Get-Date -month 10 -day 15 -year 2007) -and $_.CreationDateTime -le $(Get-Date -month 10 -day 20 -year 2007)}
  • “ Set the maximum depth for all cluster queues that start with a letter between 'D' and 'K'to 20.”
  • “ Set the maximum depth for all cluster queues that start with a letter between 'D' and 'K'to 20.” Get-WMQQueue * * | Where {$_.Name -like &quot;[D-K]*&quot; -and $_.ClusterName -ne ''} | Set-WMQQueue -MaximumDepth 20
  • “ Set the maximum depth for all cluster queues that start with a letter between 'D' and 'K'to 20.” Get-WMQQueue * * | Where {$_.Name -like &quot;[D-K]*&quot; -and $_.ClusterName -ne ''} | Set-WMQQueue -MAXDEPTH 20
  • “ Get a list of non-system sender channels, showing the name, connection name, transmission queue, SSL Cipher Spec, and the name of the queue manager it is on.”
  • “ Get a list of non-system sender channels, showing the name, connection name, transmission queue, SSL Cipher Spec, and the name of the queue manager it is on.” Get-WMQChannel * * | Where {$_.ChannelType -eq [IBM.WMQ.MQC]::MQCHT_SENDER -and $_.Name -match &quot;^(?!SYSTEM).*&quot;} | Select Name, Conn*Name, Trans*Name, SSLCiph*, @{e={$_.QueueManager.Name};n='Host'} Name ConnectionName TransmissionQueueName SSLCipherSpec Host ---- -------------- --------------------- ------------- ---- SECURE dlane.hursley.ibm.com(9090) TRANS1 NULL_MD5 post SECURE.R dlane.hursley.ibm.com(9091) TRANSR TRIPLE_DES_SHA_US test
  • Export-CSV ConvertTo-HTML
  • How?
  •  
  • What can I do with PowerShell? Ad-hoc scripts Production scripts
  • What can I do with PowerShell? Ad-hoc scripts Production scripts
  • What can I do with PowerShell? Ad-hoc scripts Production scripts
  • What can I do with PowerShell?
    • try stuff out in an interactive shell
    • tie a few things together in utilities
    • build commands up into a script
    • generalize (parameterize, etc.)
    • clean up and productize
    • share!
  • Ad-hoc function Get-WMQQueue ($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQException] { Write-Error (&quot;WMQ Exception: CC=&quot; + $_.Exception.CompletionCode + &quot; RC=&quot; + $_.Exception.ReasonCode) continue } # if we have a connection to a queue manager... if ($qmgr -ne $null) { $qNames = Get-WMQQueueNames($qmgr) foreach ($queuename in $qNames) { $nextQueue = $qmgr.AccessQueue($queuename.Trim(), [IBM.WMQ.MQC]::MQOO_INQUIRE) Write-Output $nextQueue } } else { Write-Host &quot;No queue manager connection&quot; } }
  • Ad-hoc function Get-WMQQueue ($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQException] { Write-Error (&quot;WMQ Exception: CC=&quot; + $_.Exception.CompletionCode + &quot; RC=&quot; + $_.Exception.ReasonCode) continue } # if we have a connection to a queue manager... if ($qmgr -ne $null) { $qNames = Get-WMQQueueNames($qmgr) foreach ($queuename in $qNames) { $nextQueue = $qmgr.AccessQueue($queuename.Trim(), [IBM.WMQ.MQC]::MQOO_INQUIRE) Write-Output $nextQueue } } else { Write-Host &quot;No queue manager connection&quot; } }
  • Ad-hoc function Get-WMQQueue ($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQException] { Write-Error (&quot;WMQ Exception: CC=&quot; + $_.Exception.CompletionCode + &quot; RC=&quot; + $_.Exception.ReasonCode) continue } # if we have a connection to a queue manager... if ($qmgr -ne $null) { $qNames = Get-WMQQueueNames($qmgr) foreach ($queuename in $qNames) { $nextQueue = $qmgr.AccessQueue($queuename.Trim(), [IBM.WMQ.MQC]::MQOO_INQUIRE) Write-Output $nextQueue } } else { Write-Host &quot;No queue manager connection&quot; } }
  • Ad-hoc function Get-WMQQueue ($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQException] { Write-Error (&quot;WMQ Exception: CC=&quot; + $_.Exception.CompletionCode + &quot; RC=&quot; + $_.Exception.ReasonCode) continue } # if we have a connection to a queue manager... if ($qmgr -ne $null) { $qNames = Get-WMQQueueNames($qmgr) foreach ($queuename in $qNames) { $nextQueue = $qmgr.AccessQueue($queuename.Trim(), [IBM.WMQ.MQC]::MQOO_INQUIRE) Write-Output $nextQueue } } else { Write-Host &quot;No queue manager connection&quot; } }
  • Ad-hoc function Get-WMQQueueManager($name, $hostname, $port, $svrconn) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQException] { Write-Error (&quot;WMQ Exception: CC=&quot; + $_.Exception.CompletionCode + &quot; RC=&quot; + $_.Exception.ReasonCode) continue } # hashtable to describe the connection to the queue manager $connProperties = New-Object System.Collections.Hashtable if (($hostname -eq $null) -and ($port -eq $null) -and ($svrconn -eq $null)) { # user has not provided any information for a client connection # so we default to a local bindings connection type $connProperties.Add([string][IBM.WMQ.MQC]::TRANSPORT_PROPERTY, [string][IBM.WMQ.MQC]::TRANSPORT_MQSERIES_BINDINGS) }
  • Ad-hoc else { # user has provided some information for a client connection # # a future version of this should provide support for other # connection types (e.g. managed or XA client) but for # my initial purposes, bindings and client connections are # sufficient $connProperties.Add([string][IBM.WMQ.MQC]::TRANSPORT_PROPERTY, [string][IBM.WMQ.MQC]::TRANSPORT_MQSERIES_CLIENT) if ($hostname -ne $null) { $connProperties.Add([string][IBM.WMQ.MQC]::HOST_NAME_PROPERTY, $hostname) }
  • Ad-hoc if ($svrconn -ne $null) { $connProperties.Add([string][IBM.WMQ.MQC]::CHANNEL_PROPERTY, $svrconn) } else { # use a sensible default # this wont be to everyone's tastes, but will often be # right for me, and will save me a lot of typing! $connProperties.Add([string][IBM.WMQ.MQC]::CHANNEL_PROPERTY, &quot;SYSTEM.DEF.SVRCONN&quot;) }
  • Ad-hoc if ($port -ne $null) { $connProperties.Add([string][IBM.WMQ.MQC]::PORT_PROPERTY, $port) } else { # use a sensible default # this wont be to everyone's tastes, but will often be # right for me, and will save me a lot of typing! $connProperties.Add([string][IBM.WMQ.MQC]::PORT_PROPERTY, &quot;1414&quot;) } } return New-Object IBM.WMQ.MQQueueManager($name, $connProperties) }
  • Ad-hoc function Get-WMQQueueNames($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQException] { Write-Error (&quot;WMQ Exception: CC=&quot; + $_.Exception.CompletionCode + &quot; RC=&quot; + $_.Exception.ReasonCode) continue } # if we have a connection to a queue manager... if ($qmgr -ne $null) { # using PCF to access a list of queue names # # this is sort of cheating - this is an undocumented, unsupported # API, and I wrote this using tab-complete to identify what # seems like sensible method names # # please do *not* take this as any sort of IBM recommendation # or endorsement to use PCF in C# # [IBM.WMQ.PCF.PCFMessageAgent]$agent = New-Object IBM.WMQ.PCF.PCFMessageAgent $agent.Connect($qmgr)
  • Ad-hoc [IBM.WMQ.PCF.PCFMessage]$request = New-Object IBM.WMQ.PCF.PCFMessage([IBM.WMQ.MQC]::MQCMD_INQUIRE_Q_NAMES) $request.AddParameter([IBM.WMQ.MQC]::MQCA_Q_NAME, &quot;*&quot;) $request.AddParameter([IBM.WMQ.MQC]::MQIA_Q_TYPE, [IBM.WMQ.MQC]::MQQT_LOCAL) [IBM.WMQ.PCF.PCFMessage[]]$responses = $agent.Send($request, $TRUE) [IBM.WMQ.PCF.PCFParameter[]]$pcfParms = $responses[0].GetParameters() $queueNames = $pcfParms[0].GetValue() # we don't want to display temporary queues # (such as that which will have been created by the PCF command!) # so we filter the response array before returning it return $queueNames | Where-Object -FilterScript {$_ -notlike &quot;AMQ.*&quot;} } else { Write-Host &quot;No queue manager&quot; } }
  • Ad-hoc
  • http://channel9.msdn.com/ShowPost.aspx?PostID=256835 Production
  • Production “ How to Create a Windows PowerShell Cmdlet” walkthrough http://msdn2.microsoft.com/en-us/library/ms714598.aspx
  • Production “ How to Create a Windows PowerShell Cmdlet” walkthrough http://msdn2.microsoft.com/en-us/library/ms714598.aspx “ PowerShell Cmdlet guidelines” http://msdn2.microsoft.com/en-us/library/ms714657.aspx
  • Production #region GetProcCommand /// <summary> /// This class implements a Get-Proc cmdlet that has no parameters. /// </summary> [Cmdlet(VerbsCommon.Get, &quot;Proc&quot;)] public class GetProcCommand : Cmdlet { #region Cmdlet Overrides /// <summary> /// For each of the requested process names, retrieve and write /// the associated processes. /// </summary> protected override void ProcessRecord() { // Get the current processes Process[] processes = Process.GetProcesses(); // Write the processes to the pipeline making them available to the // next cmdlet. The second parameter tells PowerShell to enumerate the // array, and send one process at a time to the pipeline WriteObject(processes, true); } #endregion Overrides } #endregion GetProcCommand
  • Production
  • Production
  • Production
  • What can I do with PowerShell?
    • try stuff out in an interactive shell
    • tie a few things together in utilities
    • build commands up into a script
    • generalize (parameterize, etc.)
    • clean up and productize
    • share!
  •  
  •  
  • Extending function --> scripts and Cmdlets Extending data stores --> providers Extending PowerShell
  •  
  • What is Windows PowerShell? How does PowerShell work? How can I hack PowerShell so that it can be used with my product? Recap
  • Dale Lane [email_address] IBM Hursley Park Windows PowerShell An introduction to...