共用方式為


流覽 SQL Server PowerShell 路徑

Database Engine PowerShell 提供者會在類似檔案路徑的結構中,公開 SQL Server 實例中的 物件集。 您可以使用 Windows PowerShell Cmdlet 來巡覽提供者路徑,並建立自定義磁碟驅動器來縮短您必須輸入的路徑。

注意

有兩個 SQL Server PowerShell 模組;SqlServerSQLPS

SqlServer 模組是要使用的目前 PowerShell 模組。

SQLPS 模組 隨附於 SQL Server 安裝中,但不再更新。

SqlServer 模組包含 SQLPS 中更新的 Cmdlet 版本,並包含新的 Cmdlet 以支援最新的 SQL 功能。

PowerShell 資源庫安裝 SqlServer 模組。

如需詳細資訊,請流覽 SQL Server PowerShell

Windows PowerShell 會實作 Cmdlet,以巡覽路徑結構,此結構代表 PowerShell 提供者所支援物件的階層。 當您瀏覽至路徑中的節點時,可以使用其他 Cmdlet 對目前物件執行基本作業。 因為 Cmdlet 經常使用,所以它們有簡短、正式的別名。 另外還有一組別名,會將 Cmdlet 對應至類似的命令提示字元命令,以及另一組 UNIX 殼層命令的別名。

SQL Server 提供者會實作提供者 Cmdlet 的子集,如下表所示:

Cmdlet 標準別名 cmd 別名 UNIX 殼層別名 描述
Get-Location gl pwd pwd 取得目前的節點。
Set-Location sl cd, chdir cd, chdir 變更目前的節點。
Get-ChildItem gci dir ls 列出儲存在目前節點的物件。
Get-Item gi 傳回目前項目的屬性。
Rename-Item rni rn 重新命名物件。
Remove-Item ri del, rd rm、rmdir 拿掉物件。

重要

某些 SQL Server 識別碼 (物件名稱) 包含 Windows PowerShell 在路徑名稱中不支援的字元。 如需如何使用包含這些字元的名稱的詳細資訊,請參閱 PowerShell 中的 SQL Server 識別子

Get-ChildItem 傳回的 SQL Server 資訊

Get-ChildItem 傳回的資訊(或其 dirls 別名) 會取決於您在 SQLSERVER: 路徑中的位置。

路徑位置 Get-ChildItem 結果
SQLSERVER:\SQL 傳回本機計算機的名稱。 如果您已使用 SMO 或 WMI 連線到其他電腦上的 Database Engine 實例,也會列出這些電腦。
SQLSERVER:\SQL\ComputerName 計算機上的Database Engine實例清單。
SQLSERVER:\SQL\ComputerName\InstanceName 實例中最上層物件類型的清單,例如端點、憑證和資料庫。
物件類別節點,例如資料庫 這個類型的物件清單,例如資料庫清單:mastermodelAdventureWorks2022
物件名稱節點,例如 AdventureWorks2022 物件中包含的物件類型清單。 例如,資料庫會列出物件類型,例如數據表和檢視表。

根據預設,Get-ChildItem 不會列出任何系統物件。 使用 Force 參數來查看系統物件,例如 sys 架構中的物件。

自定義磁碟驅動器

Windows PowerShell 可讓使用者定義稱為 PowerShell 磁碟驅動器的虛擬磁碟驅動器。 這些磁碟驅動器會對應至path語句的起始節點。 它們通常用來縮短經常輸入的路徑。 SQLSERVER:路徑可能會很長,在 Windows PowerShell 視窗中佔用空間,而且需要大量輸入。 如果您要在特定路徑節點上執行許多工作,您可以定義對應至該節點的自定義 Windows PowerShell 磁碟驅動器。

使用 PowerShell Cmdlet 別名

使用 Cmdlet 別名

  • 而不是輸入完整的 Cmdlet 名稱、輸入較短的別名,或對應至熟悉的表揚提示命令的別名。

範例:別名

例如,您可以使用下列其中一組 Cmdlet 或別名來擷取可供您使用的 SQL Server 實例清單,方法是流覽至 SQLSERVER:\SQL 資料夾,並要求資料夾的子項目清單:

## Shows using the full cmdet name.
Set-Location SQLSERVER:\SQL
Get-ChildItem

## Shows using canonical aliases.
sl SQLSERVER:\SQL
gci

## Shows using command prompt aliases.
cd SQLSERVER:\SQL
dir

## Shows using Unix shell aliases.
cd SQLSERVER:\SQL
ls

使用 Get-ChildItem

使用 Get-Childitem 傳回資訊

  1. 流覽至您想要子系列表的節點

  2. 執行 Get-Childitem 以取得清單。

範例:Get-ChildItem

這些範例說明 SQL Server 提供者路徑中不同節點 Get-Childitem 所傳回的資訊。

## Return the current computer and any computer
## to which you have made a SQL or WMI connection.
Set-Location SQLSERVER:\SQL
Get-ChildItem

## List the instances of the Database Engine on the local computer.

Set-Location SQLSERVER:\SQL\localhost
Get-ChildItem

## Lists the categories of objects available in the
## default instance on the local computer.
Set-Location SQLSERVER:\SQL\localhost\DEFAULT
Get-ChildItem

## Lists the databases from the local default instance.
## The force parameter is used to include the system databases.
Set-Location SQLSERVER:\SQL\localhost\DEFAULT\Databases
Get-ChildItem -force

建立自定義磁碟驅動器

建立和使用自定義磁碟驅動器

  1. 使用 New-PSDrive 來定義自定義磁碟驅動器。 使用 Root 參數來指定自定義磁碟驅動器名稱所代表的路徑。

  2. 在路徑導覽 Cmdlet 中參考自訂磁碟驅動器名稱,例如 Set-Location

範例:自定義磁碟驅動器

此範例會建立名為 AWDB 的虛擬磁碟驅動器,該磁碟驅動器會對應至 AdventureWorks2022 範例資料庫的已部署複本的節點。 接著會使用虛擬磁碟驅動器來巡覽至資料庫中的數據表。

## Create a new virtual drive.
New-PSDrive -Name AWDB -Root SQLSERVER:\SQL\localhost\DEFAULT\Databases\AdventureWorks2022

## Use AWDB: to navigate to a specific table.
Set-Location AWDB:\Tables\Purchasing.Vendor