简体   繁体   English

在 Powershell 本地工作的 -split ' ' 在 Azure 管道 Powershell 任务中不起作用

[英]-split ' ' that works locally in Powershell does not work in a Azure pipeline Powershell task

I am using git diff command to get the file that changed between the last 2 commits and based on the action have to do different things with the files.我正在使用 git diff 命令来获取在最后 2 次提交之间更改的文件,并且根据操作必须对文件执行不同的操作。 So I have to extract the action on the file and the filename.所以我必须提取对文件和文件名的操作。

For a renamed file:对于重命名的文件:

The result of git diff command is the below which is a renamed swagger file from swaggerA.json to swaggerB.json git diff 命令的结果如下,这是一个从swaggerA.jsonswaggerB.json的重命名的 swagger 文件

R100 swaggerA.json swaggerB.json R100 swaggerA.json swaggerB.json

This is my code:这是我的代码:

$files=$(git diff HEAD~ HEAD --name-status) 
$temp=$files -split ' '
echo $temp 
echo $temp.Length
$name=$temp[1]
echo "this is $name file"
write-host $name 
$output=$name.Split(' ')
$length=$output.Length
$output[0] 
$output[1] 
$output[$length-1]

The above script should've ideally split and provided 3 parts as:理想情况下,上面的脚本应该已经拆分并提供了 3 个部分:

1) R100 1) R100

2) swaggerAjson 2)招摇Ajson

3) swaggerB.json 3) swaggerB.json

But it doesn't do that in the Powershell task of Azure pipeline, and gives the whole string each time.但是它在 Azure 管道的 Powershell 任务中并没有这样做,并且每次都给出整个字符串。

git diff [cid]..[cid] --name-status outputs lines of tab-separated values, so splitting on a normal space won't help you: git diff [cid]..[cid] --name-status输出制表符分隔值的行,因此在正常空间上拆分对您没有帮助:

$changes = git diff HEAD~ HEAD --name-status |ForEach-Object {
    # split on tab "`t"
    $change,$orig,$new = $_ -split "`t"
    [pscustomobject]@{
        Change = $change
        Original = $orig
        Current = $new
    }
}

Because the gid diff returnes a string with hidden ASCII symbol ( `t ), if we copy the results to Notepad++ (and in the settings we press on 'Show all characters') we can see it:因为gid diff返回一个带有隐藏 ASCII 符号 ( `t ) 的字符串,如果我们将结果复制到 Notepad++(并在设置中按下“显示所有字符”),我们可以看到它:

在此处输入图片说明

So we need to replace it with regular space before the split:所以我们需要在拆分之前用常规空间替换它:

$files = $files -replace '[^\p{L}\p{Nd}/(/./_]', ' '

Now it will work and you will get the string splitted :)现在它将起作用,您将分割字符串:)

暂无
暂无

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

相关问题 为什么 powershell env 可以在本地运行,但不能与 dockerfile 一起使用? - Why does powershell env works locally but not with dockerfile? Github 发布 Azure 管道任务 - tagpattern 通配符不起作用 - Github Release Azure Pipeline task - tagpattern wildcards does not work Azure DevOps、发布管道、Powershell 任务:即使操作成功,提取 .tar.gz 仍显示为失败 - Azure DevOps, Release Pipeline, Powershell task: Extracting .tar.gz shown as failure even after operation succeeds 使用 Powershell 脚本在 Azure 上配置 CI/CD 管道 - configuration CI/CD pipeline on Azure with Powershell Script Azure DevOps:使用“git clone”的 Powershell 管道无法正常工作 - Azure DevOps: Pipeline with Powershell using "git clone" not working Git Bash 不适用于 npm、node、ng 命令,但所有这些命令都适用于 powershell windows 10 - Git Bash does not work for npm, node, ng commands but all these commands works in powershell windows 10 如何从 Azure DevOps powershell 任务运行 git push 命令 - How to run git push command from Azure DevOps powershell task 在发布管道中即时通讯时,如何从Azure PowerShell访问Git Repo - How can i access Git Repo from Azure PowerShell when im in a Release Pipeline 如何使用 PowerShell 任务 (git clone) 在 Azure DevOps Server 发布管道中运行 Git 命令 - How to run Git commands in Azure DevOps Server release pipeline using PowerShell tasks (git clone) PowerShell git push作为计划任务 - PowerShell git push as a scheduled task
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM