0

我有一个我试图从powershell调用的方法,它是

 Workspace.GetPendingChanges(string[] items)

这种方法有多个重载,但让我很难过的是:

 Workspace.GetPendingChanges(string items)
 Workspace.GetPendingChanges(string[] items)
 Workspace.GetPendingChanges(ItemSpec[] itemspecs)

我正在使用Find-FilesTFS 2015 的新构建任务中的 cmdlet 获取文件列表:

if ($ItemSpec.Contains("*") -Or $ItemSpec.Contains("?"))
{
    Write-Verbose "Pattern found in solution parameter. Calling Find-Files."
    Write-Verbose "Calling Find-Files with pattern: $ItemSpec"    
    [string[]] $FilesToCheckin = @(Find-Files -SearchPattern $ItemSpec -RootFolder $env:BUILD_SOURCESDIRECTORY)
    Write-Verbose "Found files: $FilesToCheckin"
}
else
{
    Write-Verbose "No Pattern found in solution parameter."
    [string[]] $FilesToCheckin = @($ItemSpec)
}

然后我打电话:

$pendingChanges = $provider.Workspace.GetPendingChanges( @($FilesToCheckin))

我已经尝试过我知道的任何强制[string[]]强制数组表示法版本@(),但是 find-files 的结果总是转换为单个长字符串,然后再传递给正确的重载:

System.Management.Automation.MethodInvocationException: Exception calling "GetPendingChanges" with "1" argument(s): "TF400889: The following path contains more than the allowed 259 characters: 

这是生成的“长字符串”

C:\TfsData\Build\_work\1\s\BuildProcessTemplates\AzureContinuousDeployment.11.xaml C:\TfsData\Build\_work\1\s\BuildProcessTemplates\DefaultTemplate.11.1.xaml C:\TfsData\Build\_work\1\s\BuildProcessTemplates\LabDefaultTemplate.11.xaml C:\TfsData\Build\_work\1\s\BuildProcessTemplates\UpgradeTemplate.xaml C:\TfsData\Build\_work\1\s\checkin-changes.ps1 C:\TfsData\Build\_work\1\s\update.txt C:\TfsData\Build\_work\1\s\updatefile.ps1. 

 

Specify a shorter path." ---> Microsoft.TeamFoundation.InvalidPathException: TF400889: The following path contains more than the allowed 259 characters: C:\TfsData\Build\_work\1\s\BuildProcessTemplates\AzureContinuousDeployment.11.xaml C:\TfsData\Build\_work\1\s\BuildProcessTemplates\DefaultTemplate.11.1.xaml C:\TfsData\Build\_work\1\s\BuildProcessTemplates\LabDefaultTemplate.11.xaml ...
    at System.IO.PathHelper.GetFullPathName()
    at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
    at System.IO.Path.GetFullPathInternal(String path)
    at Microsoft.TeamFoundation.Common.FileSpec.GetFullPathWrapper(String path)
    --- End of inner exception stack trace ---
    at Microsoft.TeamFoundation.Common.FileSpec.GetFullPathWrapper(String path)
    at Microsoft.TeamFoundation.Common.FileSpec.GetFullPath(String path, Boolean checkForIllegalDollar)
    at Microsoft.TeamFoundation.VersionControl.Common.VersionControlUtil.GetFullPath(String item, PathLength maxServerPathLength)
    at Microsoft.TeamFoundation.VersionControl.Client.ItemSpec..ctor(String item, RecursionType recursionType, Int32 deletionId)
    at Microsoft.TeamFoundation.VersionControl.Client.ItemSpec.FromStrings(String[] paths, RecursionType recursion)
    at 

这表明它正在调用正确的重载

Microsoft.TeamFoundation.VersionControl.Client.Workspace.GetPendingChanges(String[] items, RecursionType recursion, Boolean includeDownloadInfo)

该 cmdlet 返回List<string>以下内容:

protected override void ProcessRecord()
{
    base.ProcessRecord();
    if (!this.IncludeFiles && !this.IncludeFolders)
    {
        this.IncludeFiles = true;
    }
    List<string> sendToPipeline = FindFiles.FindMatchingFiles(this.RootFolder, this.SearchPattern, this.IncludeFiles, this.IncludeFolders);
    base.WriteObject(sendToPipeline, true);
}

为了确保我从调用中获得列表,Find-Files我添加了:

Write-Verbose "Found files: " -Verbose
Write-Verbose $FilesToCheckin.Count -Verbose

返回:7,正如预期的那样。

我的问题

如何防止 Powershell 在将结果find-files传递给之前将其转换为一个长字符串GetPendingChanges

项目可用,损坏的代码在底部注释掉:

4

2 回答 2

1

您是否尝试过通过反射调用该方法来强制正确的重载?就像是:

[Object[]] $parameters = @($stringArray)
$methods = $provider.Workspace.GetType().GetMethods() | where {$_.Name -eq "GetPendingChanges" -and $_.GetParameters()[0].ParameterType.ToString() -eq "System.String[]" } | Select-Object $_
$methods[0].Invoke($provider.Workspace, $parameters);
于 2016-01-04T19:50:02.400 回答
0

我不知道为什么会这样,但是在到处添加@()和强制转换并删除它之后:[string[]]Write-Verbose $FilesToCheckin

if ($ItemSpec.Contains("*") -Or $ItemSpec.Contains("?"))
{
    Write-Verbose "Pattern found in solution parameter. Calling Find-Files."
    Write-Verbose "Calling Find-Files with pattern: $ItemSpec"    
    [string[]] $FilesToCheckin = @(Find-Files -SearchPattern $ItemSpec -RootFolder $env:BUILD_SOURCESDIRECTORY)
}
else
{
    Write-Verbose "No Pattern found in solution parameter."
    [string[]] $FilesToCheckin = @($ItemSpec)
}

$pendingChanges = $provider.Workspace.GetPendingChanges( [string[]] @($FilesToCheckin) )
于 2016-01-04T19:22:40.487 回答