# encoding: ascii
# api: powershell
# title: Convert-PowerPack2Ps1
# description: Converts PowerGUI’s .PowerPack files into ps1 script files – each script node, link and action get represented as a function with the element’s name and its code inside. You can then dot-source the file and use the functions in your scripts or command line. Works both in PowerShell v1 and v2.
# version: 0.1
# type: script
# author: Dmitry Sotnikov
# license: CC0
# function: Output-Link
# x-poshcode-id: 818
# x-derived-from-id: 822
# x-archived: 2016-03-18T03:38:16
# x-published: 2009-01-21T15:35:00
#
# Example:
# & .\Convert-PowerPack2Ps1.ps1 “ActiveDirectory.powerpack” “ActiveDirectory.ps1”
# . .\ActiveDirectory.ps1
# Get-QADUser ‘Dmitry Sotnikov’ | MemberofRecursive
#
#######################################################################
# Convert-PowerPack2Ps1
#
# Converts PowerGUI .PowerPack files to ps1 PowerShell script library
# v1 - raw conversion, no name changes, only script elements converted
######################################################################
# Example:
# & .\Convert-PowerPack2Ps1.ps1 "ActiveDirectory.powerpack" "ActiveDirectory.ps1"
# . .\ActiveDirectory.ps1
# Get-QADUser 'Dmitry Sotnikov' | MemberofRecursive
######################################################################
#
# (c) Dmitry Sotnikov
# http://dmitrysotnikov.wordpress.com
#
#####################################################################
param(
$PowerPackFile = (throw 'Please supply path to source powerpack file'),
$OutputFilePath = (throw 'Please supply path to output ps1 file')
)
#region Functions
function IterateTree {
# processes all script nodes
param($segment)
if ( $segment.Type -like 'Script*' ) {
$name = $segment.name -replace ' |\(|\)', ''
$code = $segment.script.PSBase.InnerText
@"
########################################################################
# Function: $name
# Return type: $($segment.returntype)
########################################################################
function $name {
$code
}
"@ | Out-File $OutputFilePath -Append
}
# recurse folders
if ($segment.items.container -ne $null) {
$segment.items.container | ForEach-Object { IterateTree $_ }
}
}
function Output-Link {
PROCESS {
if ( $_.script -ne $null ) {
$name = $_.name -replace ' |\(|\)', ''
$code = $_.script.PSBase.InnerText
@"
########################################################################
# Function: $name
# Input type: $($_.type)
# Return type: $($_.returntype)
########################################################################
function $name {
$code
}
"@ | Out-File $OutputFilePath -Append
}
}
}
#endregion
$sourcefile = Get-ChildItem $PowerPackFile
if ($sourcefile -eq $null) { throw 'File not found' }
@"
########################################################################
# Generated from: $PowerPackFile
# by Convert-PowerPack2Ps1 script
# on $(get-date)
########################################################################
"@ | Out-File $OutputFilePath
$pp = [XML] (Get-Content $sourcefile)
@"
# Scripts generated from script nodes
"@ | Out-File $OutputFilePath -Append
IterateTree $pp.configuration.items.container[0]
@"
# Scripts generated from script links
"@ | Out-File $OutputFilePath -Append
$pp.configuration.items.container[1].items.container |
where { $_.id -eq '481eccc0-43f8-47b8-9660-f100dff38e14' } | ForEach-Object {
$_.items.item, $_.items.container | Output-Link
}
@"
# Scripts generated from script actions
"@ | Out-File $OutputFilePath -Append
$pp.configuration.items.container[1].items.container |
where { $_.id -eq '7826b2ed-8ae4-4ad0-bf29-1ff0a25e0ece' } | ForEach-Object {
$_.items.item, $_.items.container | Output-Link
}