Advertisement
Guest User

Cognos Analytics object export

a guest
Jan 5th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. #
  2. # extract report specifications from the Cognos Content Store database
  3. # save the report specs to the file system
  4. #
  5.  
  6. #$d = date
  7. #$dnum = $d.tostring("yyyyMMdd")
  8. $dlog = (date).tostring("yyyy-MM-dd HH:mm:ss.fff ")
  9. $dow = (date).DayofWeek.value__
  10. $dow = 0 #temporarily set the day to always be Sunday
  11.  
  12. "$dlog Content Store export beginning" | Out-File -append "C:\ReportBackup\logs\ReportBackupLog.txt"
  13. Write-Host "$dlog Content Store export beginning"
  14.  
  15.  
  16. $sqlquery = "with cte2(CMID,
  17. ObjectName,
  18. ObjectPath,
  19. ObjectClass,
  20. Modified) as
  21. (
  22. select o.CMID
  23. , n.NAME
  24. , cast(
  25. replace(
  26. replace(
  27. replace(
  28. replace(
  29. replace(
  30. replace(
  31. replace(
  32. replace(
  33. replace(
  34. replace(
  35. replace(
  36. n.NAME, char(151), '-' -- em dash
  37. ), char(150), '-' -- en dash
  38. ), char(60), '' -- <
  39. ), char(62), '' -- >
  40. ), char(58), '' -- :
  41. ), char(34), '' -- quote
  42. ), char(47), '' -- /
  43. ), char(92), '' -- \
  44. ), char(124), '' -- |
  45. ), char(63), '' -- ?
  46. ), char(42), '' -- *
  47. ) as varchar(max))
  48. , cast(c.NAME as varchar(max))
  49. , o.MODIFIED
  50. from CMOBJECTS o
  51. inner join CMOBJNAMES n on n.CMID = o.CMID
  52. inner join CMCLASSES c on c.CLASSID = o.CLASSID
  53. where n.ISDEFAULT = 1
  54. and isdate(n.NAME) = 0
  55. and o.CMID = 2
  56.  
  57. union all
  58. select o.CMID
  59. , n.NAME
  60. , cast(cte2.ObjectPath + '/' +
  61. replace(
  62. replace(
  63. replace(
  64. replace(
  65. replace(
  66. replace(
  67. replace(
  68. replace(
  69. replace(
  70. replace(
  71. replace(
  72. n.NAME, char(151), '-'
  73. ), char(150), '-'
  74. ), char(60), ''
  75. ), char(62), ''
  76. ), char(58), ''
  77. ), char(34), ''
  78. ), char(47), ''
  79. ), char(92), ''
  80. ), char(124), ''
  81. ), char(63), ''
  82. ), char(42), ''
  83. ) as varchar(max))
  84. , cast(c.NAME as varchar(max))
  85. , o.MODIFIED
  86. from CMOBJECTS o
  87. inner join CMOBJNAMES n on n.CMID = o.CMID
  88. inner join CMCLASSES c on c.CLASSID = o.CLASSID
  89. inner join cte2 cte2 on cte2.CMID = o.PCMID
  90. where n.ISDEFAULT = 1
  91. and n.CMID != 0
  92. )
  93.  
  94. select REPLACE(c.ObjectPath,'/','\') + case when c.ObjectClass = 'exploration' then '.json' else '.xml' end as FilePath
  95. , s.SPEC as SPEC
  96. from cte2 c
  97. left join CMOBJPROPS7 s on s.CMID = c.CMID
  98. where c.ObjectClass in ('report', 'exploration', 'dataSet2')
  99. and c.ObjectPath like 'Team Content/Reports%'"
  100.  
  101. # If it's Sunday, don't filter the results. Get the entire Content Store.
  102. if ($dow -ne 0) {
  103. $sqlquery = $sqlquery + "
  104. and c.Modified > dateadd(day, -3, getdate())"
  105. }
  106.  
  107. $sqlquery = $sqlquery + "
  108. order by 1"
  109.  
  110. #$sqlquery | Out-File -append "C:\ReportBackup\logs\ReportBackupLog.txt"
  111.  
  112. push-location
  113. try {
  114. $result = Invoke-Sqlcmd $sqlquery -ServerInstance "<SERVERNAME>" -Database "IBMCOGNOS" -MaxCharLength 2000000 -ErrorAction 'Stop' -QueryTimeout 900
  115. }
  116. catch {
  117. pop-location
  118. $dlog = (date).tostring("yyyy-MM-dd HH:mm:ss.fff ")
  119. "$dlog Error: $($_.Exception.Message)" | Out-File -append "C:\ReportBackup\logs\ReportBackupLog.txt" #write the error to the log
  120. Write-Host "$dlog Error: $($_.Exception.Message)"
  121. break #quit the program without making changes
  122. }
  123.  
  124. pop-location
  125.  
  126. $l = $result.length
  127.  
  128. $dlog = (date).tostring("yyyy-MM-dd HH:mm:ss.fff ")
  129. if ($dow -ne 0) {
  130. "$dlog $l reports modified" | Out-File -append "C:\ReportBackup\logs\ReportBackupLog.txt"
  131. Write-Host "$dlog $l reports modified"
  132. }
  133.  
  134. # If it's Sunday, delete everything under Team Content\Reports.
  135. # This is to delete items that are no longer in the Content Store.
  136. if ($dow -eq 0) {
  137. "$dlog $l reports total" | Out-File -append "C:\ReportBackup\logs\ReportBackupLog.txt"
  138. Remove-Item -path "C:\Users\pulsed\repos\CognosReports\Team Content\Reports" -recurse
  139. }
  140.  
  141.  
  142. # Write the results to the file system
  143. foreach($row in $result)
  144. {
  145. $filepath = "C:\Users\pulsed\repos\CognosReports\"+$row.FilePath
  146. $contents = $row.SPEC
  147.  
  148. try {
  149. if(!(Test-Path $filepath))
  150. {
  151. New-Item $filepath -ItemType file -Force -Value $contents
  152. }
  153. else
  154. {
  155. Set-Content -Path $filepath -Value $contents
  156. }
  157. }
  158. catch {
  159. "Error: " + $row.FilePath | Out-File -append "C:\ReportBackup\logs\ReportBackupLog.txt"
  160. " " + $_.Exception.Message | Out-File -append "C:\ReportBackup\logs\ReportBackupLog.txt"
  161. #" " + $_.Exception.ItemName | Out-File -append "C:\ReportBackup\logs\ReportBackupLog.txt"
  162. " " | Out-File -append "C:\ReportBackup\logs\ReportBackupLog.txt"
  163. Write-Host "Error: " + $row.FilePath
  164. Write-Host " " + $_.Exception.Message
  165. Write-Host " "
  166. }
  167. }
  168.  
  169. $dlog = (date).tostring("yyyy-MM-dd HH:mm:ss.fff ")
  170. "$dlog Content Store export complete" | Out-File -append "C:\ReportBackup\logs\ReportBackupLog.txt"
  171. Write-Host "$dlog Content Store export complete"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement