An Introduction to Windows PowerShell

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

162 comments

Comments 1 - 10 of 162 previous next Post a comment

  • + guest2d2c59 guest2d2c59 2 years ago
    Great presentation - learned quite a bit by just paging thru it. thx.
  • + dalelane Dale Lane 2 years ago
    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.

  • + dalelane Dale Lane 2 years ago
    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).

  • + dalelane Dale Lane 2 years ago
    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.

  • + dalelane Dale Lane 2 years ago
    I've talked about how to add new function to PowerShell – by writing scripts or custom cmdlets

    This isn't the only way you can extend PowerShell.

  • + dalelane Dale Lane 2 years ago
    Make-Shell lets you create custom shells suitable for sharing – so you can provide your customers or third-party with a finished executable that includes your new cmdlets.

  • + dalelane Dale Lane 2 years ago
    Or, you could build them into a shell suitable for sharing

  • + dalelane Dale Lane 2 years ago
    I mentioned before that the PowerShell functions can be polished into production-quality commands that you can share with customers or third-parties.

    If you write your own Cmdlets you could share this as a DLL, suitable for adding to PowerShell as I've just shown.

  • + dalelane Dale Lane 2 years ago
    Then, open a PowerShell prompt and run

    Add-PSSnapIn to add the new cmdlet(s) to your PowerShell environment

  • + dalelane Dale Lane 2 years ago
    You then install this using InstallUtil

Comments 1 - 10 of 162 previous next

Post a comment
Embed Video
Edit your comment Cancel

6 Favorites

