#Requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Start-TypedDemo.ps1 # Version: 0.9 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # Date: 5/3/2011 # Keywords: # Comments: # # "Those who forget to script are doomed to repeat their work." # # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** # ----------------------------------------------------------------------------- Function Start-TypedDemo { <# .SYNOPSIS Simulate a PowerShell session .DESCRIPTION This command simulates an interactive PowerShell session. It will process a text file of PowerShell commands. EACH COMMAND MUST BE A ONE LINE EXPRESSION. The function will insert your prompt and "type" out each command when you press any key. At the end of the typed command or whenever a pipe character is inserted, the script will pause. Press Enter or any key to continue. If it is the end of the command pressing Enter will executew the command. Use the -NoExecute parameter to run through the demo without executing any commands. Commented lines will be skipped. The Pause parameter is the "typing" speed interval in milliseconds. The default is 100. Press 'q' or ESC at any pause to quit the demo. This function will NOT run in the ISE. .PARAMETER File The file name of PowerShell commands to execute .PARAMETER Pause The typing speed interval between characters in milliseconds. The default is 100. .PARAMETER NoExecute Do not execute any of the commands. .EXAMPLE PS C:\> Start-TypedDemo c:\work\demo.txt Run the commands in c:\work\demo.txt .NOTES NAME : Start-TypedDemo VERSION : 0.9 LAST UPDATED: 5/3/2011 AUTHOR : SERENITY\Jeff .LINK http://jdhitsolutions.com/blog/2011/05/start-typeddemo/ .LINK Write-Host Invoke-Command .INPUTS None .OUTPUTS None #> Param( [Parameter(Position=0,Mandatory=$True,HelpMessage="Enter the name of a text file with your demo commands")] [ValidateScript({Test-Path $_})] [string]$File, [ValidateScript({$_ -gt 0})] [int]$Pause=100, [switch]$NoExecute ) #this is an internal function so I'm not worried about the name Function PauseIt { Param() #wait for a key press $Running=$true #keep looping until a key is pressed While ($Running) { if ($host.ui.RawUi.KeyAvailable) { $key = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown") if ($key) { $Running=$False #check the value and if it is q or ESC, then bail out if ($key -match "q|27") { Write-Host `r #Exit Return "quit" } } } start-sleep -millisecond 100 } #end While } #PauseIt function #abort if running in the ISE if ($host.name -match "PowerShell ISE") { Write-Warning "This will not work in the ISE. Use the PowerShell console host." Return } Clear-Host #strip out all comments $commands=Get-Content -Path $file | Where {-NOT $_.StartsWith("#")} $count=0 #write a prompt using your current prompt function Write-Host $(prompt) -NoNewline foreach ($command in $commands) { $count++ #pause until a key is pressed which will then process the next command $p=PauseIt If ($p -eq "quit") {Return} for ($i=0;$i -lt $command.length;$i++) { write-host $command[$i] -NoNewline Start-sleep -Milliseconds $Pause #if character is a pipe ( | ) pause in case an #explanation is needed if ($command[$i] -eq "|") { $p=PauseIt If ($p -eq "quit") {Return} } } #Pause until ready to run the command $p=PauseIt If ($p -eq "quit") {Return} Write-host `n #execute the command unless -NoExecute was specified if (-NOT $NoExecute) { Invoke-Expression $command | Out-Default } #reset the prompt unless we've just done the last command if ($count -lt $commands.count) { Write-Host $(prompt) -NoNewline } } #foreach } #function #uncomment if you want the alias #Set-Alias -Name std -Value Start-TypedDemo