Sql-Server

如何在 sql server 中編寫伺服器級對象的腳本?

  • March 20, 2021

我經常在虛擬機或其他地方安裝 sql server(主要是 sql server 2016),我有很多東西,比如權限、連結伺服器和端點,我幾乎都編寫了腳本,這樣在新安裝中我可以創建所有這些對象。

在此處輸入圖像描述

然而,在我正在處理的全新安裝中,它應該是特定伺服器的精確副本,我們稱之為serverX.

serverX在我創建 sql server 伺服器級對像腳本時未指定幾個端點, 因此它導致了我的麻煩 - 埠 4022 未列出,未在偵聽,並且 powershell 連接測試無法正常工作

declare @sql varchar(1008)
set @sql = 'powershell.exe -command Test-NetConnection my_sql_server -port 4022'
EXEC xp_cmdshell @sql

雖然所有的防火牆都已經設置好了。

為避免將來出現這種情況,有沒有辦法編寫所有端點的腳本?或者更好的是根據上圖編寫所有伺服器級對象的腳本?

顯然,要編寫此特定端點的腳本,您只需右鍵點擊並編寫腳本:

USE [master]
GO

/****** Object:  Endpoint [ServiceBrokerEndpoint]    
  Script Date: 20/03/2021 16:04:05 
******/

CREATE ENDPOINT [ServiceBrokerEndpoint] 
   STATE=STARTED
   AS TCP (LISTENER_PORT = 4022, LISTENER_IP = ALL)
   FOR SERVICE_BROKER (MESSAGE_FORWARDING = DISABLED
, MESSAGE_FORWARD_SIZE = 10
, AUTHENTICATION = WINDOWS NEGOTIATE
, ENCRYPTION = DISABLED)
GO

在我這樣做之後netstat命令開始列出埠4022-服務代理預設值。

但只是為了automate將來的安裝,有沒有辦法通過程式碼或powershell,編寫所有或一些伺服器級對象

編寫伺服器級對像腳本的一種方法是使用 SMO 和 PowerShell。下面是一個 PS 範例,它使用 SSMS 中包含的 SMO 程序集來編寫伺服器端點和触發器的腳本。SMO 在NuGet上也可用,以避免對 SSMS 安裝的依賴。

# generate scripts for server-scoped object collection
Function Create-ServerObjectScripts($serverCollection, $rootFolder, $typeName) {

   # exclude system objects
   $serverCollection = $serverCollection | Where-Object {
       $_.IsSystemObject -eq $false -or $_.IsSystemObject -eq $null
   }

   if($serverCollection.Count -eq 0) {
       return
   }

   Write-Host "Scripting $typeName for server $($server.Name)..."

   $scriptingOptions = New-Object Microsoft.SqlServer.Management.Smo.ScriptingOptions
   $scriptingOptions.IncludeIfNotExists = $true
   # specify desired SQL version below
   $scriptingOptions.TargetServerVersion = [Microsoft.SqlServer.Management.Smo.SqlServerVersion]::Version130
   $scriptingOptions.AppendToFile = $true
   $scriptingOptions.ToFileOnly = $true

   foreach ($serverObject in $serverCollection) {
       $scriptingOptions.FileName = "$rootFolder/$typeName.sql"
       [void](New-Item -Path "$($scriptingOptions.FileName)" -ItemType "file" -Force)
       $serverObject.Script($scriptingOptions)
   }

}

# ############
# ### MAIN ###
# ############

try {

   $serverName = "YourServer"
   $scriptRootFolder = "C:\Scripts\YourServer"

   $smoAssemblyDirectory = "C:\Program Files (x86)\Microsoft SQL Server Management Studio 18\Common7\IDE"
   Add-Type -Path "$smoAssemblyDirectory/Microsoft.SqlServer.SMO.dll"

   $server = New-Object Microsoft.SqlServer.Management.Smo.Server($ServerName)
   if ($server.ServerType -eq $null) {
       throw "Unable to connect to server '$ServerName' "
   }

   Create-ServerObjectScripts -serverCollection $server.Endpoints -rootFolder "$scriptRootFolder" -typeName "Endpoints"
   Create-ServerObjectScripts -serverCollection $server.Triggers -rootFolder "$scriptRootFolder" -typeName "Triggers"

   Write-Host "Scripting completed"

}
catch {
   throw
}

引用自:https://dba.stackexchange.com/questions/287410