msdeploy和regex替换连接字符串值

时间:2016-04-04 22:04:46

标签: powershell msdeploy

我有一个带有实体框架连接字符串的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" />

我想要替换初始目录又名数据库名称&#34; CRAP&#34;使用新名称&#34; TEST&#34;

该代码应该像以前使用另一个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语句的语法 WORKED

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

1 个答案:

答案 0 :(得分:0)

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

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

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

  1. 您在TextFile之后错过了双引号。
  2. 您的match值不应该有双引号。
  3. 我不确定您希望在$之后web.config,但我会放弃它,因为它似乎找到了您的文件。
  4. 我认为你最后一行需要看起来像这样:

    & $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更改为单引号,因为它让我觉得它会弄乱你的双引号{{1 }}。如果kind不喜欢单引号,我们需要来自CMD的实际工作命令来确定发生了什么。