2 Replies Latest reply on Jan 12, 2018 10:57 AM by jstedler5480

    New-TagAssignment works in .ps1, but not in .psf...

    jstedler5480 Novice

      I have a very simple function I've created that creates Tags (if they don't exist), then assigns them to a VM. I created and tested this code in a normal .ps1 file first before porting it over to the Form Project. As stated in the subject, this code works perfectly fine in a .ps1, but if I try to add the exact same code to my Form Project, or even a bare-minimum .psf (with a single button to 'GO'), the script execution hangs/freezes forever when trying to execute the New-TagAssignment statement.


      PLEASE NOTE: to test the code, change the Connect-VIServer and $VMname lines to be applicable to your environment.


      I'm attaching 2 files to demonstrate what I'm talking about.  The code to do the Tag query and assignment are the same between the two files.  The only difference is one file is a regular .ps1 whereas the other file contains a GUI (.psf). 

      1. Doesn't_Work.psf - This is a very stripped down version of the GUI from my actual project. This file only contains the form and a single "GO" button to execute the code.  Even in this very basic version, the script hangs/freezes forever when trying to execute the New-TagAssignment cmdlet.
      2. Working.ps1 - This is strictly the code without a GUI.  This works perfectly...


      After doing a tremendous amount of troubleshooting on my own, contacting Sapien support (the IDE I use for GUI powershell projects - Powershell Studio 2017, and massive Googling, I've come to the conclusion that there must be a bug in the cmdlet that is causing the hang/freeze.  There aren't any messages (error or otherwise) displayed; just straight up hangs/freezes forever.


      I've seen many people with similar issues like I have (all concerning the "Tag" cmdlets), but I haven't seen any real/working resolution.


      Here is the requested version info:


      Name                       Value
      PSVersion                  5.1.16299.98
      PSEdition                  Desktop
      PSCompatibleVersions       {1.0, 2.0, 3.0, 4.0...}
      BuildVersion               10.0.16299.98
      CLRVersion                 4.0.30319.42000
      WSManStackVersion          3.0
      PSRemotingProtocolVersion  2.3




      vCenter ServerVersionBuild
        • 1. Re: New-TagAssignment works in .ps1, but not in .psf...
          LucD Guru
          vExpertUser ModeratorsCommunity Warriors

          When you use forms in your script, you are of course running in a different environment (additional DLLs loaded).

          I have no fix for you issue, but you indeed seem to be not alone in seeing issues when combining forms and Tags.

          See for example PowerCLI - New-TAG hangs in a script


          As a possible alternative, you could try with the cmdlets in the rCisTag module.

          These are based on the REST API, and might not experience the issue.

          • 2. Re: New-TagAssignment works in .ps1, but not in .psf...
            jstedler5480 Novice

            I just wanted to follow-up on this post especially since others are having similar problems.


            A Sapien support person pointed me to a potential workaround they are suggesting to people having this problem.  The link is here:



            When I started to play around with this, it wasn't exactly what I was looking for as it added a TON of 'extra' code that is nice if needed, but for what I am wanting, it was extraneous.  However, it did introduce me to the Timer control and how Jobs can be used in conjunction with them.


            So without all the extra code, I was able to simply add a "Timer" control to my form, used a normal Button control to kick the script off, and created a Job to execute the Tag portion of code utilizing the Timer I added. Voila it works!


            It still seems to me (I could be wrong) that this behavior points to either a bug or unintended interaction between the Tag cmdlets and powershell scripts utilizing GUI's. The release notes for the previous version of PowerCLI even includes notes that they were fixing other issues with the Tag cmdlets so maybe Vmware will correct this issue as well since many others are having the same issue.


            I hope this helps others.  It's taken me quite a bit to get this working but it does indeed work...