Sql-Server
使用 powershell 連接數據庫
我正在嘗試連接到我的
database
使用PowerShell
,但即使我可以使用 SSMS 看到它也找不到它。我正在嘗試遵循 MS在此處提供的範例
如果您在數據庫所在的 SQL Server 上,則可以按照以下步驟在PowerShell中連接到數據庫
首先,您必須載入SQL Server PowerShell 模組。這是通過以下簡單命令實現的:
PS C:\> sqlps Microsoft (R) SQL Server (R) PowerShell Version 12.0.5557.0 Copyright (c) 2014 Microsoft. All rights reserved. PS SQLSERVER:\>
您現在已載入 SQL Powershell 模組並已切換到SQLSERVER驅動器。
讓我們看看我們可以從系統中檢索到什麼:
PS SQLSERVER:\> get-childitem Name Root Description ---- ---- ----------- DAC SQLSERVER:\DAC SQL Server Data-Tier Application Component DataCollection SQLSERVER:\DataCollection SQL Server Data Collection SQLPolicy SQLSERVER:\SQLPolicy SQL Server Policy Management Utility SQLSERVER:\Utility SQL Server Utility SQLRegistration SQLSERVER:\SQLRegistration SQL Server Registrations SQL SQLSERVER:\SQL SQL Server Database Engine SSIS SQLSERVER:\SSIS SQL Server Integration Services XEvent SQLSERVER:\XEvent SQL Server Extended Events DatabaseXEvent SQLSERVER:\DatabaseXEvent SQL Server Extended Events SQLAS SQLSERVER:\SQLAS SQL Server Analysis Services PS SQLSERVER:\>
這看起來很有希望。
我們將切換到SQL Server 數據庫引擎(SQL) 並查看我們擁有的內容:
PS SQLSERVER:\> cd SQL PS SQLSERVER:\SQL> get-childitem MachineName ----------- SQLSERVER-NAME
好的。讓我們連接到
SQLSERVER-NAME
實例並繼續研究(我localhost
在範例中使用了,因為我在伺服器本身上):PS SQLSERVER:\SQL> cd localhost PS SQLSERVER:\SQL\localhost> get-childitem Instance Name ------------- DEFAULT
切換到 DEFAULT 實例:
PS SQLSERVER:\SQL\localhost> cd DEFAULT PS SQLSERVER:\SQL\localhost\DEFAULT>
並檢索實例對象列表:
PS SQLSERVER:\SQL\localhost\DEFAULT> get-childitem Audits AvailabilityGroups BackupDevices Credentials CryptographicProviders Databases Endpoints JobServer Languages LinkedServers Logins Mail ResourceGovernor Roles ServerAuditSpecifications SystemDataTypes SystemMessages Triggers UserDefinedMessages PS SQLSERVER:\SQL\localhost\DEFAULT>
好的,切換到數據庫…
PS SQLSERVER:\SQL\localhost\DEFAULT> cd Databases PS SQLSERVER:\SQL\localhost\DEFAULT\Databases> get-childitem Name Status Containment Type Recovery Model CompatLvl Collation Owner ---- ------ ---------------- -------------- --------- --------- ----- AdminDB2 Normal None Full 120 Latin1_General_CS_AS sa AdventureWorks2012 Normal None Simple 110 SQL_Latin1_General_CP1_CI_AS sa File_Types Normal None Full 120 Latin1_General_CS_AS sa ReportServer Normal None Full 120 Latin1_General_CI_AS_KS_WS sa ReportServerTempDB Normal None Simple 120 Latin1_General_CI_AS_KS_WS sa sqlnexus Normal None Full 120 Latin1_General_CS_AS sa StackExchange Normal None Full 120 Latin1_General_CS_AS sa TestDatabase2FS Normal None Full 120 Latin1_General_CS_AS sa PS SQLSERVER:\SQL\localhost\DEFAULT\Databases>
一旦進入正確的數據庫,您就可以設置相關的 CMD-let。
PS SQLSERVER:\SQL\localhost\DEFAULT\Databases> cd StackExchange PS SQLSERVER:\SQL\localhost\DEFAULT\Databases\StackExchange> cd Tables PS SQLSERVER:\SQL\localhost\DEFAULT\Databases\StackExchange\Tables> Get-ChildItem Schema Name Created ------ ---- ------- dbo Blocking 05.04.2017 16:17 dbo deadlock1 10.01.2017 16:09 dbo deadlock2 10.01.2017 16:09 dbo Issue_776 29.05.2017 07:54 dbo Q184469_Customer 28.08.2017 10:29 dbo Q184469_DVD 28.08.2017 10:29 dbo Q184469_DVD_Purchase 28.08.2017 10:29 dbo Q190497_Stats_Hash 09.11.2017 09:06 dbo Reference_Questions 06.01.2017 15:07 dbo Täble_Name 18.12.2017 11:53 dbo tblTable 30.08.2017 10:45 dbo TestJohnNessDBATemp 12.09.2017 11:58 DBT Application 08.02.2017 07:59 DBT Database 08.02.2017 08:02 DBT Instance 08.02.2017 07:57 DBT Server 08.02.2017 07:54 DBT User 08.02.2017 08:00 PS SQLSERVER:\SQL\localhost\DEFAULT\Databases\StackExchange\Tables>
然後,您可以從這裡繼續。