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
444 stars 155 forks source link

Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.at ~\Modules\VSTeam\7.4.0\vsteam.functions.ps1:266 #426

Closed RubenOchando closed 2 years ago

RubenOchando commented 2 years ago

Steps to reproduce

last month I complete this gist with possitive success Security seems ok , https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/git-commands?view=azure-devops&tabs=yaml#allow-scripts-to-access-the-system-token This week I get the error

Script

Connect-AzAccount Select-AzSubscription Get-module -name VSTeam $PAT_test='somePersonaroken' Set-VSTeamAccount -Account https://dev.azure.com/myorganization -PersonalAccessToken $PAT_test $Project= "az-400" Get-VSTeamReleaseDefinition -ProjectName $Project -verbose -Debug | Format-List *



Expected behavior
-----------------
Be able to complete gist , like past month

Actual behavior
---------------
WARNING: An error occurred: The remote server returned an error: (401) Unauthorized.
Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
At C:\Program Files\WindowsPowerShell\Modules\VSTeam\7.4.0\vsteam.functions.ps1:266 char:18
+          $resp = Invoke-RestMethod @params
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Environment data
----------------

OS

- [ ] macOS
- [X] Windows
- [ ] Linux

Server

- [ ] TFS 2017
- [ ] TFS 2018
- [ ] Azure DevOps Server
- [X] Azure DevOps Service

<!-- provide the output of $VSTeamVersionTable -->
$VSTeamVersionTable ----> N/A ????

> Get-VSTeamAPIVersion

Billing                     : 5.1-preview.1
Build                       : 5.1
Core                        : 5.1
DistributedTask             : 6.0-preview
DistributedTaskReleased     : 5.1
ExtensionsManagement        : 6.0-preview
Git                         : 5.1
Graph                       : 6.0-preview
HierarchyQuery              : 5.1-preview
MemberEntitlementManagement : 6.0-preview
Packaging                   : 6.0-preview
Pipelines                   : 5.1-preview
Policy                      : 5.1
Processes                   : 6.0-preview
Release                     : 5.1
ServiceEndpoints            : 5.0-preview
TaskGroups                  : 6.0-preview
Tfvc                        : 5.1
VariableGroups              : 5.1-preview.1
Version                     : VSTS

<!-- provide the output of $PSVersionTable -->

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1237
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1237
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
SebastianSchuetze commented 2 years ago

Hi, the gist does not contain any VSTeam cmdlets. Did you reference the wrong one?

Update: I also don't know what the gist and your referenced doc page has to do with this Module other that it's both for Azure DevOps.

RubenOchando commented 2 years ago

Added part of the script that I am using

SebastianSchuetze commented 2 years ago

On other question. You said it suddenly stopped working, even without update. I would be surprised if it is coming from the module.

Currently, I would expect that the PAT is changed or expired. Or the user the PAT is coming from does not have the permissions anymore.

Did you also check this?

SebastianSchuetze commented 2 years ago

I tested the script myself and created test project and test release definition as well as running your other suggested cmdlets. It is working form me fine. So I can't reproduce it.

PS C:\Users\SebastianSSchuetze> Get-VSTeamInfo

Name                           Value
----                           -----
ModuleVersion                  7.4.0
DefaultProject
DefaultTimeout
Version                        VSTS
DefaultProjectCount
Account                        https://dev.azure.com/DEV-DB-AG

PS C:\Users\SebastianSSchuetze> Get-VSTeamReleaseDefinition -ProjectName AutoScalerTest -verbose -Debug | Format-List *
VERBOSE: Team Module/7.4.0 (Windows) PowerShell/7.1.5
VERBOSE: GET https://vsrm.dev.azure.com/dev-db-ag/AutoScalerTest/_apis/release/definitions?api-version=5.1 with 0-byte payload
VERBOSE: received 1951-byte response of content type application/json
VERBOSE: Content encoding: utf-8
VERBOSE: return type: PSCustomObject
VERBOSE: @{count=1; value=System.Object[]}

Url               : https://vsrm.dev.azure.com/DEV-DB-AG/7ed69873-74d5-42fa-854e-1427be79c244/_apis/Release/definitions
                    /1
