我有一些將腳本加載到變量的代碼,然後將變量傳遞給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
}
您能顯示變量$ createScript的內容嗎? –
以下是數據庫腳本中的代碼:'USE Russell_Test GO create table scripttest(a VARCHAR(1)) GO'讀取它的實際代碼是:'$ createScript = Get-Content $ scriptFile .FullName | Out-String' – Russ960
試試這個簡單的CREATE語句:create table scripttest(一個VARCHAR(1)) –