An Introduction to Windows PowerShell - Presentation Transcript

  1. Windows PowerShell An introduction to...
  2. Dale Lane [email_address] IBM Hursley Park
  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. What is Windows PowerShell?
  5. What is Windows PowerShell for?
  6. What is Windows PowerShell for? ADMIN
  7. What is Windows PowerShell for? ADMIN Building GUIs on top of PowerShell
  8. What is Windows PowerShell for? ADMIN Interactive command shell
  9. What is Windows PowerShell for? ADMIN Scripting
  10. What is Windows PowerShell for? ADMIN COM Scripting for WMI
  11. What is Windows PowerShell for? ADMIN
  12. How does PowerShell work?
  13.  
  14.  
  15. Get-Process
  16. Get-Process
  17. a verb a noun Get-Process
  18. http://msdn2.microsoft.com/en-us/library/ms714428.aspx The verbs
  19. The verbs Consistent Learnable Readable
  20.  
  21. Get-Process | Where { $_.Handles -gt 500 } | Sort Handles | Format-Table
  22. HELP!
  23. HELP! Get-Command Get-Help Get-PSDrive Get-Members
  24.  
  25. What command do I need?
  26.  
  27. How does it work?
  28. How does it work?
  29. How does it work?
  30. How does it work?
  31. How does it work?
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. Consistent Get-Process | Where { $_.Handles -gt 500 } | Sort Handles | Format-Table
  40.  
  41. What data stores are available?
  42.  
  43. Windows Registry
  44.  
  45. Certificates
  46.  
  47. Environment variables
  48.  
  49. Variables
  50. 'dir' ?
  51.  
  52. Aliases
  53.  
  54.  
  55. Stopping a process kill -9 `ps -aef | grep 'notepad' | grep -v grep | awk '{print $2}'`
  56. Stopping a process kill -9 `ps -aef | grep 'notepad' | grep -v grep | awk '{print $2}'` Why so complicated?
  57. Stopping a process Get-Process notepad | Stop-Process
  58. Stopping a process Get-Process notepad | Stop-Process Why so much easier?
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. Select Where Sort Compare
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. -Confirm -Verbose -WhatIf -Debug
  78.  
  79.  
  80.  
  81.  
  82. Get-Process | Export-Csv
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. Using .NET
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. ([xml](new-object Net.WebClient) .DownloadString ($bbc_news_rss_url)).rss.channel.item | Select title, Desc*, *date -first 8
  100.  
  101.  
  102.  
  103. How can I “ hack” PowerShell?
  104. Why?
  105. WebSphere MQ “ Queue” “ Message” “ Queue Manager”
  106.  
  107. a verb a noun Get-Process
  108. a verb product name object type http://msdn2.microsoft.com/en-us/library/ms714657.aspx Get-ProdObject
  109.  
  110.  
  111. “ Some of the queues used by the sales team are getting a bit full. Can you increase the amount of space in them?”
  112. “ Some of the queues used by the sales team are getting a bit full. Can you increase the amount of space in them?”
  113. “ Some of the queues used by the sales team are getting a bit full. Can you increase the amount of space in them?”
  114. “ Some of the queues used by the sales team are getting a bit full. Can you increase the amount of space in them?”
  115.  
  116. Get-Command Get-*WMQ*Queue
  117. Get-WMQQueue
  118. Where {$_.Name -like “SALES.*” -and $_.CurrentDepth -gt ($_.MaximumDepth - 10)}
  119. Select Name, CurrentDepth, MaximumDepth
  120.  
  121.  
  122. ForEach-Object -process {Set-WMQQueue $_ -MaximumDepth ($_.MaximumDepth * 2)}
  123. ForEach-Object -process {Set-WMQQueue $_ -MaximumDepth ($_.MaximumDepth * 2)}
  124.  
  125.  
  126. Get-Member
  127.  
  128.  
  129. Where {$_.CreationDateTime -ge $(Get-Date -month 10 -day 15 -year 2007) -and $_.CreationDateTime -le $(Get-Date -month 10 -day 20 -year 2007)}
  130. “ Set the maximum depth for all cluster queues that start with a letter between 'D' and 'K'to 20.”
  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 "[D-K]*" -and $_.ClusterName -ne ''} | Set-WMQQueue -MaximumDepth 20
  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 "[D-K]*" -and $_.ClusterName -ne ''} | Set-WMQQueue -MAXDEPTH 20
  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.”
  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 "^(?!SYSTEM).*"} | 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
  135. Export-CSV ConvertTo-HTML
  136. How?
  137.  
  138. What can I do with PowerShell? Ad-hoc scripts Production scripts
  139. What can I do with PowerShell? Ad-hoc scripts Production scripts
  140. What can I do with PowerShell? Ad-hoc scripts Production scripts
  141. 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!
  142. Ad-hoc function Get-WMQQueue ($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQException] { Write-Error ("WMQ Exception: CC=" + $_.Exception.CompletionCode + " RC=" + $_.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 "No queue manager connection" } }
  143. Ad-hoc function Get-WMQQueue ($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQException] { Write-Error ("WMQ Exception: CC=" + $_.Exception.CompletionCode + " RC=" + $_.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 "No queue manager connection" } }
  144. Ad-hoc function Get-WMQQueue ($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQException] { Write-Error ("WMQ Exception: CC=" + $_.Exception.CompletionCode + " RC=" + $_.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 "No queue manager connection" } }
  145. Ad-hoc function Get-WMQQueue ($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQException] { Write-Error ("WMQ Exception: CC=" + $_.Exception.CompletionCode + " RC=" + $_.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 "No queue manager connection" } }
  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 ("WMQ Exception: CC=" + $_.Exception.CompletionCode + " RC=" + $_.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) }
  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) }
  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, "SYSTEM.DEF.SVRCONN") }
  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, "1414") } } return New-Object IBM.WMQ.MQQueueManager($name, $connProperties) }
  150. Ad-hoc function Get-WMQQueueNames($qmgr) { # display details of any WMQ errors encountered in this function Trap [IBM.WMQ.MQException] { Write-Error ("WMQ Exception: CC=" + $_.Exception.CompletionCode + " RC=" + $_.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)
  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, "*") $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 "AMQ.*"} } else { Write-Host "No queue manager" } }
  152. Ad-hoc
  153. http://channel9.msdn.com/ShowPost.aspx?PostID=256835 Production
  154. Production “ How to Create a Windows PowerShell Cmdlet” walkthrough http://msdn2.microsoft.com/en-us/library/ms714598.aspx
  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
  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
  157. Production
  158. Production
  159. Production
  160. 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!
  161.  
  162.  
  163. Extending function --> scripts and Cmdlets Extending data stores --> providers Extending PowerShell
  164.  
  165. What is Windows PowerShell? How does PowerShell work? How can I hack PowerShell so that it can be used with my product? Recap
  166. Dale Lane [email_address] IBM Hursley Park Windows PowerShell An introduction to...

+ Dale LaneDale Lane, 2 years ago

custom

11722 views, 6 favs, 1 embeds more stats

In this presentation, I give an introduction to Win more

More Info

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

Go to text version
  • Total Views 11722
    • 11689 on SlideShare
    • 33 from embeds
  • Comments 162
  • Favorites 6
  • Downloads 325
Most viewed embeds
  • 33 views on http://dalelane.co.uk

more

All embeds
  • 33 views on http://dalelane.co.uk

less

Flagged as inappropriate Flag as inappropriate
Flag as innappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel

Categories