Hi Forum,
the idea: I have several subfolders containing tsql-scripts. I want to change into a folder and execute every script - sorted by name (important because of dependencies).
I have an array with Subfolder names, I build on fly an array wiht script names inside of every subfolder, I'm reading the content of a tsql-file (sentence by sentence), I'm paying attention for a "go" statement and then execute the command.
When the first script doesn't end with a "go" statement, I'll get an error executing the first line of the second script.
I don't know where to set my command-variable to $null or to blank (" ").
For example the first script is:
DECLARE @CMD varchar(500) SET @CMD = 'EXEC sp_configure ''show advanced option'', ''1''; reconfigure with override EXEC sp_configure ''xp_cmdshell'',''1'' reconfigure with override' print @CMD EXEC (@CMD)..and the second begins with:
sp_configure 'allow updates','1' go reconfigure with override GOthen I get an error:
DECLARE @CMD varchar(500)
SET @CMD = 'EXEC sp_configure ''show advanced option'', ''1'';
reconfigure with override
EXEC sp_configure ''xp_cmdshell'',''1''
reconfigure with override'
print @CMD
EXEC (@CMD)
sp_configure 'allow updates','1'
Exception calling "ExecuteScalar" with "0" argument(s): "Incorrect syntax near 'allow updates'."
At L:\SQL-INSTALL\Install_AdminDB.ps1:42 char:50
+ $SQLCommand.ExecuteScalar <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
My PSScript looks like that:
param( #$ServerName [parameter(Mandatory=$true, HelpMessage="SQLServer-Name?",Position=0)] [String] [ValidateNotNullOrEmpty()] $ServerName = $(throw "sqlserver parameter is required.") ) $Paths = "DEV_1", "DEV_2", "DEV_3" [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null $objSQLConnection = New-Object System.Data.SqlClient.SqlConnection $objSQLConnection.ConnectionString = "Server=$ServerName;Integrated Security=SSPI;" $objSQLConnection.Open() #| Out-Null foreach($path in $Paths) { $sqlSkripts = Join-Path ($PWD.Path) $path | dir -Filter "*.sql" | sort-object name"sqlSkripts: " + $sqlSkripts foreach($sqlSkript in $sqlSkripts) { $SQLCommandText = @(Get-Content -Path $sqlSkript.FullName) "#" * $sqlSkript.FullName.Length | Out-File -FilePath ($PWD.Path + "\SQLProtokoll.txt") -Append $sqlSkript.FullName | Out-File -FilePath ($PWD.Path + "\SQLProtokoll.txt") -Append"#" * $sqlSkript.FullName.Length | Out-File -FilePath ($PWD.Path + "\SQLProtokoll.txt") -Append Try { foreach($SQLString in $SQLCommandText) { if($SQLString -ne "go") { $SQLPacket += $SQLString + "`n" } else { $IsSQLErr = $false try { $SQLCommand = New-Object System.Data.SqlClient.SqlCommand($SQLPacket, $objSQLConnection) $SQLCommand.ExecuteScalar() } catch { Write-Host "Error..." $IsSQLErr = $true"SQLPaket: " + $SQLPacket $SQLPacket | Out-File -FilePath ($PWD.Path + "\SQLProtokoll.txt") -Append $Error[0] | Out-File -FilePath ($PWD.Path + "\SQLProtokoll.txt") -Append } $SQLPacket = $null $SQLString = "" } } } Catch { $errText = $Error[0].ToString()"Error: " + $errText | Out-File -FilePath ($PWD.Path + "\SQLProtokoll.txt") -Append continue } $SQLCommandText = "" } } $objSQLConnection.Close() Write-Host "Done!..." -NoNewline
Thanks for your help
Purclot