|
| 1 | + <# |
| 2 | + .SYNOPSIS |
| 3 | + This module .SQL files to PSM1 modules. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + This file will convert all files with extension .SQL in BIN\DevUtils to SQLScript_<CollectorName>.psm1 files in BIN folder. |
| 7 | + All converted SQL files will be moved to _sql.txt files to avoid double conversion. |
| 8 | +
|
| 9 | + .INPUTS |
| 10 | + None. You can't pipe objects to Add-Extension. |
| 11 | +
|
| 12 | + .OUTPUTS |
| 13 | + All files of .sql extension converted to SQLScript_<collectorName>.psm1 |
| 14 | +
|
| 15 | + .EXAMPLE |
| 16 | + PS> ConvertSQL2PSM.ps1 |
| 17 | + |
| 18 | + |
| 19 | + .LINK |
| 20 | + Online documenation: https://mssql-support.visualstudio.com/SQL%20LogScout/_wiki/wikis/SQL-LogScout.wiki/110/Working-TSQL-Scripts |
| 21 | +
|
| 22 | + #> |
| 23 | + |
| 24 | + <# |
| 25 | + This function will take TSQL filename as input (tsql.sql) and convert it to a pouwershell module (psm1) file. |
| 26 | + The fill will containe a single function that will generate the tsql file. |
| 27 | +
|
| 28 | + The function name will be <fileName_without_extension>_Query |
| 29 | +
|
| 30 | + |
| 31 | + #> |
| 32 | + |
| 33 | +function convertFile2PSM ([String] $fileName) |
| 34 | +{ |
| 35 | + #clean the file from its extension, this will provide us with collector name |
| 36 | + $fileName = [System.IO.Path]::GetFileNameWithoutExtension($fileName) |
| 37 | + |
| 38 | + #since we run this script in bin\devutils we get he parent path to write output files to. |
| 39 | + $path = Split-Path $PSScriptRoot -Parent |
| 40 | + |
| 41 | + #adding .sql file extension to collector name |
| 42 | + $filepath = $fileName + ".sql" |
| 43 | + |
| 44 | + #output file name will be SQLScript_<collector name>.psm1 |
| 45 | + $outputName = $path + "\SQLScript_" + $fileName + ".psm1" |
| 46 | + |
| 47 | + #function name will be <collector name>_Query |
| 48 | + $functionName = $fileName + "_Query([Boolean] `$returnVariable = `$false)" |
| 49 | + |
| 50 | + # Read the content of the text file |
| 51 | + $content = Get-Content -Path $filePath -Raw |
| 52 | + |
| 53 | + # Escape special characters and double quotes |
| 54 | + $escapedContent = $content -replace '"', '`"' -replace '\$', '`$' |
| 55 | + |
| 56 | + |
| 57 | + $fileHeader = " |
| 58 | + function $functionName |
| 59 | + { |
| 60 | + Write-LogDebug `"Inside`" `$MyInvocation.MyCommand |
| 61 | +
|
| 62 | + [String] `$collectorName = `"" + $fileName +"`" |
| 63 | + [String] `$fileName = `$global:internal_output_folder + `$collectorName + `".sql`" |
| 64 | +
|
| 65 | + `$content = `" |
| 66 | + $escapedContent |
| 67 | + `" |
| 68 | +
|
| 69 | + if (`$true -eq `$returnVariable) |
| 70 | + { |
| 71 | + Write-LogDebug `"Returned variable without creating file, this maybe due to use of GUI to filter out some of the xevents`" |
| 72 | +
|
| 73 | + `$content = `$content -split `"``r``n`" |
| 74 | + return `$content |
| 75 | + } |
| 76 | +
|
| 77 | + if (-Not (Test-Path `$fileName)) |
| 78 | + { |
| 79 | + Set-Content -Path `$fileName -Value `$content |
| 80 | + } else |
| 81 | + { |
| 82 | + Write-LogDebug `"`$filName already exists, could be from GUI`" |
| 83 | + } |
| 84 | +
|
| 85 | + #check if command was successful, then add the file to the list for cleanup AND return collector name |
| 86 | + if (`$true -eq `$?) |
| 87 | + { |
| 88 | + `$global:tblInternalSQLFiles += `$collectorName |
| 89 | + return `$collectorName |
| 90 | + } |
| 91 | +
|
| 92 | + Write-LogDebug `"Failed to build SQL File `" |
| 93 | + Write-LogDebug `$fileName |
| 94 | +
|
| 95 | + #return false if we reach here. |
| 96 | + return `$false |
| 97 | +
|
| 98 | + } |
| 99 | + " |
| 100 | + #if output file exists, we can compare content before we write it again to disk |
| 101 | + [Boolean] $fileExist = Test-Path $outputName |
| 102 | + if ($true -eq $fileExist) |
| 103 | + { |
| 104 | + $origContent = Get-Content $outputName |
| 105 | + #remove trailing spaces. |
| 106 | + while ([string]::IsNullOrEmpty( $origContent[-1].Trim())) { |
| 107 | + $origContent = $origContent[0..($origContent.Length - 2)] |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + #if the existing file is the same as new file, do not write it to disk |
| 113 | + if (($true -eq $fileExist) -and ($origContent.Equals($fileHeader))) |
| 114 | + { |
| 115 | + Write-Host "$fileName has the same content, no writing is done" |
| 116 | + } |
| 117 | + else { |
| 118 | + # Output the escaped content |
| 119 | + Set-Content -Path $outputName -Value $fileHeader |
| 120 | + write-host "Converted $outputName" |
| 121 | + $newFileName = $fileName + "_sql.txt" |
| 122 | + Move-Item -Path $filepath -Destination $newFileName -Force |
| 123 | + } |
| 124 | + |
| 125 | +} #end of convertFile2PSM |
| 126 | + |
| 127 | +#Main body of the script, get a colleection of all .sql files present in BIN\DevUtils folder. |
| 128 | +$files = Get-ChildItem -Path . -Filter *.sql | Where-Object { $_.Name -notMatch "del_*" } |
| 129 | + |
| 130 | + |
| 131 | +if ($null -eq $files) { |
| 132 | + Write-Host "No files to process" |
| 133 | + exit |
| 134 | +} |
| 135 | +#Process each .sql file to creat .psm1 file |
| 136 | +foreach ($file in $files) |
| 137 | +{ |
| 138 | + Write-Host $file.Name |
| 139 | + convertFile2PSM -fileName $file.Name |
| 140 | + |
| 141 | +} |
| 142 | + |
| 143 | + |
0 commit comments