Create Notebook from sql file.ps1 8.2 KB

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