Page 1 of 1

Assemblies not saving?

Posted: Thu Apr 27, 2017 10:50 am
by OldLost
PowerShell Studio 2017 v5.4.139 x64
Windows Server 2012 R2 64bit

I have a project with a PS1 file. While editing the PS1 file I click on Assemblies and, of course, at first it starts with the default list of assemblies. I click the plus to add one, click Browse... and select the DLL file in my project's folder, then close the dialog. I click Assemblies in the toolbar again and verify that indeed the DLL has been added to the list.

I now close the project and reopen it. I go back to my PS1 file and click Assemblies and the DLL in my project's folder is no longer present in the list.

What am I doing wrong? How do I get the added DLL to persist?

Re: Assemblies not saving?

Posted: Thu Apr 27, 2017 6:44 pm
by DevinL
I've managed to reproduce this on my end and have filed an internal bug report.

Thank you for bringing this to our attention. When I have some news to share, I'll be sure to post it here.

Re: Assemblies not saving?

Posted: Fri Apr 28, 2017 8:26 am
by DevinL
Hey OldLost,

I've heard back from the team and if you wish to add an assembly to a specific file, you must use the Add-Type cmdlet in that individual script.

If you wish for the entire project to have access, you would place the Add-Type cmdlet in your Startup.pss for the project.

Re: Assemblies not saving?

Posted: Fri Apr 28, 2017 11:10 am
by OldLost
I'm doing that for when the script runs, but I was looking for PSS to provide Intellisense/primalsense to the objects while coding. Add-Type doesn't help then, or at least it doesn't seem to work similarly to putting "Import-Module blah" for getting PSS to recognize module functions while coding.

In a single coding session I can add the DLL into the assemblies list and get intellisense/primalsense for the objects from the DLL, but as soon as I close the project and reopen it later PSS does not recognize the object properties or methods until I repeat the process of adding it to the assemblies list again.

Re: Assemblies not saving?

Posted: Mon May 01, 2017 1:06 pm
by DevinL
Currently, the only way I can reproduce this is if I use a path that has to be computed for the Assembly. For example:
  1. Add-Type -LiteralPath (Resolve-Path -Path .\MathLibrary.DLL)
I've noted this and will be finding out if we can resolve this, but in the meantime, if I use something like the following then it provides me with PrimalSense:
  1. Add-Type -LiteralPath .\MathLibrary.DLL
I've attached a sample project I made with a dll and the cs files I used to create it. Are you using a path like the first example by chance? If not, then can you please specify what you mean by not the same as the Import-Module support?
(4.46 KiB) Downloaded 120 times

Re: Assemblies not saving?

Posted: Tue May 02, 2017 7:51 am
by OldLost
Yes, in code I am doing something more like in your first example since I cannot be sure the current directory is $PSScriptRoot. If I do not the assembly may not be found.

What I meant by "Import-Module support" was that when I place an Import-Module command into the code that module's exported commands are now recognized by PrimalSense. Before I add such a statement they are not. Of course, with Add-Module there is no need to specify a path since it looks in $env:PSModulePath for the module. Add-Type does not have an equivalent variable where it searches for DLLs so typically to guarantee an assembly's file is found by Add-Type it is best to compute the assembly's path and use that with Add-Type. So, basically Add-Type would need something like $env:PSAddTypePath to avoid having to computer the assembly's location in the file system.

If the "Assemblies" feature in PSS worked (for this use-case) to allow an assembly's objects to be recognized by PrimalSense then there'd be no need to rely on having the Add-Type command with a literal path in code.

Do you have a coding style recommendation that would allow PrimalSense to work and also guarantee (as much as anything can be guaranteed :)) that during runtime the assembly file is located?

Re: Assemblies not saving?

Posted: Tue May 02, 2017 8:16 am
by OldLost
PS: assemblies can be loaded in other ways besides Add-Type. [Reflection.Assembly]::LoadFile(), for example. I do not think PSS would recognize assemblies loaded that way either so, again, the Assemblies feature within PSS would be the best way to go.

Also note that my project is a module project so does not have a Startup.pss file.

Re: Assemblies not saving?

Posted: Tue May 02, 2017 8:35 am
by davidc
If you don't want to use a path the only way is add the assembly to the machine's Global Assembly Cache. Then you can use the assembly's name instead of the path to invoke it in your script.
  1. Add-Type -Assembly 'AssemblyName'
Otherwise I recommend creating an installer for the script and place the necessary assemblies in the same directory as your script.

Re: Assemblies not saving?

Posted: Tue May 02, 2017 8:56 am
by OldLost
I tried your method of placing Add-Type -LiteralPath into the code, and while it does seem to work for getting PrimalSense to show the types and methods from the containing objects, it still feels like a kludge.

For example, I am also loading an assembly for EWS which may be different on each system so hard-coding in a path to the file would not work. So I end up doing this to get PrimalSense to work:
  1. # Below line here for PowerShell Studio PrimalSense only
  3. if ($false) { Add-Type -LiteralPath 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll' }
  5. # Now the real code
  7. $EWSDLL = (($(Get-ItemProperty -ErrorAction SilentlyContinue -Path Registry::$(Get-ChildItem -ErrorAction SilentlyContinue -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Exchange\Web Services' | Sort-Object Name -Descending | Select-Object -First 1 -ExpandProperty Name)).'Install Directory') + "Microsoft.Exchange.WebServices.dll")
  9. Add-Type -Path $EWSdll | Out-Null
Also, existing code is not syntax colored correctly for the added types. For example, here your DLL_Test.ps1 when loaded and after adding a new line of code:
5-2-2017 8-46-12 AM.jpg
5-2-2017 8-46-12 AM.jpg (55 KiB) Viewed 3571 times
As you can see, existing code is syntax colored as "Text" while newly added code is syntax colored correctly as "Type", but of course if it is saved and reloaded all of it is now syntax colored wrongly as "Text" again.