Publicidad

Intro to PowerShell Workflow

Contributing Editor en Petri.com
4 de Dec de 2014
Publicidad

Más contenido relacionado

Publicidad
Publicidad

Intro to PowerShell Workflow

  1. Intro to PowerShell Workflow JEFF HICKS
  2. Agenda What is Workflow? Workflow Syntax Running Workflows Practical Workflows Best Practices
  3. Think! Just because you can use a PowerShell workflow doesn’t mean you should.
  4. Definitions – What is a Workflow A robust multi-machine orchestration engine Designed for long running unattended tasks across potentially thousands of machines Persistent states can survive reboots and network interruptions Previously built in Visual Studio
  5. Definitions - What is PowerShell Workflow? • Persistence via check points • Suspend and Resume capabilitiesRobust • Parallel tasks • Connection pooling • Connection throttling Performance and Scalability • Use existing cmdlets* • No need to master XAML or use Visual Studio • Built in parameters for multi-machine management PowerShell Based
  6. Workflow Scenarios Server deployment Server configuration/remediation User provisioning Private cloud deployments Sharepoint configuration Any business workflow that can be orchestrated with command line tools.
  7. Key Workflow Concepts Workflow is a series of orchestrated activities Workflow activities are isolated All data and objects are serialized Objects are strongly typed Workflows use static scoping
  8. Requirements Built on .NET Framework 4.0 and Windows Workflow Foundation Requires PowerShell 3.0 Requires PowerShell 3.0 remoting Leverages PowerShell's job infrastructure
  9. Requirements: Remoting Workflows connect to machines using WSMan protocol Connects to the default Workflow session configuration PS C:> get-pssessionconfiguration microsoft.powerShell.workflow Important defaults: ◦ Max persistence storage 10GB ◦ Max memory per shell 1GB ◦ Admin permissions required Be very careful of changing this configuration
  10. High Level Workflow Configure-Server { #magic happens } PS C:> configure-server –pscomputername SRV01 Script converted to Workflow XAML Workflow endpoint Workflow engine
  11. Limitations and Gotchas All objects and data must be "serializable" Must use full cmdlet and parameter names – these are activities No positional parameters No Begin/ Process/End scriptblocks No "eventing"
  12. Limitations and Gotchas No Traps - Use Try/Catch Not intended to be interactive – don’t use Write-Host. No comment based help - must use MAML formatted files Pay close attention to scope!
  13. Workflow Architecture Workflow Engine PowerShell Client Session PowerShell Workflow “Service” WinRM Client Client PSRP/PowerShell Managed Node Remoting Commands/API Operation Host Process Operations Host WSMan/CIM Managed Node
  14. Building Workflows Don't simply replace "Function" with "Workflow" Start new and plan out your activities Minimize sharing of data or variables across activities PowerShell turns your workflow into XAML Workflow is a new command type in PowerShell 3.0 PS C:> get-command -commandtype Workflow
  15. Syntax: Sequence Execute a collection of activities in order Can be executed with Parallel Watch out for scope!
  16. Syntax: Sequence Sequence { MyCommand1 } Sequence { MyCommand2 MyCommand3 MyCommand4 } This command runs Then these commands run in sequence
  17. Syntax: Parallel Execute a collection of activities independently and in parallel ForEach -Parallel • The parameter only works in a workflow • Run a set of commands in parallel for each object in a collection Parallel key word • Run a set of commands simultaneously and in parallel • Runs in a new scope • Often used to run a series of Sequences
  18. Parallel Parallel { MyCommand1 MyCommand2 MyCommand3 Sequence { MyCommand4 MyCommand5 } #sequence } #parallel These commands run in sequence Commands and sequence run simultaneously
  19. ForEach -Parallel ForEach -parallel ($item in $object) { #do something with each object MyCommand1 $item MyCommand2 $item } For each item in the collection… Run these commands All object processed simultaneously
  20. Syntax: InlineScript Send PowerShell commands to remote machine(s) Runs out-of-process Runtime command validation This is really a series of Invoke-Command activities Use for all other non-activity commands
  21. InlineScript Workflow New-Audit { Inlinescript { $procs = Get-WmiObject Win32_process $procs | Select-Object ProcessID,Name, @{Name="RunTime";Expression={(Get-Date) - $_.ConvertToDateTime($_.CreationDate)}}, @{Name="Owner";Expression={"$($_.GetOwner().Doma in)$($_.GetOwner().User)"}}} }
  22. Syntax: Scope and Variables Workflows uses static scopes, i.e. PowerShell won't "search" for a variable Assume each activity is isolated Can use $Workflow:MyVar for “global” references • Read-only • Available from InlineScript Access "out of scope" variables with $Using:MyVar
  23. Syntax: Common Parameters All workflows have a set of common parameters They do not need to be defined •PSComputerName •PSCredential •PSConnectionRetryCo unt •PSActionRetryCount •PSPersist
  24. Syntax: Persistence Workflows can be made persistent to survive interruptions Planned reboots Network interruptions By default entire workflow restarts unless… Set persistence per activity •Checkpoint-Workflow •Persist •Suspend-Workflow Set persistence for the entire workflow -PSPersist common parameter Set to $True: -pspersist $true
  25. Running Workflows Load workflow Specify remote computers Specify credentials Run as a job Use a Workflow session
  26. Invoke-AsWorkflow Run any command or expression as a workflow Run as an InlineScript Executed from the console Great for ad-hoc remote management without writing a workflow Invoke-AsWorkflow -CommandName get-eventlog - Parameter @{Logname="System";Newest=10; Entrytype="Error"} -pscomputername $computers -asjob
  27. Workflow Sessions Run Workflows from within a special PSSession Workflows can be monitored and managed from within the session Great for long running workflows or those that might be suspended and resumed Session can be disconnected and reconnected
  28. Best Practices Try to test locally and interactively Use Write-Verbose for tracing and troubleshooting Use a workflow-aware editor like the PowerShell ISE Take advantage of parallelism Plan your activities…this is not just another way to script
  29. Resources The Lonely Administrator (http://jdhitsolutions.com/blog) PowerShell in Depth http://PowerShell.org http://blogs.technet.com/b/heyscriptingguy
  30. Thank You http://jdhitsolutions.com/blog jhicks@jdhitsolutions.com @JeffHicks http://plus.google.com/+JefferyHicks
Publicidad