Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio - Package Manager Console - Running commands programmatically

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>
like image 431
NicoJuicy Avatar asked Aug 07 '26 12:08

NicoJuicy


1 Answers

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.

like image 129
Ogre71 Avatar answered Aug 09 '26 11:08

Ogre71



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!