sql-server - 使用 powershell 更改 SQL Server 的实例级别排序规则
问题描述
我想使用 powershell 脚本以编程方式更改 SQL Server 实例的排序规则。以下是手动步骤:
- 停止 SQL Server 实例
- 转到目录位置:“C:\Program Files\Microsoft SQL Server\MSSQL14.SQL2017\MSSQL\Binn”
- 执行以下命令:sqlservr -c -m -T4022 -T3659 -s"SQL2017" -q"SQL_Latin1_General_CP1_CI_AS"
- 执行上述命令后,显示以下消息:“默认排序规则已成功更改。”
- 然后我需要按 ctrl+c 停止进一步执行。如何以编程方式执行此操作?
解决方案
当我们执行命令来更改 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)
推荐阅读
- c# - C# 串口异常
- android - 自定义 StackLayout 膨胀的对象比预期的要多
- c# - 如何在 C# 中导入带有空行的 SQLite-DB?
- assembly - jmp和b的区别?
- biztalk - BizTalk 信封架构和分批说明
- laravel - 如何使用 laravel 和 mailtrap 发送电子邮件
- elasticsearch - 弹性搜索按字段分组
- javascript - Gulp 编译多个 scss/js 文件
- javascript - 有没有办法从three.js中的函数参数获取位置(xyz)?
- javascript - 在反应原生中单击按钮时如何运行node.js服务器