Convert-PowerPack2Ps1 by Dmitry Sotnikov 3 years ago (modification of post by Dmitry Sotnikov view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/822"></script>download | new post
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.
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
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository