I have some code that loads a script to a variable, and then I pass the variable to an SMO object. I get the following exception:
Exception calling "ExecuteWithResults" with "1" argument(s): "Execute with results failed for Database 'Russell_Test'. "
- $serverName is the server name.
- $databaseName is the database name.
- $createScript is the script that was read.
How can I fix this problem?
Below is the relevant portion of the code.
# 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
}
USE Russell_Test GO create table scripttest (a VARCHAR(1)) GOThe actual code that reads this in is:$createScript = Get-Content $scriptFile.FullName | Out-StringGO. TheGOcommand is not a SQL command it is a Management Studio (SSMS) command and is implemented by it and SQLCMD (it's command-line interface) only. If you want to handleGO, then you will have to implement the code (powershell or otherwise) to cut up the script into separate batches on everyGOyourself.