Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Powershell and MSSQL

$
0
0

Hello i need a little help with strange behavior my  powershell script performs.

I have two MSSQL instances and there are several databases restored on each one.
The script runs into a folder where my sql scripts are located. Then match the script's name and execute it against the corresponding databases.
All scripts are  exactly like the name of database.
Like this:

Server\Test instance

 --Test1_databse
 --Test2_databse
 --Test3_databse
 --TestPRODUCTION_database
 
 
Server\PROD instance

 -PRODUNTION


My sql scripts are exactly:
Test1_database.sql
Test2_database.sql
Test3_database.sql
TestPRODUCTION_database.sql
PRODUCTION.sql


SO the script runs, and executes each script on its database, but then it tries to find  TestPRODUCTION_database on 'Server\PROD' instance, for some reason.
I am  guessing that  my scripts is not exactly correct.
Here is the script :

#########################################################

$Server = gc env:computername

 
Set-Location D:\dbcripts

$stringtofind = ':setvar DeploymentConfiguration "TEST"'
$replacewith = ':setvar DeploymentConfiguration "PROD"'

gci -Filter *.sql | select -expand fullname | where { !$_.PSIsContainer } | % {
    $file = get-content   $_
    $containsword = $file | %{ $_ -match $stringtofind }

        if ($containsword -contains $true)
        
        {
             (ForEach {
             echo "SQL file"  $_  "need to be modified."
             echo "Edit in progress ..."
             (Get-Content $_ |                   
             foreach { $_ -replace ':setvar DeploymentConfiguration "TEST"',':setvar DeploymentConfiguration "PROD"'}) |
             Set-Content $_ })
             echo " The selected String in" $_  "file has been renamed"
        }
        else  #($replacewith -contains $true)
        {
            
             write-host "The Selected String in" $_ " is renamed"
             "Start executing SQL scripts"
           
              
        }   
        }  
     
      
$DBScriptsArray = Get-ChildItem -Filter *.sql
$runArray = @()
Set-Location SQLSERVER:\SQL\localhost\TEST\Databases
$dbArray = Get-ChildItem  -name -Exclude  "master*", "model*", "msdb*", "DBA*"
        foreach ($script in $DBScriptsArray)
            {
       
        foreach ($db in $dbArray)
            {
               $DBScriptName = $db + ".sql"

                 if ($script.name -match $DBScriptName -eq "TRUE")
               
                    {
                
                     Invoke-Sqlcmd -InputFile $script -ServerInstance 'localhost\PROD' -database $db -SuppressProviderContextWarning -ErrorAction stop | out-file "D:\sql_test_results.txt"
                                     
                    }
            }
            }
   
     
Set-Location SQLSERVER:\SQL\localhost\PROD\Databases
$dbArray = Get-ChildItem  -name -Exclude  "master*", "model*", "msdb*", "DBA*"
     
     
        foreach ($script in $DBScriptsArray)
            {
       
        foreach ($db in $dbArray)
            {
               $DBScriptName = $db + ".sql"

                 if ($script.name -match $DBScriptName -eq "TRUE" )
               
                    {
                
                     Invoke-Sqlcmd -InputFile $script -ServerInstance 'localhost\PROD' -database $db -SuppressProviderContextWarning -ErrorAction stop | out-file "D:\sql_prod_results.txt"
                                  
                    }
            }
            }
   
 ###############################################


And the error i got is:

Database 'TestPRODUCTION_database' does not exist. Make sure that the name is entered correctly.
At line:78 char:35
+                      Invoke-Sqlcmd <<<<  -InputFile $script -ServerInstance 'localhost\PROD' -database $db -SuppressProviderContextWarning -ErrorAction stop | out-file "D:\sql
_prod_results.txt"
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
 

 
 I know the script is not optimized at all so i am accepting all yours recommendations.


Viewing all articles
Browse latest Browse all 6937

Trending Articles