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?...
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  -firs...
 
 
 
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 1...
“ 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 * * | Wher...
“ Set the maximum depth for all cluster queues that start with a letter between 'D' and 'K'to 20.” Get-WMQQueue * * | Wher...
“ Get a list of non-system sender channels, showing the name, connection name, transmission queue, SSL Cipher Spec, and th...
“ Get a list of non-system sender channels, showing the name, connection name, transmission queue, SSL Cipher Spec, and th...
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? <ul><li>try stuff out in an interactive shell </li></ul><ul><li>tie a few things together i...
Ad-hoc function Get-WMQQueue ($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQEx...
Ad-hoc function Get-WMQQueue ($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQEx...
Ad-hoc function Get-WMQQueue ($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQEx...
Ad-hoc function Get-WMQQueue ($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQEx...
Ad-hoc function Get-WMQQueueManager($name, $hostname, $port, $svrconn) { # display details of any WMQ errors encountered i...
Ad-hoc else { # user has provided some information for a client connection # # a future version of this should provide sup...
Ad-hoc if ($svrconn -ne $null) { $connProperties.Add([string][IBM.WMQ.MQC]::CHANNEL_PROPERTY, $svrconn) } else { # use a s...
Ad-hoc if ($port -ne $null) { $connProperties.Add([string][IBM.WMQ.MQC]::PORT_PROPERTY, $port) } else { # use a sensible d...
Ad-hoc function Get-WMQQueueNames($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ....
Ad-hoc [IBM.WMQ.PCF.PCFMessage]$request =  New-Object IBM.WMQ.PCF.PCFMessage([IBM.WMQ.MQC]::MQCMD_INQUIRE_Q_NAMES) $reques...
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.asp...
Production #region GetProcCommand /// <summary> /// This class implements a Get-Proc cmdlet that has no parameters. /// </...
Production
Production
Production
What can I do with PowerShell? <ul><li>try stuff out in an interactive shell </li></ul><ul><li>tie a few things together i...
 
 
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? R...
Dale Lane [email_address] IBM Hursley Park Windows PowerShell An introduction to...
Upcoming SlideShare
Loading in...5
×

An Introduction to Windows PowerShell

34,278

Published on

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 :-)

Published in: Technology
163 Comments
21 Likes
Statistics
Notes
  • Thank you.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Educational and Helpful
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • 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/>
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • 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/>
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • 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/>
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
34,278
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
1,221
Comments
163
Likes
21
Embeds 0
No embeds

No notes for slide
  • An Introduction to Windows PowerShell

    1. 1. Windows PowerShell An introduction to...
    2. 2. Dale Lane [email_address] IBM Hursley Park
    3. 3. What is Windows PowerShell? How does PowerShell work? How can I 'hack' PowerShell so that it can be used with my product? Agenda
    4. 4. What is Windows PowerShell?
    5. 5. What is Windows PowerShell for?
    6. 6. What is Windows PowerShell for? ADMIN
    7. 7. What is Windows PowerShell for? ADMIN Building GUIs on top of PowerShell
    8. 8. What is Windows PowerShell for? ADMIN Interactive command shell
    9. 9. What is Windows PowerShell for? ADMIN Scripting
    10. 10. What is Windows PowerShell for? ADMIN COM Scripting for WMI
    11. 11. What is Windows PowerShell for? ADMIN
    12. 12. How does PowerShell work?
    13. 15. Get-Process
    14. 16. Get-Process
    15. 17. a verb a noun Get-Process
    16. 18. http://msdn2.microsoft.com/en-us/library/ms714428.aspx The verbs
    17. 19. The verbs Consistent Learnable Readable
    18. 21. Get-Process | Where { $_.Handles -gt 500 } | Sort Handles | Format-Table
    19. 22. HELP!
    20. 23. HELP! Get-Command Get-Help Get-PSDrive Get-Members
    21. 25. What command do I need?
    22. 27. How does it work?
    23. 28. How does it work?
    24. 29. How does it work?
    25. 30. How does it work?
    26. 31. How does it work?
    27. 39. Consistent Get-Process | Where { $_.Handles -gt 500 } | Sort Handles | Format-Table
    28. 41. What data stores are available?
    29. 43. Windows Registry
    30. 45. Certificates
    31. 47. Environment variables
    32. 49. Variables
    33. 50. 'dir' ?
    34. 52. Aliases
    35. 55. Stopping a process kill -9 `ps -aef | grep 'notepad' | grep -v grep | awk '{print $2}'`
    36. 56. Stopping a process kill -9 `ps -aef | grep 'notepad' | grep -v grep | awk '{print $2}'` Why so complicated?
    37. 57. Stopping a process Get-Process notepad | Stop-Process
    38. 58. Stopping a process Get-Process notepad | Stop-Process Why so much easier?
    39. 68. Select Where Sort Compare
    40. 77. -Confirm -Verbose -WhatIf -Debug
    41. 82. Get-Process | Export-Csv
    42. 89. Using .NET
    43. 99. ([xml](new-object Net.WebClient) .DownloadString ($bbc_news_rss_url)).rss.channel.item | Select title, Desc*, *date -first 8
    44. 103. How can I “ hack” PowerShell?
    45. 104. Why?
    46. 105. WebSphere MQ “ Queue” “ Message” “ Queue Manager”
    47. 107. a verb a noun Get-Process
    48. 108. a verb product name object type http://msdn2.microsoft.com/en-us/library/ms714657.aspx Get-ProdObject
    49. 111. “ Some of the queues used by the sales team are getting a bit full. Can you increase the amount of space in them?”
    50. 112. “ Some of the queues used by the sales team are getting a bit full. Can you increase the amount of space in them?”
    51. 113. “ Some of the queues used by the sales team are getting a bit full. Can you increase the amount of space in them?”
    52. 114. “ Some of the queues used by the sales team are getting a bit full. Can you increase the amount of space in them?”
    53. 116. Get-Command Get-*WMQ*Queue
    54. 117. Get-WMQQueue
    55. 118. Where {$_.Name -like “SALES.*” -and $_.CurrentDepth -gt ($_.MaximumDepth - 10)}
    56. 119. Select Name, CurrentDepth, MaximumDepth
    57. 122. ForEach-Object -process {Set-WMQQueue $_ -MaximumDepth ($_.MaximumDepth * 2)}
    58. 123. ForEach-Object -process {Set-WMQQueue $_ -MaximumDepth ($_.MaximumDepth * 2)}
    59. 126. Get-Member
    60. 129. Where {$_.CreationDateTime -ge $(Get-Date -month 10 -day 15 -year 2007) -and $_.CreationDateTime -le $(Get-Date -month 10 -day 20 -year 2007)}
    61. 130. “ Set the maximum depth for all cluster queues that start with a letter between 'D' and 'K'to 20.”
    62. 131. “ 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
    63. 132. “ 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
    64. 133. “ 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.”
    65. 134. “ 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
    66. 135. Export-CSV ConvertTo-HTML
    67. 136. How?
    68. 138. What can I do with PowerShell? Ad-hoc scripts Production scripts
    69. 139. What can I do with PowerShell? Ad-hoc scripts Production scripts
    70. 140. What can I do with PowerShell? Ad-hoc scripts Production scripts
    71. 141. What can I do with PowerShell? <ul><li>try stuff out in an interactive shell </li></ul><ul><li>tie a few things together in utilities </li></ul><ul><li>build commands up into a script </li></ul><ul><li>generalize (parameterize, etc.) </li></ul><ul><li>clean up and productize </li></ul><ul><li>share! </li></ul>
    72. 142. 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; } }
    73. 143. 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; } }
    74. 144. 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; } }
    75. 145. 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; } }
    76. 146. 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) }
    77. 147. 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) }
    78. 148. 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;) }
    79. 149. 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) }
    80. 150. 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)
    81. 151. 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; } }
    82. 152. Ad-hoc
    83. 153. http://channel9.msdn.com/ShowPost.aspx?PostID=256835 Production
    84. 154. Production “ How to Create a Windows PowerShell Cmdlet” walkthrough http://msdn2.microsoft.com/en-us/library/ms714598.aspx
    85. 155. 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
    86. 156. 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
    87. 157. Production
    88. 158. Production
    89. 159. Production
    90. 160. What can I do with PowerShell? <ul><li>try stuff out in an interactive shell </li></ul><ul><li>tie a few things together in utilities </li></ul><ul><li>build commands up into a script </li></ul><ul><li>generalize (parameterize, etc.) </li></ul><ul><li>clean up and productize </li></ul><ul><li>share! </li></ul>
    91. 163. Extending function --> scripts and Cmdlets Extending data stores --> providers Extending PowerShell
    92. 165. What is Windows PowerShell? How does PowerShell work? How can I hack PowerShell so that it can be used with my product? Recap
    93. 166. Dale Lane [email_address] IBM Hursley Park Windows PowerShell An introduction to...
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×