【问题标题】:msdeploy and regex replace a connection string valuemsdeploy 和正则表达式替换连接字符串值
【发布时间】:2016-04-04 22:04:45
【问题描述】:

我有一个带有实体框架连接字符串的 web.config。

这是一个这样的字符串的样本:

<add name="MyEntities" connectionString="metadata=res://*/GeneratedModel.MyModel.csdl|res://*/GeneratedModel.MyModel.ssdl|res://*/GeneratedModel.MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;initial catalog=CRAP;persist security info=True;user id=MyUser;password=password;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

我想用新名称“TEST”替换初始目录又名数据库名称“CRAP”

该代码应该像以前使用另一个 msdeploy.exe 命令一样工作。

我只需要重新构造 $msd 变量...

$newActiveTargetDbName = 'TEST';

$msd = "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe"
$iisPath = "MyWebSite/MyService,wmsvc='MyMachine:MyPort/msdeploy.axd',userName='username',password='password'"
& $msd -verb=sync -allowUntrusted -dest=contentPath="$iisPath" -source=contentPath="$iisPath" -setParam=kind="TextFile,match=""(MyEntities.*?catalog=).*?(;)"",scope=web.config$,value=`$1$newActiveTargetDbName`$2"

当我运行上面的语句时,我在 web.config 中得到了这个连接字符串:

 <add name="$1TEST$2persist security info=True;user id=MyUser;password=password;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

在我看来,我需要一些单引号/双引号等...但实际上我尝试了所有类型的组合,这让我发疯了。

我该如何解决这个问题?

更新

这是 msdeploy 语句的语法,在我更改之前它是如何工作的!

$iisServicePath = 'MyWebsite/MyService'; 
$iisPath = "$iisServicePath,computerName=%TargetComputerName%,userName=%NTUserName%,password=%NTUserPassword%"

& "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" `
"-source=contentPath=$iisPath" `
'-verb=sync' `
'-verbose' `
'-allowUntrusted' `
"-dest=contentPath=$iisPath" `
"-setParam=kind=TextFile,match=""(MyEntities.*?catalog=).*?(;)"",scope=web.config$,value=`$1$newActiveTargetDbName`$2";

【问题讨论】:

    标签: powershell msdeploy


    【解决方案1】:

    我不熟悉msdeploy.exe,但我确实似乎有些错误......

    According to the docs,您应该在初始参数名称后使用冒号(:);如:-dest:contentPath 而不是-dest=contentPath

    另外(记住,这些都是猜测,因为我不熟悉msdeploy.exe):

    1. TextFile 后面缺少双引号。
    2. 您的match 值不应包含双引号。
    3. 我不确定你是否想要web.config 之后的$,但我不会管它,因为它似乎找到了你的文件。

    我认为你的最后一行需要看起来像这样:

    & $msd -verb=sync -allowUntrusted -dest:contentPath="$iisPath" -source:contentPath="$iisPath" -setParam:kind="TextFile",match="(MyEntities.*?catalog=).*?(;)",scope=web.config$,value="`$1$newActiveTargetDbName`$2"
    

    我希望这会有所帮助!

    更新

    根据您的工作,我怀疑它是否有效,因为它使用 % 变量名(而不是 POSH 做事的方式),这应该可以满足您的需求:

    & $msd -verb=sync -allowUntrusted -dest=contentPath="$iisPath" -source=contentPath="$iisPath" -setParam=kind=TextFile,match="(MyEntities.*?catalog=).*?(;)",scope=web.config$,value="`$1$newActiveTargetDbName`$2"
    

    注意: 还有几个未回答的变量;如果你给我们完整的代码,那么就会有很多错误。

    更新 2

    再次查看所有内容后(几乎没有来自 OP 的反馈),这就是我在 PowerShell 中的操作方式。我不能 100% 确定你的论点是正确的:

    #region Set These Variables
    $computername = $env:ComputerName
    $nt_username = 'username'
    $nt_password = 'password'
    $newActiveTargetDbName = 'newActiveTargetDbName'
    #endregion
    
    $msd = "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe"
    $iisServicePath = 'MyWebsite/MyService'
    $iisPath = "${iisServicePath},computerName=${computername},userName=${nt_username},password=${nt_password}"
    
    $start_process = @{
        'FilePath' = $msd;
        'ArgumentList' = @(
            '-verb=sync',
            '-allowUntrusted',
            "-dest=contentPath=""${iisPath}""",
            "-source=contentPath=""${iisPath}""",
            "-setParam=kind=""TextFile,match='(MyEntities.*?catalog=).*?(;)',scope=web.config$,value=`$1${newActiveTargetDbName}`$2"""
        );
        'Wait' = $True
    }
    
    $process = Start-Process @start_process
    

    这会给你一个在命令提示符中看起来像这样的命令:

    "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -verb=sync -allowUntrusted -dest=contentPath="MyWebsite/MyService,computerName=V-ASUSM32,userName=username,password=password" -source=contentPath="MyWebsite/MyService,computerName=V-ASUSM32,userName=username,password=password" -setParam=kind="TextFile,match='(MyEntities.*?catalog=).*?(;)',scope=web.config$,value=$1newActiveTargetDbName$2"
    

    注意:setParam 参数中,我将match 更改为单引号,因为在我看来它会弄乱kind 周围的双引号。如果msdeploy.exe 不喜欢单引号,我们需要一个来自 CMD 的实际工作命令来弄清楚发生了什么。

    【讨论】:

    • 我用以前的工作示例更新了我的问题。在那里你看到 -dest=contentPath 完全没问题。我确实喜欢您的最后一行建议并收到此错误:msdeploy.exe:错误:无法识别的参数“match=(ErpProductsEntities.*?catalog=).*?(;)”。所有参数必须以“-”开头。
    • 您的多行示例会引发异常/错误。我知道您说您不熟悉 msdeploy,但您确实应该在 PS IDE 中尝试您的示例,否则人们认为该示例可能有效或成为解决方案:-)
    • @Elisabeth,您尝试过单行示例吗?在多行示例之后我有一个注释:Note: 这故意不正确的豪华多行语法,因为我不想要反引号(`)打破语法突出显示。
    • @Elisabeth,我取出了多行示例以避免进一步混淆。另外,我更新以解决您的更新。祝你好运!
    • @Elisabeth 你修好了吗?
    猜你喜欢
    • 2019-04-17
    • 2018-07-13
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    相关资源
    最近更新 更多