PoshCode Logo PowerShell Code Repository

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

  1. #######################################################################
  2. # Convert-PowerPack2Ps1
  3. #
  4. # Converts PowerGUI .PowerPack files to ps1 PowerShell script library
  5. # v1 - raw conversion, no name changes, only script elements converted
  6. ######################################################################
  7. # Example:
  8. # & .\Convert-PowerPack2Ps1.ps1 "ActiveDirectory.powerpack" "ActiveDirectory.ps1"
  9. # . .\ActiveDirectory.ps1
  10. # Get-QADUser 'Dmitry Sotnikov' | MemberofRecursive
  11. ######################################################################
  12. #
  13. # (c) Dmitry Sotnikov
  14. #  http://dmitrysotnikov.wordpress.com
  15. #
  16. #####################################################################
  17. param(
  18.         $PowerPackFile = $(throw 'Please supply  path to source powerpack file'),
  19.         $OutputFilePath = $(throw 'Please supply  path to output ps1 file')
  20. )
  21.  
  22. #region Functions
  23.  
  24. function IterateTree {
  25.         # processes all script nodes
  26.         param($segment)
  27.         if ( $segment.Type -like 'Script*' ) {
  28.                
  29.                 $name = $segment.name -replace ' |\(|\)', ''
  30.                 $code = $segment.script.PSBase.InnerText
  31.                
  32. @"
  33.  
  34. ########################################################################
  35. # Function: $name
  36. # Return type: $($segment.returntype)
  37. ########################################################################
  38. function $name {
  39. $code
  40. }
  41. "@ | Out-File $OutputFilePath -Append          
  42.                
  43.         }
  44.         # recurse folders
  45.         if ($segment.items.container -ne $null) {
  46.                 $segment.items.container | ForEach-Object { IterateTree $_ }
  47.         }
  48. }
  49.  
  50.  
  51. function Output-Link {
  52.         PROCESS {
  53.                 if ( $_.script -ne $null ) {
  54.                         $name = $_.name -replace ' |\(|\)', ''
  55.                         $code = $_.script.PSBase.InnerText
  56.  
  57. @"
  58.  
  59. ########################################################################
  60. # Function: $name
  61. # Input type: $($_.type)
  62. # Return type: $($_.returntype)
  63. ########################################################################
  64. function $name {
  65. $code
  66. }
  67. "@ | Out-File $OutputFilePath -Append          
  68.                 }
  69.         }
  70. }
  71.  
  72.  
  73. #endregion
  74.  
  75.  
  76. $sourcefile = Get-ChildItem $PowerPackFile
  77. if ($sourcefile -eq $null) { throw 'File not found' }
  78.        
  79. @"
  80. ########################################################################
  81. # Generated from: $PowerPackFile
  82. #   by Convert-PowerPack2Ps1 script
  83. #   on $(get-date)
  84. ########################################################################
  85. "@ | Out-File $OutputFilePath
  86.  
  87. $pp = [XML] (Get-Content $sourcefile)
  88.  
  89. @"
  90.  
  91. # Scripts generated from script nodes
  92. "@ | Out-File $OutputFilePath -Append
  93. IterateTree $pp.configuration.items.container[0]
  94.  
  95. @"
  96.  
  97. # Scripts generated from script links
  98. "@ | Out-File $OutputFilePath -Append
  99.  
  100. $pp.configuration.items.container[1].items.container |
  101.         where { $_.id -eq '481eccc0-43f8-47b8-9660-f100dff38e14' } | ForEach-Object {
  102.                 $_.items.item, $_.items.container | Output-Link
  103.         }
  104.  
  105.  
  106. @"
  107.  
  108. # Scripts generated from script actions
  109. "@ | Out-File $OutputFilePath -Append
  110.  
  111. $pp.configuration.items.container[1].items.container |
  112.         where { $_.id -eq '7826b2ed-8ae4-4ad0-bf29-1ff0a25e0ece' } | ForEach-Object {
  113.                 $_.items.item, $_.items.container | Output-Link
  114.         }

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.

Syntax highlighting:


Remember me