2014-01-09 80 views
2

我需要提供一個PowerShell腳本,運行一系列步驟來安裝自定義解決方案。其中一個步驟是創建一個SQL Server數據庫並執行一個.sql文件來生成該模式。在沒有安裝SQL Server的情況下在Powershell中執行.SQL文件?

事情是,我需要一種在任何機器上做到這一點的方式,無論是否有本地安裝的SQL服務器(它通常在另一臺網絡機器上)。 另外我需要避免使用第三方模塊,因爲我無法強制客戶端安裝它們。

有沒有辦法做到這一點?

更新:運行該腳本的服務器將安裝.NET 4.5,因爲將安裝SharePoint 2013。

+0

以及你只能在數據庫中創建模式,因爲PowerShell沒有內置的數據庫引擎,我認爲這不會工作。你可以嘗試連接到一個sql server – Paul

+0

@Paul是的,一個SQL Server實例是可用的,只是它可能在本地(不可能)或在另一臺網絡機器上(極有可能)。問題是,我發現的所有PS cmdlet只有在您嘗試運行腳本的地方安裝了SQL Server時纔有效。 – emzero

回答

6

您可以使用PowerShell中的普通.NET類SqlConnection和SqlCommand。爲此,您不需要安裝SQL Server,也不需要安裝任何特定的PowerShell模塊。只需使用New-Object cmdlet創建對象。

更新:

這裏是你怎麼能這樣做(沒有錯誤處理添加)來調用,而無需使用SQL特定的cmdlet執行SQL代碼示例:

$connectionString = "" #Set your connection string here 
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString) 
$query = "" #Set your sql query here 
$command = New-Object -TypeName System.Data.SqlClient.SqlCommand($query, $connection) 
$connection.Open() 
$command.ExecuteNonQuery() #Other methods are available if you want to get the return value of your query. 
$connection.Close()
+0

你有這樣的例子嗎? – emzero

+0

我已經添加了一個例子。 –

0

您正在嘗試做一些事情,也許無法完成,取決於你對「sql未安裝」的定義。如果客戶端沒有安裝sql客戶端工具,您將無法直接與sql server直接對話。

如果您需要在遠程計算機上安裝sql server,一般情況下無需管理員權限即可完成此操作,並且由於管理員權限的遠程操作仍然很複雜,因爲SQL Server 2012不是爲遠程計算機設計的安裝 - 雖然你可以使用CMD處理器通過遠程連接執行遠程安裝(如果你有一些方法可以做到這一點,因爲這不是內置於標準的Windows安裝。

如果sql客戶端工具安裝在你的本地機器(你正在運行powershell的地方)和sql server正在遠程機器上運行 - 並且你有一個具有合適權限的數據庫登錄,你可以實例化SqlCommand對象來做你可能需要的一切。

+0

SQL客戶端工具隨附.NET –

+0

IIRC最舊版本的.Net框架不包括sql客戶端工具。 –

+0

讓我更好地解釋。 powershell腳本將在安裝Sharepoint 2013的服務器上執行(因此安裝.NET 4.5)。 SQL Server實例很可能位於另一臺網絡計算機上,但它已經安裝,我不需要安裝它。我只需要創建一個數據庫並從SharePoint機器執行.sql腳本。 – emzero

7

事情是這樣的:

#Variables 
$sqlServer = "." 
$sqlDBName = "master" 
$sqlQuery = "CREATE DATABASE test" 

# Create the connection string 
$sqlConnectionString ="Server = $sqlServer; Database = $sqlDBName; Integrated Security = True" 
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
$SqlConnection.ConnectionString = $sqlConnectionString 

#Create the SQL Command object 
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand 
$SqlCmd.CommandText = $SqlQuery 
$SqlCmd.Connection = $SqlConnection 

#Open SQL connection 
$SqlCmd.Connection.Open() 

#Execute the Query 
$ReturnValue = $SqlCmd.ExecuteNonQuery() 

- 編輯

技術上GO不是T-SQL命令。這是由Microsoft SQL工具(Management Studio,isql,osql)識別的批量結束標記。所以這就是爲什麼當你直接執行語句時,它不被識別。 '真正的'解決方案是消除GO語句,或將語句拆分爲單獨的批處理過程(物理或字符串)。拆分使用SQL管理對象從理論上可以處理 「轉到」 語句( 「GO」))

,或備用(SqlCommand() ExecuteNonQuery() truncates command text):

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null 
$SMOServer = New-Object ('Microsoft.SqlServer.Management.Smo.Server') 
$SMOServer.ConnectionContext.ConnectionString = $sqlConnectionString 
$SMOServer.ConnectionContext.ExecuteNonQuery($sqlQuery) 

- 編輯2

或者,如果你能「T使用SQL管理對象,你有‘GO’語句,東西快速和骯髒的是,你可以分割字符串和使用這樣的代碼:

#Variables 
$sqlServer = "." 
$sqlDBName = "master" 
$sqlQuery = "CREATE DATABASE test; GO; CREATE DATABASE test2; GO;" 

# Create the connection string 
$sqlConnectionString ="Server = $sqlServer; Database = $sqlDBName; Integrated Security = True" 
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
$SqlConnection.ConnectionString = $sqlConnectionString 

$sqlQuery -split "GO;" | ForEach-Object{ 
    if($_ -ne "") 
    { 
     #Create the SQL Command object 
     $SqlCmd = New-Object System.Data.SqlClient.SqlCommand 

     $SqlCmd.CommandText = $_ 
     $SqlCmd.Connection = $SqlConnection 

     #Open SQL connection 
     $SqlCmd.Connection.Open() 

     #Execute the Query 
     $ReturnValue = $SqlCmd.ExecuteNonQuery() 

     #Close the connection 
     $SqlCmd.Connection.Close() 
    } 
} 
+0

太棒了,工作正常。 DB創建。現在我需要使用所有create table運行.sql腳本並創建proc命令。問題是,它們被GO語句分割,而ExecuteNonQuery()不適合它們。 – emzero

+0

是的,問題是SMO只有在安裝了SQL Server的情況下才可用。 – emzero

相關問題