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

import-module vsteam loses previously stored configuration #493

Closed mnieto closed 1 year ago

mnieto commented 1 year ago

Steps to reproduce

Once configured the module with Set-VSTeamAccount:

Set-VSTeamAccount -Account myOrganization -PersonalAccessToken myToken -Version AzD -Level User
Set-VSTeamDefaultProject MyProject

If we close the powersell session and open a new one:

 Get-ChildItem env:team*

output

Name                           Value
----                           -----
TEAM_PROJECT                   MyProject
TEAM_VERSION                   AzD
TEAM_ACCT                      https://dev.azure.com/myOrganization
TEAM_PAT                       OmljeH...................WE=

Then, import the module and test the env variables:

import-module vsteam
Get-ChildItem env:team*

output now is:

Name                           Value
----                           -----
TEAM_ACCT                      https://dev.azure.com/siemensgamesa
TEAM_PAT                       Ok9tbGplS..........................zk0WVhaM2RqSnJaemMxZEhwc2FHWTN…

We lost the version, default project and TEAM_PAT has changed Because the last one, we cannot connect to azure services and we need to run Set-VSTeamAccount in each new powershell session

Expected behavior

Configuration is recovered from previous sessions

Actual behavior?

TEAM_PAT has change and default project name and Version are lost

We cannot connect to azure services and we need to run Set-VSTeamAccount in each new powershell session

This happen in version 7.9.0

On Which OS have you tried it?

Windows

What was your server version?

Azure DevOps Services

Other server version

No response

Log output of used API

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
Wiki                        : 6.0
WorkItemTracking            : 6.0-preview.1

Log output of $PSVersionTable

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.6
PSEdition                      Core
GitCommitId                    7.2.6
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
mnieto commented 1 year ago

I think the problem was introduced in commit 0528c76 due #467 and closed with #480

While loading the module, if there is a default project, it executes Set-VSTeamAccount -Account $env:TEAM_ACCT -PersonalAccessToken $env:TEAM_PAT

But:

I managed to make it work with

      # set vsteam account to initialize given variables properly
      $pat = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($env:TEAM_PAT))   #decode base64 stored pat
      $pat = $pat.Substring(1)                                                                           #remove the leading :
      $defaultProject = $env:TEAM_PROJECT                                                                #save temporary defalt project because is removed during Set-VSTeamAccount call
      Set-VSTeamAccount -Account $env:TEAM_ACCT -PersonalAccessToken $pat -Version $env:TEAM_VERSION
      # Make sure the value in the environment variable still exisits.
      if (Get-VSTeamProject | Where-Object ProjectName -eq $defaultProject) {
         Set-VSTeamDefaultProject -Project $defaultProject
      }
      else {
         Write-Warning "The default project '$defaultProject' stored in the environment variable TEAM_PROJECT does not exist."
      }

but still not resolved the issue that security was configured with -UserBearerToken parameter instead -PersonalAccessToken

bmkaiser commented 1 year ago

What's the best way to get this resolved? I'm currently working around this by adding the following to my PowerShell profile after importing the VSTeam module:

Set-VSTeamAccount -Account 'Foo' -Level 'User' -SecurePersonalAccessToken $securePAT
Set-VSTeamDefaultProject -Project 'Bar' -Level 'User'

Much thanks for @mnieto for identifying the issue and creating pull request https://github.com/MethodsAndPractices/vsteam/pull/508!