3

Every time I update the production website, I need to run 5 commands, who only differ by a name.

I'd like to automatize this (in the future, it could be I have to run these commands 100 times for example).

What I do is:

Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString "Server=PC-1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=Database1;uid=prog;password=ndp103@50;" -ConnectionProviderName "System.Data.SqlClient" 
Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString "Server=PC-1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=Database2;uid=prog;password=ndp103@50;" -ConnectionProviderName "System.Data.SqlClient" 
Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString "Server=PC-1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=Database3;uid=prog;password=ndp103@50;" -ConnectionProviderName "System.Data.SqlClient" 
Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString "Server=PC-1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=Database4;uid=prog;password=ndp103@50;" -ConnectionProviderName "System.Data.SqlClient" 
Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString "Server=PC-1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=Database5;uid=prog;password=ndp103@50;" -ConnectionProviderName "System.Data.SqlClient" 

How could I write i script, so that, when I type in the console manager:

Update-Database -type Production -version latest

It runs all the commands above, without any interference. (All connectionstrings are located in an .XML file like:

 <databases>
    <type>Production
        <database>Database1</database>
        <database>Database2</database>
        <database>Database3</database>
        <database>Database4</database>
        <database>Database5</database>
    </type>
 </databases>
2
  • Because the Package Manager Console is a PowerShell host you can write a PowerShell script for your logic. Commented Aug 23, 2012 at 15:31
  • I had Powershell as tag in the question, but i had forgotten to mention PowerShell in my question... Commented Aug 24, 2012 at 6:48

2 Answers 2

10

If I understand your question correctly, you're simply asking how to write and execute a powershell script from within the package manager console.

1) Create a new script in the current directory with notepad:

notepad newScript.ps1

2) Paste your commands into notepad

Update-Database...
Update-Datebase...
Update-Database...

3) Save the notepad file

4) Execute the notepad file by typing this into the package manager console:

.\newScript.ps1

Note that the use of powershell functions and parameters are beyond the scope of this answer.

Sign up to request clarification or add additional context in comments.

2 Comments

After execute "newScript.ps1" this error occurred : The term 'Update-Database' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. How to solve it?
The package of Entity Framework can't be found ( a bit late to the party, sorry)
3

I don't know anything about the package manager console per se, but if the goal is to read a bunch of database names out of an XML, pop them into a connection string, and pass them to a cmdlet, below should get you going:

$config = [xml](gc c:\path\to\config.xml)
$dbNames= $config.Databases.Type.Database

$dbNames|%{
  $connStr = "Server=PC-1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=$_;uid=prog;password=ndp103@50;"
  Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString $connStr -ConnectionProviderName "System.Data.SqlClient"
}

5 Comments

I used this solution. It's the closest to what i wanted. And it works, thx!
@latkin : "I don't know anything about the package manager console per se", The Package Manager Console is a PowerShell console within Visual Studio used to interact with NuGet and automate Visual Studio.
@NicoJuicy How could you manage to use the multiline { construct in Package Manager Console? I do know the ctrl+shift+enter is working to add continuation line, but I suppose you have a prewritten script to copy and paste...
@g.pickardou It's a powershell script. The powershell script can be called through Package Manager Console
@NicoJuicy many thx, I concluded the same, so there is an extra hop, outsource the code to a .ps1 file and call the script from PM>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.