Path              : \
Revision          : 1
Tags              :
Description       :
IsDeleted         : False
Triggers          :
Artifacts         :
Variables         :
Properties        :
Environments      :
VariableGroups    :
ReleaseNameFormat : Release-$(rev:r)
CreatedOn         : 05.11.2021 04:48:14
ModifiedOn        : 05.11.2021 04:48:14
CreatedBy         : Sebastian Schütze
ModifiedBy        : Sebastian Schütze
Links             : Self: https://vsrm.dev.azure.com/DEV-DB-AG/7ed69873-74d5-42fa-854e-1427be79c244/_apis/Release/defin
                    itions/1
                    Web: https://dev.azure.com/DEV-DB-AG/7ed69873-74d5-42fa-854e-1427be79c244/_release?definitionId=1

ResourceType      : ReleaseDefinition
CreatedByUser     : Sebastian Schütze
Id                : 1
ProjectName       : AutoScalerTest
InternalObject    : @{source=userInterface; revision=1; description=; createdBy=; createdOn=05.11.2021 04:48:14;
                    modifiedBy=; modifiedOn=05.11.2021 04:48:14; isDeleted=False; variableGroups=;
                    releaseNameFormat=Release-$(rev:r); properties=; id=1; name=New release pipeline; path=\;
                    projectReference=; url=https://vsrm.dev.azure.com/DEV-DB-AG/7ed69873-74d5-42fa-854e-1427be79c244/_a
                    pis/Release/definitions/1; _links=}
DisplayMode       : ------
Name              : New release pipeline

PS C:\Users\SebastianSSchuetze> Get-VSTeamSecurityNamespace

