使用 SQL IaaS 代理程式擴充功能在 Azure 中註冊多個 SQL VM

適用於:Azure VM 上的 SQL Server

這篇文章說明如何在 Azure 中透過 Register-SqlVMsAzure PowerShell Cmdlet,並使用 SQL IaaS 代理程式擴充功能大量註冊 SQL Server 虛擬機器 (VM)。

或者您可自動註冊所有 SQL Server VM,或手動註冊個別 SQL Server VM

注意

SQL Server 在 2022 年 10 月之後透過 Azure Marketplace 部署的 VM 預設會啟用最低權限模型。 SQL IaaS 代理程式延伸模組的管理模式已在 2023 年 3 月移除。

概觀

Register-SqlVMs Cmdlet 可用在指定的訂用帳戶、資源群組或特定虛擬機器清單中註冊所有虛擬機器。 此 Cmdlet 會註冊虛擬機器,然後產生報表和記錄檔

註冊程式不會產生任何風險、沒有停機時間,也不會重新啟動 SQL Server 服務或虛擬機器。

CEIP 服務偵測到 SQL Server 2016 或更新版本的 Azure VM 時,預設會使用 SQL IaaS 代理程式延伸模組自動註冊 Azure VM。 您可以使用大量註冊來註冊 CEIP 服務未偵測到的任何SQL Server VM。

如需隱私權的相關資訊,請參閱 SQL IaaS 代理程式延伸模組隱私權聲明

必要條件

若要透過擴充功能註冊您的 SQL Server VM ,將需要下列條件:

開始使用

在繼續操作前,您必須先建立指令碼的本機複本、將其匯入為 PowerShell 模組,然後連線至 Azure。

建立指令碼

若要建立指令碼,請複製本文結尾的完整指令碼,並在本機將它儲存為 RegisterSqlVMs.psm1

匯入指令碼

建立指令碼之後,您可以將其匯入為 PowerShell 終端機中的模組。

開啟系統管理 PowerShell 終端機,並瀏覽至您儲存 RegisterSqlVMs.psm1 檔案的位置。 然後,執行下列 PowerShell Cmdlet,將指令碼匯入為模組:

Import-Module .\RegisterSqlVMs.psm1

連線到 Azure

使用以下 PowerShell Cmdlet 來連線至 Azure:

Connect-AzAccount

在訂用帳戶清單中的所有 VM

使用下列 Cmdlet,在訂用帳戶清單中註冊所有 SQL Server 的虛擬機器:

Register-SqlVMs -SubscriptionList SubscriptionId1,SubscriptionId2

範例輸出︰

Number of subscriptions registration failed for 
because you do not have access or credentials are wrong: 1
Total VMs Found: 10
VMs Already registered: 1
Number of VMs registered successfully: 4
Number of VMs failed to register due to error: 1
Number of VMs skipped as VM or the guest agent on VM is not running: 3
Number of VMs skipped as they are not running SQL Server On Windows: 1

Please find the detailed report in file RegisterSqlVMScriptReport1571314821.txt
Please find the error details in file VMsNotRegisteredDueToError1571314821.log

在單一訂用帳戶中的所有 VM

使用下列 Cmdlet,在單一訂用帳戶中註冊所有 SQL Server 的虛擬機器:

Register-SqlVMs -Subscription SubscriptionId1

範例輸出︰

Total VMs Found: 10
VMs Already registered: 1
Number of VMs registered successfully: 5
Number of VMs failed to register due to error: 1
Number of VMs skipped as VM or the  guest agent on VM is not running: 2
Number of VMs skipped as they are not running SQL Server On Windows: 1

Please find the detailed report in file RegisterSqlVMScriptReport1571314821.txt
Please find the error details in file VMsNotRegisteredDueToError1571314821.log

在多個資源群組中的所有 VM

使用下列 Cmdlet,在單一訂用帳戶內的多個資源群組中註冊所有 SQL Server 的虛擬機器:

Register-SqlVMs -Subscription SubscriptionId1 -ResourceGroupList ResourceGroup1,ResourceGroup2

範例輸出︰

Total VMs Found: 4
VMs Already registered: 1
Number of VMs registered successfully: 1
Number of VMs failed to register due to error: 1
Number of VMs skipped as they are not running SQL Server On Windows: 1

Please find the detailed report in file RegisterSqlVMScriptReport1571314821.txt
Please find the error details in file VMsNotRegisteredDueToError1571314821.log

資源群組中的所有 VM

使用下列 Cmdlet,在單一資源群組中註冊所有 SQL Server 的虛擬機器:

Register-SqlVMs -Subscription SubscriptionId1 -ResourceGroupName ResourceGroup1

範例輸出︰

