Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code $psise equivalent

I'm looking for a way to replicate the $psISE functionality in VSCode, at least as far as discovering the filename of the open, active file in the editor.

As background:

I'm trying to migrate into using VSCode for powershell editing instead of the ISE since VSCode is the future. We're having issues with migrating some workflows (specifically signing scripts). In the ISE, I have created a function that uses the $psise.currentfile.fullpath so that we can easily sign the file that's open in the ISE. I'd like to recreate this for vscode, but I'm not finding any way to determine what the open file is. We tend to run this interactively (i.e. we're testing in the ISE, get it to where we want to test on another box, and sign it by just typing Set-CoSAuthenticodeSignature, which is a custom function that wraps set-authenticodesignature but has defaults set and automatically finds the file, if we don't specify), so we don't want to have to type the path name all the time, especially since we don't have to now.

Thanks!

like image 201
Danny Avatar asked May 30 '17 15:05

Danny


People also ask

Does Visual Studio Code have a wysiwyg editor?

Visual Studio Code Extension for Tizen Web supports WYSIWYG Design Editor features such as Preview, JavaScript Assistant, Structure View, New Page, HTML Assistant, Undo, and Redo.

How do I run a PS1 code in Visual Studio?

Once it is saved as a PS1, VS Code will identify the file as a PowerShell script. From there, you can execute the PowerShell script by press F5 . You can also click the Run button on the top right of the editor. To run a select, you can press F8 or right click on the selection and click the Run Selection option.

Is VS Code and VSCodium same?

Brief: VSCodium is a clone of Microsoft's popular Visual Studio Code editor. It's identical to VS Code with the single biggest difference that unlike VS Code, VSCodium doesn't track your usage data. Microsoft's Visual Studio Code is an excellent editor not only for web developers but also for other programmers.

How do I enable auto format in VS Code?

VS Code Auto Format On SaveOpen Visual Studio Code editor. Click the “Settings” gear icon in the bottom-left corner. Search “Formatter” and click the “Editor: Default Formatter” option. From the drop-down menu, select whichever code formatter you want to use.


2 Answers

$ScriptPath=''
if($ScriptPath -eq '' -and $MyInvocation.MyCommand.CommandType -eq 'ExternalScript'){$ScriptPath = (Split-Path -Path $MyInvocation.MyCommand.Source                  -Parent)} # Running from File
if($ScriptPath -eq '' -and (Test-Path variable:psISE)) {                             $ScriptPath = (Split-Path -Path $psISE.CurrentFile.FullPath                     -Parent)} # Running from ISE
if($ScriptPath -eq '' -and (Test-Path variable:psEditor)) {                          $ScriptPath = (Split-Path -Path $psEditor.GetEditorContext().CurrentFile.Path   -Parent)} # Running from Visual Studio Code
like image 167
Brian S Avatar answered Nov 15 '22 23:11

Brian S


I think it's not implemented yet (the $psise). You can file an issue on the vscode-powershell Project for VSCode. A similar one has already been filed.

At least you might use something like $context = [Microsoft.Powershell.EditorServices.Extensions.EditorContext]$psEditor.GetEditorContext()

Then you can access the current file... $context.CurrentFile.

See also this video for an introduction...

like image 38
Peter Schneider Avatar answered Nov 16 '22 00:11

Peter Schneider