MethodsAndPractices / vsteam

PowerShell module for accessing Azure DevOps Services and Azure DevOps Server (formerly VSTS or TFS)
https://methodsandpractices.github.io/vsteam-docs/
MIT License
445 stars 155 forks source link

Project Won't Validate #363

Closed shazaman23 closed 3 years ago

shazaman23 commented 3 years ago

In an org that has more than 100 projects, if your project doesn't show up in the first 100 results of Get-VsTeamProject, commands that use a project parameter fail with the error Cannot validate argument on parameter 'Project'. 'myproject' is invalid

Steps to reproduce

# Use an AZDO account with more than 100 projects in it
# Pick a project (myproject in this example) that doesn't show up when running Get-VsTeamProject
Set-VsTeamDefaultProject 'myproject' -Verbose

Expected behavior

VERBOSE: Performing the operation "Set-VSTeamDefaultProject" on target "myproject".

Actual behavior

Set-VSTeamDefaultProject: Cannot validate argument on parameter 'Project'. 'myproject' is invalid

Environment data

OS

Server

> Get-VSTeamAPIVersion
Name                           Value
----                           -----
ExtensionsManagement           6.0-preview
Version                        VSTS
Release                        5.1
Graph                          6.0-preview
DistributedTaskReleased        5.1
Pipelines                      5.1-preview
Policy                         5.1
Core                           5.1
ServiceEndpoints               5.0-preview
Tfvc                           5.1
DistributedTask                6.0-preview
Git                            5.1
MemberEntitlementManagement    6.0-preview
TaskGroups                     6.0-preview
Build                          5.1
HierarchyQuery                 5.1-preview
Packaging                      6.0-preview
VariableGroups                 5.1-preview.1
Processes                      6.0-preview
> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.0.3
PSEdition                      Core
GitCommitId                    7.0.3
OS                             Darwin 19.6.0 Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
shazaman23 commented 3 years ago

Just a comment on this, in the example provided you should not be able to see myproject in the results if you run Get-VsTeamProject but you do see myproject in the results if you run Get-VsTeamProject -top 500

SebastianSchuetze commented 3 years ago

Thx @shazaman23. Just to clarify, you are saying that without using the top parameter you expect that there is a better error message?

Can you clarify your expected behavior a bit?

morrell commented 3 years ago

I'm having this issue on several of the cmdlets. It seems anything that is trying to validate the projectName if it is not listed within the first 100 results.

The following command is an example of this behavior, which worked as expected in the 6.5.1 version of the module.

Import-Module vsteam -MaximumVersion 7.1.1
Get-VSTeamProject -Name "RxSourcing"      
Get-VSTeamProject : Cannot validate argument on parameter 'Name'. 'RxSourcing' is invalid
At line:1 char:25
+ Get-VSTeamProject -Name "RxSourcing"
+                         ~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-VSTeamProject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-VSTeamProject

remove-module vsteam

Import-Module vsteam -MaximumVersion 6.5.1
Get-VSTeamProject -Name "RxSourcing"
Name       Description
----       -----------
RxSourcing A DevOps project
morrell commented 3 years ago

I'm having this issue on several of the cmdlets. It seems anything that is trying to validate the projectName if it is not listed within the first 100 results.

The following command is an example of this behavior, which worked as expected in the 6.5.1 version of the module.

Import-Module vsteam -MaximumVersion 7.1.1
Get-VSTeamProject -Name "RxSourcing"      
Get-VSTeamProject : Cannot validate argument on parameter 'Name'. 'RxSourcing' is invalid
At line:1 char:25
+ Get-VSTeamProject -Name "RxSourcing"
+                         ~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-VSTeamProject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-VSTeamProject

remove-module vsteam

Import-Module vsteam -MaximumVersion 6.5.1
Get-VSTeamProject -Name "RxSourcing"
Name       Description
----       -----------
RxSourcing A DevOps project

Possibly is related to the InternalCache not caching all of the projects?

https://github.com/MethodsAndPractices/vsteam/blob/trunk/Source/Classes/Cache/InternalCache.cs#L61

shazaman23 commented 3 years ago

Right. As @morrell mentioned, it is occurring on any command that takes the project name as a parameter. I can pass the top flag to the Get-VsTeamProject to return enough results to see my project, but when I run Set-VSTeamDefaultProject it says that I am using an invalid project name. I believe this is because the module is only caching the top 100 projects and using those to validate the project name parameter on any command.

jhoneill commented 3 years ago

The quickest short term fix is to edit vsteam.functions.ps1 , search for the line function Get-VSteamProject and about 8 lines below that change $top = 100 to a big enough number to get all the projects. Save and reload.

@DarqueWarrior can make a call at a later point on the value of Get-VSteamProject only returning some projects byDefault, IMHO it should be all by default and paged if -TOP is specified and non zero, but I don't know why the limit was put in so there may be a good reason.

shazaman23 commented 3 years ago

I am also able to resolve this momentarily by importing the version 6.5.1 of the module (where this was still working). But we may want to use the latest version soon so I was hoping this issue could be resolved without running a custom build of the module.

DarqueWarrior commented 3 years ago

I will be working on this soon. Thanks for the detailed issue.

DarqueWarrior commented 3 years ago

Another solution that does not require changing any code is to set a default value for Top.

$Global:PSDefaultParameterValues["*-vsteam*:top"] = 500

This will pass 500 to Top.

SebastianSchuetze commented 3 years ago

I close this since with an earlier commit a new cmdlet was added that lets you set the default $top parameter number for projects.

leefw commented 2 years ago

I experienced this error recently. Using the source code for function Get-VSteamProject (referred to earlier by @jhoneill in this thread) as inspiration, my fix was to set the _$env:TEAMPROJECTCOUNT value to something greater than 100 so.

This worked for me:

$env:TEAM_PROJECTCOUNT = 200

leefw commented 2 years ago

$env:TEAM_PROJECTCOUNT = 200

But probably better to use the Set-VSTeamDefaultProjectCount which seems to do the same thing (I noticed by chance)