使用 powershell 更改 SQL 服务器的实例级排序规则

Change instance level collation of SQL Server using powershell

我想使用 powershell 脚本以编程方式更改 SQL 服务器实例的排序规则。以下是手动步骤:

  1. 停止 SQL 服务器实例
  2. 转到目录位置:"C:\Program Files\Microsoft SQL Server\MSSQL14.SQL2017\MSSQL\Binn"
  3. 执行以下命令:sqlservr -c -m -T4022 -T3659 -s"SQL2017" -q"SQL_Latin1_General_CP1_CI_AS"
  4. 执行上述命令后,显示如下信息:"The default collation was successfully changed."
  5. 然后我需要按ctrl+c停止进一步的执行。我能怎么做 这是编程方式?

当我们执行更改 SQL 服务器排序规则的命令时,它会在事件查看器应用程序日志中记录执行细节。使用循环,我们可以连续检查 SqlServr.exe 的事件查看器应用程序日志,当它生成以下日志消息时:"The default collation was successfully changed",我们可以终止进程。

#Take the time stamp before execution of Collation Change Command
$StartDateTime=(Get-Date).AddMinutes(-1)

# Execute the Collation Change Process
Write-Host "Executing SQL Server Collation Change Command"
$CollationChangeProcess=Start-Process -FilePath $SQLRootDirectory -ArgumentList 
"-c -m -T 4022 -T 3659 -s $JustServerInstanceName -q $NewCollationName" - 
NoNewWindow -passthru

Do
{
  $log=Get-WinEvent -FilterHashtable @{logname='application'; 
  providername=$SQLServiceName; starttime = $StartDateTime} | Where-Object - 
  Property Message -Match 'The default collation was successfully changed.'
  IF($log.count -gt 0 -and  $log.TimeCreated -gt $StartDateTime )
  {
    Stop-Process -ID $CollationChangeProcess.ID
    write-host 'Collation Change Process Completed Successfully.'
    break
  }
  $DateTimeNow=(Get-Date)
  $Duration=$DateTimeNow-$StartDateTime
  write-host  $Duration.totalminutes
  Start-Sleep -Seconds 2
  IF ($Duration.totalminutes -gt 2)
  {
    write-host 'Collation Change Process Failed.'
    break
  }
 }while (1 -eq 1)

谢谢。我对您的解决方案进行了一些更改,使其更易于使用。 (我没有足够高的声誉将此添加为对您的回答的评论)。

# Params
$NewCollationName="Danish_Norwegian_CI_AS"
$SQLRootDirectory="C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Binn\sqlservr.exe"
$SQLServiceName="MSSQLSERVER"

# Stop running SQL instance
net stop $SQLServiceName 

#Take the time stamp before execution of Collation Change Command
$StartDateTime=(Get-Date).AddMinutes(-1)

# Execute the Collation Change Process
Write-Host "Executing SQL Server Collation Change Command"
$CollationChangeProcess=Start-Process -FilePath $SQLRootDirectory -ArgumentList "-c -m -T 4022 -T 3659 -q $NewCollationName" -NoNewWindow -passthru

Do
{
  $log=Get-WinEvent -FilterHashtable @{logname='application'; 
  providername=$SQLServiceName; starttime = $StartDateTime} | Where-Object -Property Message -Match 'The default collation was successfully changed.'
  IF($log.count -gt 0 -and  $log.TimeCreated -gt $StartDateTime )
  {
    Stop-Process -ID $CollationChangeProcess.ID
    write-host 'Collation Change Process Completed Successfully.'
    
    # Start SQL instance again
    net start $SQLServiceName 

    break
  }
  $DateTimeNow=(Get-Date)
  $Duration=$DateTimeNow-$StartDateTime
  write-host  $Duration.totalminutes
  Start-Sleep -Seconds 2
  IF ($Duration.totalminutes -gt 2)
  {
    write-host 'Collation Change Process Failed.'
    Stop-Process -ID $CollationChangeProcess.ID
    break
  }
 }while (1 -eq 1)