使用powershell更改SQL Server的实例级别排序规则

问题描述 投票:0回答:1

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

  1. 停止SQL Server实例
  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. 执行上述命令后,显示以下消息:“默认排序规则已成功更改。”
  5. 然后我需要按ctrl + c停止进一步执行。我能怎么做以编程方式?
sql-server powershell collation
1个回答
0
投票

[当我们执行更改SQL Server排序规则的命令时,它将执行详细信息记录在事件查看器应用程序日志中。使用循环,我们可以连续检查事件查看器应用程序日志中是否存在SqlServr.exe,并且当它生成以下日志消息:“默认排序规则已成功更改”时,我们可以终止该进程。

#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)
© www.soinside.com 2019 - 2024. All rights reserved.