Name                           IsRemotable Id
----                           ----------- --
Analytics                      False       58450c49-b02d-465a-ab12-59ae512d6531
AnalyticsViews                 False       d34d3680-dfe5-4cc6-a949-7d9c68f73cba
ReleaseManagement              False       7c7d32f7-0e86-4cd6-892e-b35dbba870bd
ReleaseManagement              False       c788c23e-1b46-4162-8f5e-d7585343b5de
AuditLog                       False       a6cc6381-a1ca-4b36-b3c1-4e65211e82b6
Identity                       False       5a27515b-ccd7-42c9-84f1-54c998f03866
WorkItemTrackingAdministration False       445d2788-c5fb-4132-bbef-09c4045ad93f
DistributedTask                True        101eae8c-1709-47f9-b228-0e476c35b3ba
Git Repositories               True        2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87
VersionControlItems2           True        3c15a8b7-af1a-45c2-aa97-2cb97078332e
EventSubscriber                True        2bf24a2b-70ba-43d3-ad97-3d9e1f75622f
WorkItemTrackingProvision      False       5a6cd233-6615-414d-9393-48dbb252bd23
ServiceEndpoints               False       49b48001-ca20-4adc-8111-5b60c903a50c
ServiceHooks                   True        cb594ebe-87dd-4fc9-ac2c-6a10a4c92046
Collection                     True        3e65f728-f8bc-4ecd-8764-7e378b19bfa7
Proxy                          False       cb4d56d2-e84b-457e-8845-81320a133fbb
Plan                           False       bed337f8-e5f3-4fb9-80da-81e17d06e7a8
Process                        False       2dab47f9-bd70-49ed-9bd5-8eb051e59c02
AccountAdminSecurity           False       11238e09-49f2-40c7-94d0-8f0307204ce4
Library                        False       b7e84409-6553-448a-bbb2-af228e07cbeb
Environment                    True        83d4c2e6-e57d-4d6e-892b-b87222b7ad20
Project                        True        52d39943-cb85-4d7f-8fa8-c6baac873819
EventSubscription              False       58b176e7-3411-457a-89d0-c6d0ccb3c52b
CSS                            True        83e28ad4-2d72-4ceb-97b0-c7726d5502c3
TeamLabSecurity                False       9e4894c3-ff9a-4eac-8a85-ce11cafdc6f1
ProjectAnalysisLanguageMetrics False       fc5b7b85-5d6b-41eb-8534-e128cb10eb67
Tagging                        False       bb50f182-8e5e-40b8-bc21-e8752a1e7ae2
MetaTask                       True        f6a4de49-dbe2-4704-86dc-f8ec1a294436
Iteration                      False       bf7bfa03-b2b7-47db-8113-fa2e002cc5b1
WorkItemQueryFolders           False       71356614-aad7-4757-8f2c-0fb3bff6f680
Favorites                      False       fa557b48-b5bf-458a-bb2b-1b680426fe8b
Registry                       False       4ae0db5d-8437-4ee8-a18b-1f6fb38bd34c
Graph                          False       c2ee56c9-e8fa-4cdd-9d48-2c44f697a58e
ViewActivityPaneSecurity       False       dc02bf3d-cd48-46c3-8a41-345094ecc94b
Job                            False       2a887f97-db68-4b7c-9ae3-5cebd7add999
WorkItemTracking               False       73e71c45-d483-40d5-bdba-62fd076f7f87
StrongBox                      False       4a9e8381-289a-4dfd-8460-69028eaa93b3
Server                         False       1f4179b3-6bac-4d01-b421-71ea09171400
TestManagement                 True        e06e1c24-e93d-4e4a-908a-7d951187b483
SettingEntries                 False       6ec4592e-048c-434e-8e6c-8671753a8418
BuildAdministration            True        302acaca-b667-436d-a946-87133492041c
Location                       False       2725d2bc-7520-4af4-b0e3-8d876494731f
Boards                         False       251e12d9-bea3-43a8-bfdb-901b98c0125e
OrganizationLevelData          False       f0003bce-5f45-4f93-a25d-90fc33fe3aa9
UtilizationPermissions         False       83abde3a-4593-424e-b45f-9898af99034d
WorkItemsHub                   False       c0e7a722-1cad-4ae6-b340-a8467501e7ce
WebPlatform                    False       0582eb05-c896-449a-b933-aa3d99e121d6
VersionControlPrivileges       False       66312704-deb5-43f9-b51c-ab4ff5e351c3
Workspaces                     False       93bafc04-9075-403a-9367-b7164eac6b5c
CrossProjectWidgetView         False       093cbb02-722b-4ad6-9f88-bc452043fa63
WorkItemTrackingConfiguration  False       35e35e8e-686d-4b01-aff6-c369d6e36ce0
Discussion Threads             False       0d140cae-8ac1-4f48-b6d1-c93ce0301a12
BoardsExternalIntegration      False       5ab15bc8-4ea1-d0f3-8344-cab8fe976877
DataProvider                   False       7ffa7cf4-317c-4fea-8f1d-cfda50cfa956
Social                         False       81c27cc8-7a9f-48ee-b63f-df1e1d0412dd
Security                       False       9a82c708-bfbe-4f31-984c-e860c2196781
IdentityPicker                 False       a60e0d84-c2f8-48e4-9c0c-f32da48d5fd1
ServicingOrchestration         False       84cc1aa4-15bc-423d-90d9-f97c450fc729
Build                          True        33344d9c-fc72-4d6f-aba5-fa317101a7e9
DashboardsPrivileges           False       8adf73b7-389a-4276-b638-fe1653f7efc7
VersionControlItems            True        a39371cf-0841-4c16-bbd3-276e341bc052

PS C:\Users\SebastianSSchuetze> Get-VSTeamRelease
PS C:\Users\SebastianSSchuetze> Get-VSTeamPolicy -ProjectName AutoScalerTest
PS C:\Users\SebastianSSchuetze>
RubenOchando commented 2 years ago

I have tested these commands

Not working

Get-VSTeamSecurityNamespace Get-VSTeamRelease Get-VSTeamReleaseDefinition Get-VSTeamPolicy Get-VSTeamRelease

Working command

Get-VSTeamInfo

Name Value


Account https://dev.azure.com/XXXXXXXXXX Version VSTS DefaultProject ModuleVersion 7.4.0 DefaultProjectCount DefaultTimeout

SebastianSchuetze commented 2 years ago

Thanks, I have understood that. But unless I can't reproduce it I have to assume that it is not a problem with the Module. As you can see in the copied output, I never got an unauthorized error. This error usually leads to the a invalid PAT is provided or the permission of the user are insufficient.

That is the only thing I can say. I have tested it also with different tenants and users.

Unless we can find a way to properly reproduce it at any time I have no way of fixing it.

RubenOchando commented 2 years ago

I will check with my security group, thanks for the assistance