Total VMs Found: 4
VMs Already registered: 1
Number of VMs registered successfully: 1
Number of VMs failed to register due to error: 1
Number of VMs skipped as VM or the guest agent on VM is not running: 1

Please find the detailed report in file RegisterSqlVMScriptReport1571314821.txt
Please find the error details in file VMsNotRegisteredDueToError1571314821.log

在單一資源群組中特定 VM

使用下列 Cmdlet,在單一資源群組中註冊特定 SQL Server 的虛擬機器:

Register-SqlVMs -Subscription SubscriptionId1 -ResourceGroupName ResourceGroup1 -VmList VM1,VM2,VM3

範例輸出︰

Total VMs Found: 3
VMs Already registered: 0
Number of VMs registered successfully: 1
Number of VMs skipped as VM or the guest agent on VM is not running: 1
Number of VMs skipped as they are not running SQL Server On Windows: 1

Please find the detailed report in file RegisterSqlVMScriptReport1571314821.txt
Please find the error details in file VMsNotRegisteredDueToError1571314821.log

特定 VM

使用下列 Cmdlet 註冊特定 SQL Server 的虛擬機器:

Register-SqlVMs -Subscription SubscriptionId1 -ResourceGroupName ResourceGroup1 -Name VM1

範例輸出︰

Total VMs Found: 1
VMs Already registered: 0
Number of VMs registered successfully: 1

Please find the detailed report in  file RegisterSqlVMScriptReport1571314821.txt

輸出描述

每次使用 Register-SqlVMs Cmdlet 時,都會產生報表和記錄檔。

Report

報表會產生名為 RegisterSqlVMScriptReport<Timestamp>.txt.txt 檔案,其中時間戳記是 Cmdlet 啟動的時間。 此報告列出下列詳細資料:

輸出值 說明
因為您沒有存取權限或認證不正確的訂用帳戶註冊失敗數 這會針對所提供驗證發生問題的訂用帳戶提供訂用帳戶數目和清單。 您可以藉由搜尋訂用帳戶識別碼,在記錄檔中找到詳細的錯誤。
因為未向資源提供者註冊而無法嘗試的訂用帳戶數 此區段包含尚未透過 SQL IaaS 代理程式擴充功能註冊的訂用帳戶計數和清單。
已找到 VM 總數 在傳遞至 Cmdlet 的參數範圍內找到的虛擬機器計數。
已註冊 VM 數 已透過擴充功能註冊而略過的虛擬機器計數。
成功註冊的 VM 數 在執行 Cmdlet 後已成功註冊的虛擬機器計數。 以 SubscriptionID, Resource Group, Virtual Machine 格式列出已註冊的虛擬機器。
因發生錯誤而無法註冊的 VM 數 因某個錯誤而無法註冊的虛擬機器計數。 錯誤的詳細資料可在記錄檔中找到。
因為 VM 或 VM 上的 gust 代理程式未執行而略過的 VM 數 無法在虛擬機器上註冊為虛擬機器或客體代理程式的虛擬機器計數和清單未執行。 當虛擬機器或客體代理程式啟動之後,就可以重試這些操作。 在記錄檔中可找到詳細資料。
因未在 Windows 上執行 SQL Server 而略過的 VM 數 因為未執行 SQL Server 或不是 Windows 虛擬機器而略過的虛擬機器計數。 虛擬機器會以 SubscriptionID, Resource Group, Virtual Machine 格式列出。

Log

錯誤會記錄在名為 VMsNotRegisteredDueToError<Timestamp>.log 的記錄檔中,其中時間戳記是指令碼啟動的時間。 如果錯誤位於訂用帳戶層級,則記錄檔會包含以逗號分隔的訂用帳戶識別碼和錯誤訊息。 如果此錯誤與虛擬機器註冊有關,記錄檔會包含訂用帳戶識別碼、資源群組名稱、虛擬機器名稱、錯誤碼和訊息 (以逗號分隔)。

備註

當您使用提供的指令碼並透過擴充功能註冊 SQL Server VM 時,請考慮下列事項:

  • 透過擴充功能註冊需要在 SQL Server VM 上執行客體代理程式。 Windows Server 2008 映像沒有客體代理程式,因此這些虛擬機器將會失敗,而且必須使用有限功能手動註冊
  • 有內建的重試邏輯可克服透明錯誤。 如果虛擬機器已成功註冊,則是快速操作。 不過,如果註冊失敗,則會重試每部虛擬機器。 因此,您應該允許大量時間來完成註冊流程,不過實際時間需求取決於錯誤的類型和數量。

完整指令碼

如需 GitHub 上的完整指令碼,請參閱使用 Az PowerShell 大量註冊 SQL Server VM

複製完整指令碼,並將其儲存為 RegisterSqLVMs.psm1

後續步驟

若要深入了解,請檢閱下列文章: