Debugging VDS

IMG_0708

Are you running Windows 10? Then I have a good news for you! You have already PowerShell 5 installed, and with that also the capability of debugging Vault Data Standard step by step. You are not running Windows 10? Then continue reading, as PowerShell 5 can be also installed on earlier Windows version.

With PowerShell 5, Microsoft introduced new command-lets that lets you connect to a running application, such as Vault, Inventor or AutoCAD and debug the PowerShell runspace hosted by such application. By doing so, the according PowerShell script will be opened in your PowerShell editor, so that you can execute the code line by line, see the current state of variables, and see how the code behaves. This is brilliant if you have a nasty problem with your code and like to figure it out.

Let’s see how this works. Let’s suppose you like to debug the Vault Data Standard New File dialog. Start the PowerShell ISE and let’s connect to the Vault application. For this purpose, there is a new command-let Enter-PsHostProcess -Name <ProcessName>, where the <ProcessName> is in this case Connectivity.VaultPro. You can enter Enter-PSHostProcess -Name Connectivity.VaultPro either in the command line or write the code in an empty script, select the line and execute it. You will notice that your command prompt changes from PS C:\>, to something like [Process:9436]: PS… . This means that you are now connected to the PowerShell engine of the hosting application. It is possible that the hosting application, in our case Vault, might work with several runspaces, which is the case for VDS for Vault. With Get-Runspace you can ask the list of available runspaces. You may get something like this:

2016-08-25_08-14-37

As you can see, here we have several runspaces. One of these is called RemoteHost – this is our PowerShell editor, so we can ignore it. The more interesting are the other runspaces. The question is, which runspace is the one we like to debug? In order to know, which one is the right one, we can use a trick. We bring up a message box from with the VDS code that tells us the runspace ID and also holds the code at that position, so that we have the time to start the debug mode with such runspace. For this purpose, I wrote a little function called ShowRunspaceID, that looks like this:

function ShowRunspaceID
{
            $id = [runspace]::DefaultRunspace.Id
            $app = [System.Diagnostics.Process]::GetCurrentProcess()
            [System.Windows.Forms.MessageBox]::Show("application: $($app.name)"+[Environment]::NewLine+"runspace ID: $id")
}

Just create a new file, for instance ShowRunspaceID.ps1, copy paste the code into the file and save the file nearby the Default.ps1 file. Restart Vault, in order that the new file is loaded by VDS. Now you can add the ShowRunSpaceID at any place in your code. As an example, you can add it at the beginning of the InitializeWindow function like this

2016-08-25_09-23-06

Create now a new file or folder in Vault via VDS and you should see the message box with the application name and the runspace ID. Now get back into your PowerShell Editor and enter the command Debug-Runspace -id <ID>, where <ID> is the ID provided by the message box. A short message should “welcome” you in the debug mode. Now, just close the message box, so that the code can continue. You will notice that the editor will open up the PowerShell script file and highlight the line that is currently executed. Now you can move on step by step either with F10 (step over) of F11 (step into). In order to exit the debug mode, you can either stop debugging in the PowerShell editor or just type detach in the console.

In summary, these are the two command-lets that you need in order to start debugging your VDS code:

Enter-PSHostProcess -Name Connectivity.VaultPro
Debug-Runspace -id 50

Just set the process name and the sunspace ID accordingly to the text in the message box.

We hope this new capability will help you to quicker identify issues in your code or help you understand the behavior of your VDS environment. Enjoy!

 

This entry was posted in Data Standard, PowerShell. Bookmark the permalink.

One Response to Debugging VDS

  1. Pingback: powerTree | coolorange

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s