2013-10-30 91 views
4

我有一些將腳本加載到變量的代碼,然後將變量傳遞給SMO對象。我得到以下異常:在PowerShell中通過SMO執行SQL腳本時出錯

異常調用 「ExecuteWithResults」 與 「1」 的說法(S): 「執行 與業績未能爲數據庫 'Russell_Test'」

  • $ serverName是服務器名稱。
  • $ databaseName是數據庫名稱。
  • $ createScript是被讀取的腳本。

我該如何解決這個問題?

以下是代碼的相關部分。

# Load Smo and referenced assemblies. 
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.ConnectionInfo'); 
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Management.Sdk.Sfc'); 
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO'); 
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended'); 

    Try{ 
     $server = New-Object Microsoft.SqlServer.Management.Smo.Server $serverName; 
     $db = $server.Databases.Item($databaseName); 
     $result = $db.ExecuteWithResults($createScript); 
     $result | Out-File -Append -FilePath $outputFile; 
    } 
    Catch 
    { 
     [system.exception] 
     $_.Exception | Out-File -Append -FilePath $outputFile 
    } 
+0

您能顯示變量$ createScript的內容嗎? –

+0

以下是數據庫腳本中的代碼:'USE Russell_Test GO create table scripttest(a VARCHAR(1)) GO'讀取它的實際代碼是:'$ createScript = Get-Content $ scriptFile .FullName | Out-String' – Russ960

+1

試試這個簡單的CREATE語句:create table scripttest(一個VARCHAR(1)) –

回答

2

感謝您的幫助。最終SMO選項不會工作,所以我做了這個解決方案:

# Deploy table update scripts 
$createScript = Get-Content $scriptFile.FullName | Out-String 
################# Script execution to capture errors/messages/warnings ################## 
     $createScriptList = [regex]::Split($createScript, '\bGO') 
     $cn2 = new-object system.data.SqlClient.SQLConnection("Data Source=$serverName;Integrated Security=SSPI;Initial Catalog=$databaseName;Connection Timeout=600;Max Pool Size=10"); 
     $cn2.Open(); 
     foreach ($cSL in $createScriptList) 
      { 
       Try{ 
       $cmd = new-object system.data.sqlclient.sqlcommand($cSL, $cn2); 
       $cmd.ExecuteScalar() | Out-File -Append -FilePath $outputFile; 
       } 
       Catch 
       { 
        [system.exception] 
        $_.Exception | Out-File -Append -FilePath $outputFile 
       } 
      } 
     $cn2.Close(); 
############################################################################################### 
5

這裏是如何做到這一點:

# If we cast $Content as a [String], all the newlines are lost 
# If we don't cast it, it becomes an array and batching breaks 
$Content = (Get-Content $SqlScript) -join [Environment]::NewLine 

# When using GO, we must set it up as a StringCollection, not a List or Array 
$Batch = New-Object -TypeName:Collections.Specialized.StringCollection 
$Batch.AddRange($Content) 
$result = $Database.ExecuteWithResults($Batch) 
1

嘗試使用Invoke-SqlCmd cmdlet的。此cmdlet允許您運行T-SQL代碼或支持的命令,如SQLCMD實用程序。

Try{ 
    Invoke-SqlCmd ` 
    -Query $createScript ` 
    -ServerInstance $serverName ` 
    -Database $databaseName ` 
    -ErrorAction Stop ` 
    } 
    Catch 
    { 
     [system.exception] 
     $_.Exception | Out-File -Append -FilePath $outputFile 
    } 
+0

我不確定它會根據我過去的研究獲得所有的錯誤。這就是說,使用Invoke-SqlCmd是不行的,因爲我已經在腳本啓動的所有環境中加載了sqlps模塊。 – Russ960

+0

-ErrorAction Stop參數將幫助您捕獲錯誤。答案已更新 –