Azure / azure-powershell-migration

Tools and resources to help migrate PowerShell scripts between incompatible versions of Az and AzureRM modules
MIT License
35 stars 18 forks source link

VSCode Extension never recognizes that I have Az.Tools.Migration installed #112

Closed d-duer closed 11 months ago

d-duer commented 1 year ago

Component [X] VSCode extension

Description of the issue Every time I open up VSCode, I get the "You have to install Az.Tools.Migration firstly!" error. I do have it installed though. Below is a simplified version of the code that the extension uses in extension.ts and powershell.ts:

import * as fs from 'fs';
import * as path from 'path';

const PsModulePathes = process.env.PSMODULEPATH.split(";");
const moduleName = "Az.Tools.Migration";
const systemModulePath = PsModulePathes

console.log(systemModulePath.some(
    moduleFolder => fs.existsSync(path.resolve(moduleFolder, moduleName))))

When I run that through a typescript debugger within VSCode, it logs 'True'. I noticed that the shell the extension opens up uses -noProfile and executionPolicy: 'Bypass'. Using noprofile locally still returns the same paths in $env:psmodulepath, so I don't think that has anything to do with it. However, when I try to set my execution policy to bypass, I get the following:

Set-ExecutionPolicy : Access to the registry key                                                                        
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To change the execution   
policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option. To        change the 
execution policy for the current user, run "Set-ExecutionPolicy -Scope CurrentUser".                         At line:1 char:1                                                                                                        
+ Set-ExecutionPolicy                                                                                                   + ~~~~~~~~~~~~~~~~~~~                                                                                                       
+ CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], UnauthorizedAccessException                      + FullyQualifiedErrorId : 
System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyComma     nd

Could the extension be bombing out due to that error and not recognizing the existence of the module because of it?

alexandair commented 1 year ago

I'm experiencing the same problem for a couple of months. I've seen other people complaining too.

d-duer commented 1 year ago

So I was wrong about the 'Bypass' suggestion; that wasn't it. The output of process.env.PSMODULEPATH.split(";") when ran from the extension is ['C:\Program Files\WindowsPowerShell\Modules', 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules'], but my $env:PSModulePathwhen run from a Powershell window is C:\Users\dduer\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules. When ran from the Powershell terminal in VSCode, it's C:\Users\dduer\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files\powershell\7\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;c:\Users\dduer\.vscode\extensions\ms-vscode.powershell-2023.5.0\modules.

I don't know much about how environment variables work in difference scopes so I don't know the reason behind the differences, but I'm gonna see if I can figure it out.

Edit: Created https://github.com/Azure/azure-powershell-migration/pull/113/commits/5774312e5ea4a7cea22278045e77f5cef239333b to update the readme

d-duer commented 1 year ago

Even after fixing the module issue, I still can't get the extension to do any syntax highlighting. Think I'm just gonna give up on this and use the migration module by itself.

tccack commented 1 year ago

I have the same issue. Installed module without issues but each launch I get the error too that I need to install firstly.

stefanstranger commented 1 year ago

I also get this error message.

image

While I've the module installed in all possible locations.... image

Please fix this issue.

Stefan [MSFT]

stefanstranger commented 1 year ago

Resolved by running:

save-module az.tools.migration -Path C:\Windows\system32\WindowsPowerShell\v1.0\Modules
save-module az.tools.migration -Path "C:\program files\powershell\7\Modules"
save-module az.tools.migration -Path "C:\Program Files\WindowsPowerShell\Modules"

from elevated host.

tccack commented 1 year ago

Champion!!!

VeryEarly commented 11 months ago

Even after fixing the module issue, I still can't get the extension to do any syntax highlighting. Think I'm just gonna give up on this and use the migration module by itself.

Hi, the bug that extension doesn't work was fixed, please try az.tools.migration 2.0.0+

VeryEarly commented 11 months ago

Resolved by running:

save-module az.tools.migration -Path C:\Windows\system32\WindowsPowerShell\v1.0\Modules
save-module az.tools.migration -Path "C:\program files\powershell\7\Modules"
save-module az.tools.migration -Path "C:\Program Files\WindowsPowerShell\Modules"

from elevated host.

yes the version 2.0.0 is not installed in the expected location. I'll consider scan more possible locations. Thanks for reporting.