2022-01-12

Using sp_executesql with PowerShell

 The system extended stored procedure sp_executesql is recommended in general to execute dynamic SQL to protect from SQL injection attack. Using sp_execute in PowerShell is like using any other stored procedure in PowerShell. But I prefer to do it in ADO.NET instead of using dynamic CmdLets like Invoke-SqlCmd. The reason is that I have a rather tight control with the connectivity and can make the call really fast.

The example below is a test on if a database exists with the given name. I have chosen to implement the test with a COUNT() function call so that the result always is the same type (integer) no matter if the database exists or not. Also COUNT() is usually very effective in the statement execution.

First I give the database name to look for:

$DbName = 'SSISDB'

Then I prepare the call and set up the connection to the SQL Server installation:

$SqlInstanceName = '(local)\MSSQL2019'
$CnnStr = "Server=$SqlInstanceName;Integrated Security=SSPI"
$SqlStatement = 'SELECT COUNT(*) AS [db_count] FROM [master].[sys].[databases] WHERE [name]=@dbname;'
$SqlParameters = '@dbname sysname'
$CnnSql = New-Object System.Data.SqlClient.SqlConnection
$CnnSql.ConnectionString = $CnnStr
$CnnSql.Open()

Then I call sp_executesql with the query and get the result. I close the connection as soon as possible to release the session resources:

$CmdSql = New-Object System.Data.SqlClient.SqlCommand
$CmdSql.Connection = $CnnSql
$CmdSql.CommandText = '[master].[sys].[sp_executesql]'
$CmdSql.CommandType = [System.Data.CommandType]::StoredProcedure
$CmdSql.Parameters.Add('@stmt', [System.Data.SqlDbType]::NVarChar, -1).Value = $SqlStatement
$CmdSql.Parameters.Add('@params', [System.Data.SqlDbType]::NVarChar, 500).Value = $SqlParameters
$CmdSql.Parameters.Add('@dbname', [System.Data.SqlDbType]::NVarChar, 128).Value = $DbName
$Rdr = $CmdSql.ExecuteReader()
while ($Rdr.Read()) { $DbCount = $Rdr['db_count'] }
$Rdr.Close()
$CnnSql.Close()

Afterwards I can work with the result from the query:

"Db Count: $DbCount"

This example can be used for calling any stored procedure.

2022-01-07

Windows 11 on VMware Workstation

 With VMware Workstation version 16.2 it is prepared for Windows 11. There are more details in the blog post "Workstation 16.2 Now Available". But with the increased requirements for Windows 11 there are some configuration you have to do before installation.

  1. Set firmware type to UEFI and enable secure boot. The is required to enable TPM. The setting is in Options > Advanced: Firmware type.
  2. Set encryption on the virtual machine. This is required to enable TPM. The setting is in Options: Access Control. You have to generate a password for the VM, but that is quite straight forward.
  3. Add Trusted Platform Module (TPM). This is required by Windows 11.
With these configurations in place the Windows 11 installation is quite similar to the Windows 10 installation.

If you plan to use the Windows 11 installation for usage without internet access, you should complete the installation with a local offline account. This is done during the final parts of the installation where you are expected to log on.

2022-01-01

Top-ten posts 2021

Looking back at 2021 not only as another year with the Covid-19 pandemic. For various reasons it has only been a year with fewer posts than planned. 

The top-ten from 2021 is very much like the top-ten list from 2020 (link).

  1. SqlBulkCopy with PowerShell (2017-07-30)
  2. ISO 8601 date formatting using PowerShell (2008-08-14)
  3. DBCC CHECKDB with PowerShell (2014-10-09)
  4. xp_instance_regwrite syntax (2013-09-10)
  5. T-SQL formatted duration (2017-04-29)
  6. Audit Log for Analysis Services (2014-01-08)
  7. Start shared HTA with PowerShell (2012-09-13)
  8. SQL Server Agent schedule owner (2010-02-08)
  9. PowerShell MessageBox (2014-03-15)
  10. Windows Storage Spaces (2019-03-27)
The date on each post is the creation date. Some posts I have updated since the creation, and then the history is described in the post itself. The oldest post is more than ten years old, and that I personally find quite satisfying.

Still I do not have many readers, That is fine with me as the purpose of this blog is more a personal collection of refined notes. Looking at the number of views on each post there is a rather big difference. The #1 post has about 20 times more views than post #10.