简体   繁体   中英

Visual Studio Code Powershell extension does not recognize profile while debugging

I'm using the Powershell extension for Visual Studio Code . I updated the profile:

C:\Users\xxxxxxxx\Documents\WindowsPowerShell\Microsoft.VSCode_profile.ps1

To include some functions and variables that I want to make available to other scripts. When I reference a variable from the profile within another script, it does not appear that the profile has been loaded. I suspect this because the variable value is blank when I query it from the VS Code console. If I run the same test from the standard Powershell console with an associated profile, the variable value is resolved.

Can anyone tell me what, if anything, I need to do to use a Powershell profile in VS Code while debugging?

There are several profiles. The starting point is the four locations that you can find by reading the following properties of PowerShell's built-in $profile variable.

$profile.CurrentUserAllHosts
$profile.CurrentUserCurrentHost
$profile.AllUsersCurrentHost
$profile.AllUsersAllHosts

As noted in this article by The Scripting Guy , because Windows has both Powershell and the Powershell ISE, you have at least two possible values of Current Host, so at least 6 profiles.

I've tested this in the Visual Studio Code terminal window, and it seems that the "CurrentHost" profiles are the same as you get by simply running a powershell instance. I'd assume then that Code isn't seen as a distinct host, and just runs a normal powershell. Once you've got that far, there's another possible complication, which is that the AllUsers profiles are down in C:\Windows\System32 and hence on a 64 bit system, also mirrored in C:\Windows\SysWOW64\ . So depending on whether you are using a 32 or 64 bit editor, and whether the Powershell is hosted in a 32 or 64 bit process, it is quite possible that the file you are editing has no influence on the Powershell.

Inside your debug session run: $profile . This will return the path to currently used profile file, so you can make your changes there.

Alternatively you could change the system wide profile in C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

With the just released version 0.10.0 of the VS Code PowerShell extension debugging with a previously loaded profile.ps1 being available is now implemented. Note that the interactive console and the debugger share the same PS session.

If you type $profile in the "PowerShell Integrated Console" of VSCode, you will see the path to the profile used: C:\Users\xxx\Documents\WindowsPowerShell\Microsoft.VSCode_profile.ps1

Now if you type $profile in a normal terminal, you will see your "normal" profile: C:\Users\xxx\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Now the easiest solution that did the trick for me is to create a hard link between the two files (the VSCode_profile.ps1 file didn't exist in my case): mklink /H Microsoft.VSCode_profile.ps1 Microsoft.PowerShell_profile.ps1

Reload VSCode and you are all set!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM