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

Module can't be imported in PowerShell Core 7.0 #306

Closed detlefs closed 4 years ago

detlefs commented 4 years ago

I installed VSTeam 6.4.7

Steps to reproduce

Start PowerShell Core 7.0.0

Import-Module VSTeam

Expected behavior

Import of the module

Actual behavior

Error is displayed:

Import-Module: The format of value 'Module/ (Windows) PowerShell/7.0.0' is invalid.

Environment data

OS

Server

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
SebastianSchuetze commented 4 years ago

I tried it for me and I had no problems. I ran the command in the order you can see in the screenshot:

image

As you can see the only thing that is different is the build number for my Windows 10 OS. Do you have a VM or another PC available to try this?

I have even installed an Azure VM with your Windows Version and installed only PS core 7 and Module Version 6.4.7

Here is a log of my shell

image

I currently can't reproduce this issue on two different environments.

@DarqueWarrior can you check on your environment if you can reproduce it. If not, what's your suggestion. Close it or do we have other possibilities?

DarqueWarrior commented 4 years ago

I just tested installing from the PowerShell Gallery and everything worked as expected.

detlefs commented 4 years ago

Hi guys,

I did some more experiments today. Here's what I found: First of all, I updated PowerShell to 7.0.1 Then I tried again, importing the module. Still got the error. Next, I performed manually the statements from the VSTeam.psm1 file:

PowerShell 7.0.1
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/powershell
Type 'help' to get help.

PowerShell: 7.0.1 - 18.05.2020 13:34:45

Loading personal and system profiles took 3900ms.
§ asgnb007 {~} Import-Module VSTeam
Import-Module: The format of value 'Module/ (Windows) PowerShell/7.0.1' is invalid.
§ asgnb007 {~} Import-Module SHiPS
§ asgnb007 {~} . "C:\Program Files\PowerShell\Modules\VSTeam\6.4.7\vsteam.classes.ps1"
§ asgnb007 {~} . "C:\Program Files\PowerShell\Modules\VSTeam\6.4.7\vsteam.functions.ps1"
§ asgnb007 {~} [VSTeamVersions]::ModuleVersion = _getModuleVersion
§ asgnb007 {~} Set-VSTeamAPIVersion -Target $([VSTeamVersions]::Version)
§ asgnb007 {~} Import-Module VSTeam
§ asgnb007 {~}

As you can see, after the manual steps, the module imports without error.

I'm confused about this behavior. If it works manually, it should also work when done in the psm1 file...

SebastianSchuetze commented 4 years ago

I found the source of the problem. I get the same error if I called

Set-VSTeamAccount -Profile MyOrg
Set-VSTeamDefaultProject -Project "MyProject"

before I tried to import the module. What happens is that the environment var $env:TEAM_PROJECT is filled and then Get-VSTeamProject is called. In there under the _callAPI function is called which invokes _getUserAgent.

The returned value is for me

Invoke-RestMethod: F:\repos\razorspoint_github\vsteam\dist\vsteam.functions.ps1:267:15
Line |
 267 |        $resp = Invoke-RestMethod @params
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~
     | The format of value 'Module/ (Windows) PowerShell/7.0.1' is invalid.

So basically when I call Get-VSTeamProject or any other I get the same result. So the agent header must be wrong for PS7 + I think.

image

SebastianSchuetze commented 4 years ago

Ah now I found it. I think $([VSTeamVersions]::ModuleVersion) is returning and empty string or null.

https://github.com/DarqueWarrior/vsteam/blob/13ecf4255d273e9fed49b4c4bf1199bab412b0b3/Source/Private/common.ps1#L415

SebastianSchuetze commented 4 years ago

I will try to fix this.

SebastianSchuetze commented 4 years ago

I haven't tested it, but it seems like, the Get-VSTeamProject is called before [VSTeamVersions]::ModuleVersion = _getModuleVersion is called. That means the agent header never has the version in it.

SebastianSchuetze commented 4 years ago

Now I found out that the error does not appear when I change psm1 to the following:

# Files are built using a script in the root folder
. "$PSScriptRoot\vsteam.classes.ps1"
. "$PSScriptRoot\vsteam.functions.ps1"

# Set the module version
[VSTeamVersions]::ModuleVersion = _getModuleVersion

# Load the correct version of the environment variable
Set-VSTeamAPIVersion -Target $([VSTeamVersions]::Version)

# Check to see if the user stored the default project in an environment variable
if ($null -ne $env:TEAM_PROJECT) {
   # Make sure the value in the environment variable still exisits.
   if (Get-VSTeamProject | Where-Object ProjectName -eq $env:TEAM_PROJECT) {
      Set-VSTeamDefaultProject -Project $env:TEAM_PROJECT
   }
}

@detlefs could you try the same and then importing it from a fresh shell? Btw. your tip with the psm1 file helped me to track it down. I am not sure why it didn't appear before because in PS 5.x it causes no problems.

detlefs commented 4 years ago

I tried with the modified psm1 as per you last comment. It works now. The module imports and the functions can be used.

SebastianSchuetze commented 4 years ago

Ok cool. Then we have the solution. I will make a PR to correct this.

DarqueWarrior commented 4 years ago

Thanks for all the research and hard work on this @SebastianSchuetze I will review the PR now.