简体   繁体   English

Visual Studio - 包管理器控制台 - 以编程方式运行命令

[英]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.每次更新生产网站时,我都需要运行 5 个命令,它们只是名称不同。

I'd like to automatize this (in the future, it could be I have to run these commands 100 times for example).我想自动执行此操作(例如,将来我可能必须运行这些命令 100 次)。

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: (所有连接字符串都位于一个 .XML 文件中,例如:

 <databases>
    <type>Production
        <database>Database1</database>
        <database>Database2</database>
        <database>Database3</database>
        <database>Database4</database>
        <database>Database5</database>
    </type>
 </databases>

If I understand your question correctly, you're simply asking how to write and execute a powershell script from within the package manager console.如果我正确理解您的问题,您只是在询问如何从包管理器控制台中编写和执行 powershell 脚本。

1) Create a new script in the current directory with notepad: 1)用记事本在当前目录新建一个脚本:

notepad newScript.ps1

2) Paste your commands into notepad 2)将您的命令粘贴到记事本中

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

3) Save the notepad file 3) 保存记事本文件

4) Execute the notepad file by typing this into the package manager console: 4)通过在包管理器控制台中键入以下内容来执行记事本文件:

.\newScript.ps1

Note that the use of powershell functions and parameters are beyond the scope of this answer.请注意,powershell 函数和参数的使用超出了本答案的范围。

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:我对包管理器控制台本身一无所知,但是如果目标是从 XML 中读取一堆数据库名称,将它们弹出到连接字符串中,然后将它们传递给一个 cmdlet,那么下面应该可以帮助您:

$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"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM