Create Notebook from sql file.ps1 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ## This scipt requires the ADSNotebook module greater version than 0.0.20191119.1 which can be installed with
  2. # Install-Module ADSNotebook
  3. $RawSql = Get-Content .\BPCheck\Check_BP_Servers.sql -Raw
  4. $SplitSQL = $RawSql -split '--------------------------------------------------------------------------------------------------------------------------------'
  5. $GlobalVariables = @"
  6. -- These are the variables that are required for the Azure Data Studio Notebook to function in the same way as the stored procedure. Unfortunately, it is easier to add them to the beginning of each code block.
  7. SET NOCOUNT ON;
  8. SET ANSI_WARNINGS ON;
  9. SET QUOTED_IDENTIFIER ON;
  10. -- Declare Global Variables
  11. DECLARE @UpTime VARCHAR(12),@StartDate DATETIME
  12. DECLARE @agt smallint, @ole smallint, @sao smallint, @xcmd smallint
  13. DECLARE @ErrorSeverity int, @ErrorState int, @ErrorMessage NVARCHAR(4000)
  14. DECLARE @CMD NVARCHAR(4000)
  15. DECLARE @path NVARCHAR(2048)
  16. DECLARE @osver VARCHAR(5), @ostype VARCHAR(10), @osdistro VARCHAR(20), @server VARCHAR(128), @instancename NVARCHAR(128), @arch smallint, @ossp VARCHAR(25), @SystemManufacturer VARCHAR(128), @BIOSVendor AS VARCHAR(128), @Processor_Name AS VARCHAR(128)
  17. DECLARE @existout int, @FSO int, @FS int, @OLEResult int, @FileID int
  18. DECLARE @FileName VARCHAR(200), @Text1 VARCHAR(2000), @CMD2 VARCHAR(100)
  19. DECLARE @src VARCHAR(255), @desc VARCHAR(255), @psavail VARCHAR(20), @psver tinyint
  20. DECLARE @dbid int, @dbname NVARCHAR(1000)
  21. DECLARE @sqlcmd NVARCHAR(max), @params NVARCHAR(600)
  22. DECLARE @sqlmajorver int, @sqlminorver int, @sqlbuild int, @masterpid int, @clustered bit
  23. DECLARE @ptochecks int
  24. DECLARE @dbScope VARCHAR(256)
  25. DECLARE @port VARCHAR(15), @replication int, @RegKey NVARCHAR(255), @cpuaffin VARCHAR(300), @cpucount int, @numa int
  26. DECLARE @i int, @cpuaffin_fixed VARCHAR(300), @affinitymask NVARCHAR(64), @affinity64mask NVARCHAR(1024)--, @cpuover32 int
  27. DECLARE @bpool_consumer bit
  28. DECLARE @allow_xpcmdshell bit
  29. DECLARE @custompath NVARCHAR(500) = NULL
  30. DECLARE @affined_cpus int
  31. DECLARE @langid smallint
  32. DECLARE @lpim bit, @lognumber int, @logcount int
  33. DECLARE @query NVARCHAR(1000)
  34. DECLARE @diskfrag bit
  35. DECLARE @accntsqlservice NVARCHAR(128)
  36. DECLARE @maxservermem bigint, @systemmem bigint
  37. DECLARE @mwthreads_count int
  38. DECLARE @ifi bit
  39. DECLARE @duration tinyint
  40. DECLARE @adhoc smallint
  41. DECLARE @gen_scripts bit
  42. DECLARE @ixfrag bit
  43. DECLARE @ixfragscanmode VARCHAR(8)
  44. DECLARE @logdetail bit
  45. DECLARE @spn_check bit
  46. DECLARE @dbcmptlevel int
  47. -- With the variables declared we then set them. You can alter these values for different checks. The instructions will show where you should do this.
  48. -- Set @dbScope to the appropriate list of database IDs if there's a need to have a specific scope for database specific checks.
  49. -- Valid input should be numeric value(s) between single quotes, as follows: '1,6,15,123'
  50. -- Leave NULL for all databases
  51. SELECT @dbScope = NULL -- (NULL = All DBs; '<database_name>')
  52. -- Set @ptochecks to OFF if you want to skip more performance tuning and optimization oriented checks.
  53. SELECT @ptochecks = 1 -- 1 for ON 0 for OFF
  54. -- Set @duration to the number of seconds between data collection points regarding perf counters, waits and latches. -- Duration must be between 10s and 255s (4m 15s), with a default of 90s.
  55. SELECT @duration = 90
  56. -- Set @logdetail to OFF if you want to get just the summary info on issues in the Errorlog, rather than the full detail.
  57. SELECT @logdetail = 0 --(1 = ON; 0 = OFF)
  58. -- Set @diskfrag to ON if you want to check for disk physical fragmentation.
  59. -- Can take some time in large disks. Requires elevated privileges.
  60. -- See https://support.microsoft.com/help/3195161/defragmenting-sql-server-database-disk-drives
  61. SELECT @diskfrag = 0 --(1 = ON; 0 = OFF)
  62. -- Set @ixfrag to ON if you want to check for index fragmentation.
  63. -- Can take some time to collect data depending on number of databases and indexes, as well as the scan mode chosen in @ixfragscanmode.
  64. SELECT @ixfrag = 0 --(1 = ON; 0 = OFF)
  65. -- Set @ixfragscanmode to the scanning mode you prefer.
  66. -- More detail on scanning modes available at https://docs.microsoft.com/sql/relational-databases/system-dynamic-management-views/sys-dm-db-index-physical-stats-transact-sql
  67. SELECT @ixfragscanmode = 'LIMITED' --(Valid inputs are DEFAULT, NULL, LIMITED, SAMPLED, or DETAILED. The default (NULL) is LIMITED)
  68. -- Set @bpool_consumer to OFF if you want to list what are the Buffer Pool Consumers from Buffer Descriptors.
  69. -- Mind that it may take some time in servers with large caches.
  70. SELECT @bpool_consumer = 1 -- 1 for ON 0 for OFF
  71. -- Set @spn_check to OFF if you want to skip SPN checks.
  72. SELECT @spn_check = 0 --(1 = ON; 0 = OFF)
  73. -- Set @gen_scripts to ON if you want to generate index related scripts.
  74. -- These include drops for Duplicate, Redundant, Hypothetical and Rarely Used indexes, as well as creation statements for FK and Missing Indexes.
  75. SELECT @gen_scripts = 0 -- 1 for enable 0 for disable
  76. -- Set @allow_xpcmdshell to OFF if you want to skip checks that are dependant on xp_cmdshell.
  77. -- Note that original server setting for xp_cmdshell would be left unchanged if tests were allowed.
  78. SELECT @allow_xpcmdshell = 1 -- 1 for enable 0 for disable
  79. -- Set @custompath below and set the custom desired path for .ps1 files.
  80. -- If not, default location for .ps1 files is the Log folder.
  81. SELECT @custompath = NULL
  82. -- These values are gathered for when they are needed
  83. SELECT @langid = lcid FROM sys.syslanguages WHERE name = @@LANGUAGE
  84. SELECT @adhoc = CONVERT(bit, [value]) FROM sys.configurations WHERE [Name] = 'optimize for ad hoc workloads';
  85. SELECT @masterpid = principal_id FROM master.sys.database_principals (NOLOCK) WHERE sid = SUSER_SID()
  86. SELECT @instancename = CONVERT(VARCHAR(128),SERVERPROPERTY('InstanceName'))
  87. SELECT @server = RTRIM(CONVERT(VARCHAR(128), SERVERPROPERTY('MachineName')))
  88. SELECT @sqlmajorver = CONVERT(int, (@@microsoftversion / 0x1000000) & 0xff)
  89. SELECT @sqlminorver = CONVERT(int, (@@microsoftversion / 0x10000) & 0xff)
  90. SELECT @sqlbuild = CONVERT(int, @@microsoftversion & 0xffff)
  91. SELECT @clustered = CONVERT(bit,ISNULL(SERVERPROPERTY('IsClustered'),0))
  92. -- There are some variables that get passed from one check to another. This is easy in the stored procedure but won't work in the Notebook so we have to create a table in tempdb and read them from there
  93. IF NOT EXISTS (SELECT [object_id]
  94. FROM tempdb.sys.objects (NOLOCK)
  95. WHERE [object_id] = OBJECT_ID('tempdb.dbo.dbvars'))
  96. BEGIN
  97. CREATE TABLE tempdb.dbo.dbvars(VarName VarChar(256),VarValue VarChar(256))
  98. END
  99. SELECT @ostype = (SELECT VarValue FROM tempdb.dbo.dbvars WHERE VarName = 'ostype');
  100. SELECT @osver = (SELECT VarValue FROM tempdb.dbo.dbvars WHERE VarName = 'osver');
  101. SELECT @affined_cpus = (SELECT VarValue FROM tempdb.dbo.dbvars WHERE VarName = 'affined_cpus');
  102. SELECT @psavail = (SELECT VarValue FROM tempdb.dbo.dbvars WHERE VarName = 'psavail');
  103. SELECT @accntsqlservice = (SELECT VarValue FROM tempdb.dbo.dbvars WHERE VarName = 'accntsqlservice');
  104. SELECT @maxservermem = (SELECT VarValue FROM tempdb.dbo.dbvars WHERE VarName = 'maxservermem');
  105. SELECT @systemmem = (SELECT VarValue FROM tempdb.dbo.dbvars WHERE VarName = 'systemmem');
  106. SELECT @mwthreads_count = (SELECT VarValue FROM tempdb.dbo.dbvars WHERE VarName = 'mwthreads_count');
  107. SELECT @ifi = (SELECT VarValue FROM tempdb.dbo.dbvars WHERE VarName = 'ifi');
  108. IF @sqlmajorver > 10
  109. BEGIN
  110. DECLARE @IsHadrEnabled tinyint
  111. SELECT @IsHadrEnabled = CASE WHEN SERVERPROPERTY('EngineEdition') = 8 THEN 1 ELSE CONVERT(tinyint, SERVERPROPERTY('IsHadrEnabled')) END;
  112. END
  113. -- The T-SQL for the Check starts below
  114. "@
  115. # We don't need the first or the last as they are only required for the sp
  116. $Cells = foreach ($Chunk in $SplitSQL[1..($SplitSQL.Length -2)]) {
  117. if ($Chunk.Trim().StartsWith('--- #sponly#')) {
  118. # Ignore this tag
  119. }
  120. elseif ($Chunk.trim().StartsWith('---')) {
  121. ## This is a text block
  122. $MarkDown = $Chunk.Trim().Replace('-- ', '').replace('*/','')
  123. New-ADSWorkBookCell -Type Text -Text $MarkDown
  124. }
  125. else {
  126. ## This is a code block
  127. try {
  128. $Code = $GlobalVariables + $Chunk.Trim()
  129. New-ADSWorkBookCell -Type Code -Text $Code -Collapse
  130. }
  131. catch {
  132. Write-Warning "Gah it went wrong"
  133. }
  134. }
  135. }
  136. # Create the notebook
  137. New-ADSWorkBook -Type SQL -Path .\BPCheck\DynamicBPCheck.ipynb -cells $Cells
  138. # Open the notebook
  139. azuredatastudio.cmd .\BPCheck\DynamicBPCheck.ipynb