Created
October 15, 2017 11:43
-
-
Save indented-automation/9f2f021eab68677ed18f6d761bb1a80e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Playing around with: https://blogs.msdn.microsoft.com/powershell/2007/03/03/start-demo-help-doing-demos-using-powershell/ | |
# WIP. | |
function Get-DemoCommand { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[String]$Path | |
) | |
$Path = $pscmdlet.GetUnresolvedProviderPathFromPSPath($Path) | |
$tokens = $errors = @() | |
$ast = [System.Management.Automation.Language.Parser]::ParseFile( | |
$Path, | |
[Ref]$tokens, | |
[Ref]$errors | |
) | |
foreach ($statement in $ast.EndBlock.Statements) { | |
if ($statement.PipelineElements -and $statement.PipelineElements[0].Expression -is [System.Management.Automation.Language.ScriptBlockExpressionAst]) { | |
$statement.PipelineElements[0].Expression.ScriptBlock.GetScriptBlock() | |
} else { | |
$statement.Extent.Text | |
} | |
} | |
if ($errors.Count -gt 0) { | |
$errors | Write-Error | |
throw 'Demo script has errors!' | |
} | |
} | |
filter Invoke-DemoCommand { | |
param ( | |
[Parameter(ValueFromPipeline)] | |
[Object]$Command | |
) | |
if ($Command -is [String]) { | |
Invoke-Expression $Command | Out-String | Write-Host | |
} elseif ($Command -is [ScriptBlock]) { | |
& $Command | Out-String | Write-Host | |
} | |
Write-Host | |
} | |
filter Type-DemoCommand { | |
param ( | |
[Parameter(ValueFromPipeline)] | |
[Object]$Command | |
) | |
Write-Host 'PS> ' -NoNewLine | |
if ($Command -is [ScriptBlock]) { | |
Write-Host '& {' -NoNewLine | |
} | |
foreach ($char in $Command.ToString().ToCharArray()) { | |
if ($char -eq "`r") { | |
# Discard always | |
} | |
if ($char -eq "`n") { | |
Write-Host | |
} | |
Write-Host $char -NoNewLine | |
Start-Sleep -Milliseconds 60 | |
} | |
if ($Command -is [ScriptBlock]) { | |
Write-Host '}' -NoNewLine | |
} | |
Write-Host | |
$Command | |
} | |
$commands = Get-DemoCommand C:\temp\demo.ps1 | |
for ($i = 0; $i -lt $commands.Count; $i++) { | |
# Input handler goes here | |
$commands[$i] | Type-DemoCommand | Invoke-DemoCommand | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment