Venafi / VenafiPS

Powershell module to fully automate your Venafi TLS Protect Datacenter and Cloud platforms!
https://venafips.readthedocs.io/
Apache License 2.0
18 stars 7 forks source link

Class TppObject is referenced before it is available #196

Closed Saadi6 closed 1 year ago

Saadi6 commented 1 year ago

Environment

Operating System: Windows or MacOS
VenafiPS version: 5.4.1
PowerShell version: 5.1 or 7.1
TPP version (if applicable): 22.4.1

Steps to reproduce

  1. Create a new VenafiSession
  2. Use Find-TppObject to find objects in TPP for e.g. $BasicAppObjs = Find-TppObject -Class 'Basic' -Path '\VED\Policy\Installations' -Recursive

Expected behavior

Requested objects are returned.

Actual behavior

Following error is thrown

Unable to find type [TppObject].
At C:\Program Files\Venafi\Scripts\Modules\VenafiPS\Public\Find-TppObject.ps1:98 char:17
+     [OutputType([TppObject])]
+                 ~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (TppObject:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

More Info

An object of a custom class TppObject is Find-TppObject function's OutputType, however, this class is not loaded/available when the function is used. If line 98 in Find-TppObject is commented out then calling this function is successful.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 60 days with no activity.

3sch3r54 commented 1 year ago

I was having this issue with my script using v5.4.0, but after seeing issue #117 I updated my script to Import-Module with -Force and this resolved the problem. Unfortunately, through testing one of my co-workers loaded the latest module of 5.5.1 and had issues with Get-TppPermission. So, we updated the script to check for v5.4.0 and create an error if it did not exist. If it did exist, we would load the module like: Import-Module -Name VenafiPS -RequiredVersion 5.4.0 -Force

Unfortunately, after we did this, we cannot get past this Get-TppPermission error, unless we manually comment out the Line 98, which we really would prefer not to do. I finally found if we just go back to Import-Module -Name VenafiPS -Force we no longer face the issue. Unfortunately, this means we will have to update the script and ensure that ONLY v5.4.0 is installed.

As stated by @Saadi6 it seems it's an issue with line 98, it would really help us if this issue can be resolved. Of course, with new version we will need to figure out why Get-TppPermission stopped working.

3sch3r54 commented 1 year ago

I saw there was an open branch related to this issue, reviewing the updates in it I found the TppObject type was loaded with Classes\TppObject.ps1. I tested my script still using the RequiredVersion that was causing my problems then tested. Once it errored on Find-TppObject I then manually loaded the 'Classes/TppObject.ps1' and then rerun the command, which was successful.

So, seems I can just manually load this in my script and have a workaround and still have multiple versions installed. I hope this helps identify the issue and resolve it.

Saadi6 commented 1 year ago

@3sch3r54 the issue lays with the way PowerShell loads classes from modules. Basically, it is not reliable, which is discussed at length here.

As per @gdbarron suggestion, 'using module' command solves this issue, however, using module command must appear before any other code in your script and it does not accept variables, so it is not a particularly elegant solution to this problem. I guess you can also catch this particular error in your code when you first use a function that loads TppObject or another object from a class in VenafiPS. In your catch statement you can try dot sourcing the class for its source file and then retrying your statement. This is not a great solution either but if you really cannot use the 'using module' command then this can potentially work.