【发布时间】:2023-03-09 04:03:01
【问题描述】:
我正在尝试设置一个 Cake 任务,该任务将使用 MsDeploy 将 powershell 脚本同步到远程服务器,然后将该脚本作为同步后命令执行。
我面临的问题是在 cake 文件中找到引号和转义符的组合,允许命令一直到 powershell 并正确引用文件路径以允许 powershell 找到脚本;路径中有空格。
由于此命令在运行时要经过的执行链,这似乎非常困难。
首先,因为这是用 C# 编写的,所以字符串要么需要逐字显示 (@"command here"),要么使用 \ 转义所有内部双引号。
Next Cake 对参数执行了多项操作,但在实际执行之前似乎没有任何操作会影响引用和转义等操作,此时它使用 C# Process.Start() 静态方法来运行 MsDeploy 可执行文件。在我的阅读中,有人建议这种运行命令的方法需要正确转义三个双引号,尽管这与我尝试时看到的不符。
然后在远程机器上,MsDeploy 使用CMD.exe 执行命令,该命令明显不支持单引号,因此需要使用\" 或"" 对双引号进行转义。
我得到的最接近的看起来像这样:
Task("InitializeIISApplication")
.IsDependentOn("InjectVariables")
.Does(() => {
MsDeploy(new MsDeploySettings
{
Verb = Operation.Sync,
RetryAttempts = 3,
RetryInterval = 10000,
Source = new FilePathProvider
{
Direction = Direction.source,
Path = MakeAbsolute(File(@".\MyPowershell.ps1")).ToString()
},
Destination = new FilePathProvider
{
Direction = Direction.dest,
Path = File(deployParameters.ApplicationDestinationPath + @"\MyPowershell.ps1").ToString(),
Username = deployParameters.MsDeployUserName,
Password = deployParameters.MsDeployUserPassword,
WebManagementService = deployParameters.DeploymentTargetUrl
},
AllowUntrusted = true,
EnableRules = new List<string> {
"DoNotDeleteRule"
},
PostSyncCommand = new CommandProvider {
AppendQuotesToPath = false,
Direction = Direction.dest,
Path = $"powershell -file '{deployParameters.ApplicationDestinationPath}\\MyPowershell.ps1' ",
}
});
MsDeploy(new MsDeploySettings
{
Verb = Operation.Delete,
Destination = new FilePathProvider
{
Direction = Direction.dest,
Path = File(deployParameters.ApplicationDestinationPath + "\MyPowershell.ps1").ToString(),
Username = deployParameters.MsDeployUserName,
Password = deployParameters.MsDeployUserPassword,
WebManagementService = deployParameters.DeploymentTargetUrl
},
AllowUntrusted = true
});
});
任务依赖只是设置deployParameters对象。
在启用 Cake 的诊断详细程度的情况下,会在日志中生成以下命令(为清楚起见添加了新行):
"C:/Program Files/IIS/Microsoft Web Deploy V3/msdeploy.exe"
-verb:sync
-source:filePath="Y:/PathToBuildArtifact/Deploy/MyPowershell.ps1"
-dest:filePath="C:/Application - With Spaces/MyPowershell.ps1",wmsvc="https://deploy-server/msdeploy.axd",userName=msdeployuser,password=********
-enableRule:DoNotDeleteRule
-retryAttempts:3
-retryInterval:10000
-allowUntrusted
-postSync:runCommand="powershell -file 'C:\Application - With Spaces\MyPowershell.ps1' "
然后以错误结束:
警告:处理 -File ''C:/Application' 失败:不支持给定路径的格式。为 -File 参数指定一个有效路径。
我尝试在 postSync 命令中使用双引号的任何变体都会导致此错误:
错误:无法识别的参数“-”。
如果重要,这将在 Bamboo CI 服务器上完成。
【问题讨论】:
标签: c# powershell msdeploy cakebuild