document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. ###########################################################################################################################
  2. # This script will create an Excel file to auto document Ivanti Automation
  3. # It will create individual worksheets in the Excel file for:
  4. #       *Runbooks
  5. #       *Projects
  6. #       *Modules
  7. #       *Tasks
  8. #       *Parameters
  9. # Usage:
  10. # All you need to do is export the BuildingBlock from Ivanti Automation . After the Export you can run this script against the created XML file.
  11. #
  12. # Example:
  13. # Invoke-IVA-Documentation -exportpath "C:\\Temp" -BB "C:\\Temp\\runbook.xml" -excel "C:\\Temp\\runbook.xlsx" -CSV
  14. # This will turn the buildingblock xml into an Excel file and create CSV\'s from each worksheet.
  15. #
  16. # Example2:
  17. # Invoke-IVA-Documentation -exportpath "C:\\Temp" -BB "C:\\Temp\\runbook.xml" -excel "runbook.xlsx"
  18. # This will do the same as the first example except without creating CSV files.
  19. #
  20. # Example3:
  21. # Invoke-IVA-Documentation -exportpath "C:\\Temp" -BB "C:\\Temp\\runbook.xml" -excel "runbook.xlsx" -CSVSQL -CSVDelimiter ";" -SQLServer "DATABASE01\\INSTANCE01"
  22. # This will do the same as the first example including importing the data in a SQL database "IVAMDOC".
  23. #############################################################################################################################
  24.  
  25. function Invoke-IVA-Documentation {
  26.     [CmdletBinding()]
  27.     param (
  28.         [Parameter(Mandatory = $True)]
  29.         [string]$ExportPath,
  30.         [string]$BB,
  31.         [string]$Excel,
  32.         [Parameter(Mandatory = $false)]
  33.         [switch]$CSV,
  34.         [switch]$CSV2SQL,
  35.         [string]$CSVDelimiter,
  36.         [string]$SQLServer
  37.     )
  38.  
  39.     ### Get descriptions Ivanti Automation.
  40.     $workingdir = $exportpath
  41.     $Output = $Excel
  42.     $XML = $BB
  43.     [xml]$xml = Get-Content "$XML"
  44.     $Excelfile = "$workingdir\\$Output"
  45.  
  46.     ### Set database info for importing in SQL
  47.     $DBName = "IVAMDOC"
  48.     $Delimiter = $CSVDelimiter
  49.     $SQLInstance = $SQLServer
  50.  
  51.     $RunbookCSVFile = "$workingdir\\runbook.csv"
  52.     if (Test-Path $RunbookCSVFile) { Remove-Item -Path $RunbookCSVFile }
  53.     $RunbookJobsCSVFile = "$workingdir\\runbookjobs.csv"
  54.     if (Test-Path $RunbookJobsCSVFile) { Remove-Item -Path $RunbookJobsCSVFile }
  55.     $ProjectCSVFile = "$workingdir\\project.csv"
  56.     if (Test-Path $ProjectCSVFile) { Remove-Item -Path $ProjectCSVFile }
  57.     $ProjectModuleCSVFile = "$workingdir\\projectmodule.csv"
  58.     if (Test-Path $ProjectModuleCSVFile) { Remove-Item -Path $ProjectModuleCSVFile }
  59.     $ModulesCSVFile = "$workingdir\\modules.csv"
  60.     if (Test-Path $ModulesCSVFile) { Remove-Item -Path $ModulesCSVFile }
  61.     $TasksCSVFile = "$workingdir\\tasks.csv"
  62.     if (Test-Path $TasksCSVFile) { Remove-Item -Path $TasksCSVFile }
  63.     $ParametersCSVFile = "$workingdir\\parameters.csv"
  64.     if (Test-Path $ParametersCSVFile) { Remove-Item -Path $ParametersCSVFile }
  65.     if (Test-Path $Excelfile) { Remove-Item -Path $Excelfile }
  66.    
  67.     $EXmodule = Get-Module psexcel
  68.  
  69.     if (!$EXmodule) {
  70.         Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -scope CurrentUser
  71.         Install-Module PSExcel -Force -Scope CurrentUser
  72.     }
  73.  
  74.     Import-Module PSExcel -Force
  75.  
  76.     ## If CSV2SQL enabled, check whether database $DBName exists on SQL instance $SQLInstance. If so, delete the database, and create a fresh one.
  77.     if ($CSV2SQL) {
  78.         $DBAmodule = Get-Module dbatools
  79.  
  80.         if (!$DBAmodule) {
  81.             Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -scope CurrentUser
  82.             Install-Module dbatools -Force -Scope CurrentUser
  83.         }
  84.  
  85.         Import-Module dbatools -Force
  86.        
  87.         $ExistingDB = Find-DbaDatabase -SqlInstance . -Pattern $DBName -Exact | Select-Object -ExpandProperty Name
  88.        
  89.         if ($ExistingDB -eq $DBName) {
  90.             Remove-DbaDatabase -SqlInstance $SQLInstance -Database $DBName -Confirm:$false
  91.         }
  92.         New-DbaDatabase -SqlInstance $SQLInstance -Name $DBName
  93.     }
  94.    
  95.     ## Get information about the runbooks
  96.     $Runbooks = $xml.AutomationManager.buildingblock.runbooks.runbook.properties
  97.     $Runbooktable = @()
  98.     $Runbooktotal = @()
  99.     foreach ($Runbook in $Runbooks) {
  100.    
  101.         #DATA CSV
  102.         $runbooktable = New-Object PSobject
  103.         $runbooktable | Add-Member -Type NoteProperty -Name Name -Value $runbook.name
  104.         $runbooktable | Add-Member -Type NoteProperty -Name Guid -Value $runbook.guid
  105.         $runbooktable | Add-Member -Type NoteProperty -Name Enabled -Value $runbook.enabled
  106.         $runbooktable | Add-Member -Type NoteProperty -Name Description -Value $runbook.description
  107.         $runbooktable | Add-Member -Type NoteProperty -Name Projects -Value $runbook.properties.jobs.job.count
  108.         $Runbooktotal += $Runbooktable
  109.     }
  110.     if ($CSV -or $CSV2SQL) {
  111.         $Runbooktotal | Export-Csv -Append -NoTypeInformation -Delimiter \';\' -Path $RunbookCSVFile
  112.        
  113.         if ($CSV2SQL) {
  114.             Import-DbaCsv -Path $RunbookCSVFile -SqlInstance $SQLInstance -Database $DBName -Delimiter $Delimiter -AutoCreateTable
  115.         }
  116.     }
  117.     $Runbooktotal | Export-XLSX -Path $Excelfile -AutoFit -Table -TableStyle Medium2 -WorksheetName "Runbooks"
  118.  
  119.     ## Get information about the jobs in the runbook(s)
  120.     $Runbooks = $xml.AutomationManager.buildingblock.runbooks.runbook.properties
  121.     $RunbookJobstable = @()
  122.     $RunbookJobstotal = @()
  123.  
  124.     foreach ($Runbook in $Runbooks) {
  125.         $order = 0
  126.         $jobs = $Runbook.properties.jobs
  127.         foreach ($job in $jobs) {
  128.             $a = $jobs.job
  129.             foreach ($i in $a) {
  130.                 $order = $order + 1
  131.                 #DATA CSV
  132.                 $runbookjobstable = New-Object PSobject
  133.                 $runbookjobstable | Add-Member -Type NoteProperty -Name RunbookGuid -Value $runbook.guid
  134.                 $runbookjobstable | Add-Member -Type NoteProperty -Name Order -Value $order
  135.                 $runbookjobstable | Add-Member -Type NoteProperty -Name Guid -Value $i.properties.what.innertext
  136.                 $runbookjobstable | Add-Member -Type NoteProperty -Name Name -Value $i.properties.name
  137.                 $runbookjobstable | Add-Member -Type NoteProperty -Name Enabled -Value $i.properties.enabled
  138.                 $runbookjobstable | Add-Member -Type NoteProperty -Name JobType -Value $i.properties.what.type
  139.                 $RunbookJobstotal += $RunbookJobstable
  140.             }
  141.         }
  142.     }
  143.     if ($CSV -or $CSV2SQL) {
  144.         $RunbookJobstotal | Export-Csv -Append -NoTypeInformation -Delimiter \';\' -Path $RunbookJobsCSVFile
  145.         if ($CSV2SQL) {
  146.             Import-DbaCsv -Path $RunbookJobsCSVFile -SqlInstance $SQLInstance -Database $DBName -Delimiter $Delimiter -AutoCreateTable
  147.         }
  148.     }
  149.     $RunbookJobstotal | Export-XLSX -Path $Excelfile -AutoFit -Table -TableStyle Medium2 -WorksheetName "RunbookJobs"
  150.  
  151.     ## Get information about Projects, if there is any
  152.     $Projects = $xml.AutomationManager.buildingblock.projects.project.properties
  153.     $Projecttable = @()
  154.     $Projecttotal = @()
  155.  
  156.     if ($Projects.count -gt 0)
  157.     {        
  158.         foreach ($Project in $Projects) {
  159.    
  160.             #DATA CSV
  161.             $Projecttable = New-Object PSobject
  162.             $Projecttable | Add-Member -Type NoteProperty -Name Name -Value $Project.name
  163.             $Projecttable | Add-Member -Type NoteProperty -Name Guid -Value $Project.guid
  164.             $Projecttable | Add-Member -Type NoteProperty -Name Enabled -Value $Project.enabled
  165.             $Projecttable | Add-Member -Type NoteProperty -Name Description -Value $Project.description
  166.             $Projecttotal += $Projecttable
  167.         }
  168.  
  169.         if ($CSV -or $CSV2SQL) {
  170.             $Projecttotal | Export-Csv -Append -NoTypeInformation -Delimiter \';\'-Path $ProjectCSVFile
  171.             if ($CSV2SQL) {
  172.                 Import-DbaCsv -Path $ProjectCSVFile -SqlInstance $SQLInstance -Database $DBName -Delimiter $Delimiter -AutoCreateTable
  173.             }
  174.         }
  175.         $Projecttotal | Export-XLSX -Path $Excelfile -AutoFit -Table -TableStyle Medium2 -WorksheetName "Projects"
  176.     }
  177.  
  178.     ## Get information about Modules in the Project(s), if there is any Project
  179.     $Projects = $xml.AutomationManager.buildingblock.projects.project
  180.     $ProjectModuletable = @()
  181.     $ProjectModuletotal = @()
  182.  
  183.     if ($Projects.count -gt 0)
  184.     {        
  185.         foreach ($Project in $Projects) {
  186.             $order = 0
  187.             $modules = $Project.modules
  188.             foreach ($module in $modules) {
  189.                 $a = $modules.module
  190.                 foreach ($i in $a) {
  191.                     $order = $order + 1
  192.                     #DATA CSV
  193.                     $ProjectModuletable = New-Object PSobject
  194.                     $ProjectModuletable | Add-Member -Type NoteProperty -Name ProjectGuid -Value $Project.properties.guid
  195.                     $ProjectModuletable | Add-Member -Type NoteProperty -Name Order -Value $order
  196.                     $ProjectModuletable | Add-Member -Type NoteProperty -Name ModuleGuid -Value $i.guid
  197.                     $ProjectModuletable | Add-Member -Type NoteProperty -Name Enabled -Value $i.enabled
  198.                     $ProjectModuletotal += $ProjectModuletable
  199.                 }
  200.             }
  201.         }
  202.  
  203.         if ($CSV -or $CSV2SQL) {
  204.             $ProjectModuletotal | Export-Csv -Append -NoTypeInformation -Delimiter \';\'-Path $ProjectModuleCSVFile
  205.             if ($CSV2SQL) {
  206.                 Import-DbaCsv -Path $ProjectModuleCSVFile -SqlInstance $SQLInstance -Database $DBName -Delimiter $Delimiter -AutoCreateTable
  207.             }
  208.         }
  209.         $ProjectModuletotal | Export-XLSX -Path $Excelfile -AutoFit -Table -TableStyle Medium2 -WorksheetName "ProjectModules"
  210.     }
  211.  
  212.     ## Get information about Modules, if there is any
  213.     $Modules = $xml.AutomationManager.buildingblock.modules.module
  214.     $Moduletable = @()
  215.     $Moduletotal = @()
  216.  
  217.     if ($Modules.count -gt 0)
  218.     {        
  219.         foreach ($Module in $Modules) {
  220.    
  221.             #DATA CSV
  222.             $Moduletable = New-Object PSobject
  223.             $Moduletable | Add-Member -Type NoteProperty -Name Name -Value $Module.Properties.name
  224.             $Moduletable | Add-Member -Type NoteProperty -Name Guid -Value $Module.Properties.guid
  225.             $Moduletable | Add-Member -Type NoteProperty -Name Enabled -Value $Module.Properties.Enabled
  226.             $Moduletable | Add-Member -Type NoteProperty -Name Description -Value $Module.properties.description
  227.             $Moduletotal += $Moduletable
  228.         }
  229.         if ($CSV -or $CSV2SQL) {
  230.             $Moduletotal | Export-Csv -Append -NoTypeInformation -Delimiter \';\' -Path $ModulesCSVFile
  231.             if ($CSV2SQL) {
  232.                 Import-DbaCsv -Path $ModulesCSVFile -SqlInstance $SQLInstance -Database $DBName -Delimiter $Delimiter -AutoCreateTable
  233.             }
  234.         }
  235.         $Moduletotal | Export-XLSX -Path $Excelfile -AutoFit -Table -TableStyle Medium2 -WorksheetName "Modules"
  236.     }
  237.  
  238.     ## Get information about Tasks in Module(s), if there is any module
  239.     $Modules = $xml.AutomationManager.buildingblock.modules.module
  240.     $Tasktable = @()
  241.     $Tasktotal = @()
  242.  
  243.     if ($Modules.count -gt 0)
  244.     {        
  245.         foreach ($Module in $Modules) {
  246.             $order = 0    
  247.             $tasks = $module.tasks
  248.             foreach ($task in $tasks) {
  249.                 $a = $tasks.task
  250.                 foreach ($i in $a) {
  251.                     if ($i.properties)
  252.                     {
  253.                         $order = $order + 1
  254.                         $Tasktable = New-Object PSobject
  255.                         $Tasktable | Add-Member -Type NoteProperty -Name "Part of Module" -Value $Module.Properties.name
  256.                         $Tasktable | Add-Member -Type NoteProperty -Name "Part of Module (GUID)" -Value $Module.Properties.guid
  257.                         $Tasktable | Add-Member -Type NoteProperty -Name Order -Value $order
  258.                         $Tasktable | Add-Member -Type NoteProperty -Name Type -Value $i.properties.type
  259.                         $Tasktable | Add-Member -Type NoteProperty -Name Enabled -Value $i.properties.enabled
  260.                         $Tasktable | Add-Member -Type NoteProperty -Name Description -Value $i.properties.description
  261.                         $Tasktable | Add-Member -Type NoteProperty -Name Comment -Value $i.properties.comments
  262.                         $Tasktable | Add-Member -Type NoteProperty -Name Details -Value $i.settings.OuterXml
  263.                         $Tasktotal += $Tasktable
  264.                     }
  265.                 }
  266.        
  267.             }
  268.  
  269.         }
  270.         if ($CSV -or $CSV2SQL) {
  271.             $Tasktotal | Export-Csv -Append -NoTypeInformation -Delimiter \';\' -Path $TasksCSVFile
  272.             if ($CSV2SQL) {
  273.                 Import-DbaCsv -Path $TasksCSVFile -SqlInstance $SQLInstance -Database $DBName -Delimiter $Delimiter -AutoCreateTable
  274.             }
  275.         }
  276.         $Tasktotal | Export-XLSX -Path $Excelfile -AutoFit -Table -TableStyle Medium2 -WorksheetName "Tasks"
  277.     }
  278.  
  279.     ## Get information about Runbook Parameters
  280.     $Parameters = $xml.AutomationManager.buildingblock.runbooks.runbook.properties.properties.parameters.param
  281.     $Parametertable = @()
  282.     $Parametertotal = @()
  283.  
  284.     if ($Parameters.count -gt 0)
  285.     {        
  286.         foreach ($Parameter in $Parameters) {
  287.    
  288.             #DATA CSV
  289.             $Parametertable = New-Object PSobject
  290.             $Parametertable | Add-Member -Type NoteProperty -Name Name -Value $Parameter.name
  291.             $Parametertable | Add-Member -Type NoteProperty -Name Value1 -Value $Parameter.Value1
  292.             $Parametertable | Add-Member -Type NoteProperty -Name Value2 -Value $Parameter.Value2
  293.             $Parametertable | Add-Member -Type NoteProperty -Name Value3 -Value $Parameter.Value3
  294.             $Parametertable | Add-Member -Type NoteProperty -Name Description -Value $Parameter.description
  295.             $Parametertotal += $Parametertable
  296.         }
  297.         if ($CSV -or $CSV2SQL) {
  298.             $Parametertotal | Export-Csv -Append -NoTypeInformation -Delimiter \';\' -Path $ParametersCSVFile
  299.             if ($CSV2SQL) {
  300.                 Import-DbaCsv -Path $ParametersCSVFile -SqlInstance $SQLInstance -Database $DBName -Delimiter $Delimiter -AutoCreateTable
  301.             }
  302.         }
  303.         $Parametertotal | Export-XLSX -Path $Excelfile -AutoFit -Table -TableStyle Medium2 -WorksheetName "Parameters"
  304.     }
  305. }
  306.  
  307. Invoke-IVA-Documentation -ExportPath "<PATH WHERE THE OUTPUT WILL BE SAVED>" -BB "<FULL PATH TO BUILDINGBLOCK.XML>" -Excel "<OUTPUTNAME.XLSX>" -CSV
');