Skip to content

Instantly share code, notes, and snippets.

@trondhindenes
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trondhindenes/faeb6c1212f4a3e2457c to your computer and use it in GitHub Desktop.
Save trondhindenes/faeb6c1212f4a3e2457c to your computer and use it in GitHub Desktop.
SMA things
Function Process-RunbookFolder
{
Param (
$Path,
[switch]$recurse
)
$allwfs = get-childitem -Path $Path -Recurse:$recurse
$Global:referencelist = New-Object System.Collections.ArrayList
[System.Collections.ArrayList]$Global:ToProcessList = $allwfs | select -ExpandProperty Fullname
$Global:DoneProcessList = New-Object System.Collections.ArrayList
foreach ($wf in $ToProcessList)
{
#Validate-RunbookFile -path $wf.FullName -erroraction Stop
Get-RunbookReference -path $wf
Process-RunbookFile -path $wf -basepath $Path -recurse:$recurse
}
#get-variable processlist -Scope global | Remove-Variable
}
Function Get-RunbookReference
{
Param ($path)
$ThisWf = get-content $path -Raw
$ThisWfSB = [scriptblock]::Create($ThisWf)
#$ThisWfAST = $ThisWfSB.Ast
$TokenizedWF = [System.Management.Automation.PSParser]::Tokenize($ThisWfSB,[ref]$null)
$referencedCommands = $TokenizedWF | where {$_.Type -eq "Command"} | select -ExpandProperty "Content"
$myobj = "" |Select Fullname, ReferencedRunbooks
$myobj.Fullname =$path
$myobj.ReferencedRunbooks = $referencedCommands
$Global:referencelist += $myobj; $myobj = $null
}
Function Process-RunbookFile
{
Param ($path, $basepath, [switch]$recurse)
$path = get-item $path
if ($DoneProcessList -contains ($path.FullName))
{
Write-Verbose "SKIPPING: Already processed runbook $($path.BaseName)"
return
}
Write-Verbose "PARSING: Runbook $($path.BaseName)"
#Parse to find wfs this one depends on, and process
$ThisWf = get-content $path -Raw
$ThisWfSB = [scriptblock]::Create($ThisWf)
#$ThisWfAST = $ThisWfSB.Ast
$TokenizedWF = [System.Management.Automation.PSParser]::Tokenize($ThisWfSB,[ref]$null)
$referencedCommands = $TokenizedWF | where {$_.TYpe -eq "Command"}
foreach ($referencedCommand in $referencedCommands)
{
$runbookpath = get-childitem -Path $basepath -Recurse:$recurse |where {$_.BaseName -eq $referencedCommand.Content}
if ($runbookpath)
{
Write-Verbose "REFERENCE: $($path.BaseName)--> $($referencedCommand.content)"
Process-RunbookFile -path $runbookpath.FullName
}
}
#WHen all referenced runbooks are imported, import this one
Write-Verbose "PUBLISH: Publishing runbook $($path.BaseName)"
$Global:DoneProcessList += $path.FullName
}
Process-RunbookFolder -Path "D:\trond.hindenes\Desktop\wftest"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment