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

Support for all customers who have purchased a SAPIEN PowerShell Studio product license. This forum does not offer a response time guarantee.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
jstedler
Posts: 14
Joined: Sun Mar 03, 2013 12:40 pm

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

Post by jstedler » Fri Dec 22, 2017 11:00 am

Product, version and build: Powershell Studio 2017 (5.4.145)
32 or 64 bit version of product: 64 bit
Operating system:
32 or 64 bit OS: 64 bit

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.

Below is the .psf code (doesn't work - hangs):
  1. function Assign-Tags {
  2.  
  3.     param
  4.  
  5.     (
  6.  
  7.         [Parameter(Mandatory = $true)]
  8.  
  9.         [string]$TagName,
  10.  
  11.         [Parameter(Mandatory = $true)]
  12.  
  13.         [string]$CategoryName,
  14.  
  15.         [Parameter(Mandatory = $true)]
  16.  
  17.         [string]$Entity
  18.  
  19.     )
  20.  
  21.     # Verify tag exists and create if needed.
  22.  
  23.     try {
  24.  
  25.         # Check if the tag exists.
  26.  
  27.         $tag = Get-Tag -Name $TagName -Category $CategoryName -ErrorAction Stop
  28.  
  29.         write-host "Tag [$TagName] (Category: $CategoryName) already exists..."
  30.  
  31.     } catch {
  32.  
  33.         # Create the tag.
  34.  
  35.         write-host "Tag [$TagName] (Category: $CategoryName) does not exist... Creating..."
  36.  
  37.         try {
  38.  
  39.             $tag = New-Tag -Name $TagName -Category $CategoryName -ErrorAction Stop
  40.  
  41.             write-host "`t[$TagName] (Category: $CategoryName) created successfully!"
  42.  
  43.         } catch {
  44.  
  45.             write-host "`t***** ERROR [$TagName] (Category: $CategoryName) not created! *****"
  46.  
  47.             write-host $Error[0]
  48.  
  49.         }
  50.  
  51.     }
  52.  
  53.    
  54.  
  55.     # Assign a tag to the specified object.
  56.  
  57.     try {
  58.  
  59.         $null = New-TagAssignment -Tag $tag -Entity $Entity -ErrorAction Stop
  60.  
  61.         Write-Host "`tSuccessfully assigned tag [$TagName] (Category: $CategoryName) to [$Entity]!"
  62.  
  63.     } catch {
  64.  
  65.         write-host "`t***** ERROR [$TagName] (Category: $CategoryName) not assigned! *****"
  66.  
  67.         write-host $Error[0]
  68.  
  69.     }
  70.  
  71.    
  72.  
  73.     write-host ''
  74.  
  75. }
  76.  
  77.  
  78.  
  79. $button_Go_Click = {
  80.  
  81.    
  82.  
  83.     Connect-VIServer '<vCenterServer>' -User '<UID>' -Password '<PASSWORD>'
  84.  
  85.    
  86.  
  87.     $VMname = '<VM Name>'
  88.  
  89.    
  90.  
  91.     $OwnerTag = 'Generic User (jstest)'
  92.  
  93.     $CategoryName = 'Owner'
  94.  
  95.     Assign-Tags -TagName $OwnerTag -CategoryName $CategoryName -Entity $VMName
  96.  
  97.    
  98.  
  99.     $ManagerTag = 'Tom Thoma (thoma)'
  100.  
  101.     $CategoryName = 'Manager'
  102.  
  103.     Assign-Tags -TagName $ManagerTag -CategoryName $CategoryName -Entity $VMName
  104.  
  105.    
  106.  
  107.     $TemplateTag = 'CUCM2500_Test'
  108.  
  109.     $CategoryName = 'Template'
  110.  
  111.     Assign-Tags -TagName $TemplateTag -CategoryName $CategoryName -Entity $VMName
  112.  
  113.    
  114.  
  115.     Disconnect-VIServer * -Confirm:$false
  116.  
  117. }
Below is the same code, only in .ps1 form (minus the GUI/button) - this works!!!
  1. function Assign-Tags {
  2.  
  3.     param
  4.  
  5.     (
  6.  
  7.         [Parameter(Mandatory = $true)]
  8.  
  9.         [string]$TagName,
  10.  
  11.         [Parameter(Mandatory = $true)]
  12.  
  13.         [string]$CategoryName,
  14.  
  15.         [Parameter(Mandatory = $true)]
  16.  
  17.         [string]$Entity
  18.  
  19.     )
  20.  
  21.     # Verify tag exists and create if needed.
  22.  
  23.     try {
  24.  
  25.         # Check if the tag exists.
  26.  
  27.         $tag = Get-Tag -Name $TagName -Category $CategoryName -ErrorAction Stop
  28.  
  29.         write-host "Tag [$TagName] (Category $CategoryName) already exists..."
  30.  
  31.     } catch {
  32.  
  33.         # Create the tag.
  34.  
  35.         write-host "Tag [$TagName] (Category $CategoryName) does not exist... Creating..."
  36.  
  37.         try {
  38.  
  39.             $tag = New-Tag -Name $TagName -Category $CategoryName -ErrorAction Stop
  40.  
  41.             write-host "`t[$TagName] (Category $CategoryName) created successfully!"
  42.  
  43.         } catch {
  44.  
  45.             write-host "`t***** ERROR [$TagName] (Category $CategoryName) not created! *****"
  46.  
  47.             write-host $Error[0]
  48.  
  49.         }
  50.  
  51.     }
  52.  
  53.    
  54.  
  55.     # Assign a tag to the specified object.
  56.  
  57.     try {
  58.  
  59.         $null = New-TagAssignment -Tag $tag -Entity $Entity -ErrorAction Stop
  60.  
  61.         Write-Host "`tSuccessfully assigned tag [$TagName] (Category $CategoryName) to [$Entity]!"
  62.  
  63.     } catch {
  64.  
  65.         write-host "`t***** ERROR [$TagName] (Category $CategoryName) not assigned! *****"
  66.  
  67.         write-host $Error[0]
  68.  
  69.     }
  70.  
  71.    
  72.  
  73.     write-host ''
  74.  
  75. }
  76.  
  77.  
  78.  
  79.    
  80.  
  81. Connect-VIServer '<vCenterServer>' -User '<UID>' -Password '<PASSWORD>'
  82.  
  83.  
  84.  
  85. $VMname = '<VM Name>'
  86.  
  87.  
  88.  
  89. $OwnerTag = 'Generic User (jstest)'
  90.  
  91. $CategoryName = 'Owner'
  92.  
  93. Assign-Tags -TagName $OwnerTag -CategoryName $CategoryName -Entity $VMName
  94.  
  95.  
  96.  
  97. $ManagerTag = 'Tom Thoma (thoma)'
  98.  
  99. $CategoryName = 'Manager'
  100.  
  101. Assign-Tags -TagName $ManagerTag -CategoryName $CategoryName -Entity $VMName
  102.  
  103.  
  104.  
  105. $TemplateTag = 'CUCM2500_Test'
  106.  
  107. $CategoryName = 'Template'
  108.  
  109. Assign-Tags -TagName $TemplateTag -CategoryName $CategoryName -Entity $VMName
  110.  
  111.  
  112.  
  113. Disconnect-VIServer * -Confirm:$false

User avatar
Alexander Riedel
Posts: 6506
Joined: Tue May 29, 2007 4:43 pm

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

Post by Alexander Riedel » Fri Dec 22, 2017 11:09 am

On which line does it actually hang?
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
jstedler
Posts: 14
Joined: Sun Mar 03, 2013 12:40 pm

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

Post by jstedler » Fri Dec 22, 2017 11:13 am

I stated which line it hangs on in the original post:

"the script execution hangs/freezes forever when trying to execute the New-TagAssignment statement."

User avatar
davidc
Posts: 5133
Joined: Thu Aug 18, 2011 4:56 am

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

Post by davidc » Tue Dec 26, 2017 11:14 am

It could have to do with the internal working of the New-TagAssignment cmdlet. The GUI is holding up the pipeline and if the cmdlet depends on messaging to complete, it might freeze.

I recommend using the Button - Start Job control set to execute the call and see if that works for you.

https://info.sapien.com/index.php/guis/ ... sive-forms
David
SAPIEN Technologies, Inc.

User avatar
mxtrinidad
Site Admin
Posts: 99
Joined: Sun Mar 03, 2013 12:42 pm

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

Post by mxtrinidad » Tue Dec 26, 2017 11:51 am

Also, I may suggest to display the content of the $null variable which may hold (if any) the error message that the New-TagAssignment cmdlet may produce.
Just for troubleshooting purpose also remove the parameter '-ErrorAction stop'. Let it bomb out!

By the way, $null is a reserved variable. Please change it to something meaninful like $results. Then, check if is storing any errors in it.
Just in case!!

:)

User avatar
jstedler
Posts: 14
Joined: Sun Mar 03, 2013 12:40 pm

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

Post by jstedler » Mon Jan 08, 2018 12:57 pm

davidc wrote:
Tue Dec 26, 2017 11:14 am
It could have to do with the internal working of the New-TagAssignment cmdlet. The GUI is holding up the pipeline and if the cmdlet depends on messaging to complete, it might freeze.

I recommend using the Button - Start Job control set to execute the call and see if that works for you.

https://info.sapien.com/index.php/guis/ ... sive-forms
Sorry, I'm just now getting back from Christmas break and am working on this again. I looked at this link you've provided but this doesn't help. I'm not looking to use the 'button - start job' control because the logic I've provided for you above is executed as part of a long script. I've provided you with the form/button setup to simply demonstrate that the NEW-TAGASSIGNMENT cmdlet hangs forever when using a form or form project, but works perfectly in simple powershell script (.ps1).
To me, this is more indicative of a GUI/Powershell Studio bug in the handling of the cmdlet rather than a problem with the cmdlet itself (since it DOES work in .ps1 files).

User avatar
jstedler
Posts: 14
Joined: Sun Mar 03, 2013 12:40 pm

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

Post by jstedler » Mon Jan 08, 2018 1:20 pm

mxtrinidad wrote:
Tue Dec 26, 2017 11:51 am
Also, I may suggest to display the content of the $null variable which may hold (if any) the error message that the New-TagAssignment cmdlet may produce.
Just for troubleshooting purpose also remove the parameter '-ErrorAction stop'. Let it bomb out!

By the way, $null is a reserved variable. Please change it to something meaninful like $results. Then, check if is storing any errors in it.
Just in case!!

:)
Thanks for the reply. In the course of my troubleshooting before posting here, I've essentially tried what you've suggested. Taking away the "$null = " and "-ErrorAction Stop" does nothing to change the behavior of freezing forever.
I understand the $null variable is reserved, but it's a more efficient way of suppressing cmdlet output that you don't want to see (compared to piping to Out-Null).
No errors are ever displayed, in fact NOTHING is ever displayed when trying to execute the New-TagAssignment cmdlet in any 'form' scenario (as opposed to the working .ps1 scenario). Also goes without saying, it never bombs out either... :-/

User avatar
jstedler
Posts: 14
Joined: Sun Mar 03, 2013 12:40 pm

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

Post by jstedler » Mon Jan 08, 2018 6:56 pm

So after a full day of trying everything I can think of to workaround this problem, I'm no closer than I started... I've tried various methods to get around the freezing issue by using Invoke-Expression, Invoke-Command, Start-Job, etc. all without any success. Once again, any method I get working only works with .ps1 files not any form/GUI files (.psf)... Here is an example of using Invoke-Command that does work, but again, only with .ps1 files not .psf:
  1.     Invoke-Command -ScriptBlock {
  2.  
  3.             param
  4.  
  5.             (
  6.  
  7.             $tag,
  8.  
  9.             [string]$entity
  10.  
  11.             )
  12.  
  13.             New-TagAssignment -Tag $tag -Entity $entity
  14.  
  15.     } -ArgumentList $tag, $Entity
At this point, I'm completely stuck without anymore ideas to try. Can someone please help?

User avatar
mxtrinidad
Site Admin
Posts: 99
Joined: Sun Mar 03, 2013 12:42 pm

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

Post by mxtrinidad » Tue Jan 09, 2018 7:20 am

Quick question... How did you create the Form? Meaning, did you create the form using "New Form", or the "New Form Project"?
Let us know which one.

User avatar
jstedler
Posts: 14
Joined: Sun Mar 03, 2013 12:40 pm

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

Post by jstedler » Tue Jan 09, 2018 10:21 am

mxtrinidad wrote:
Tue Jan 09, 2018 7:20 am
Quick question... How did you create the Form? Meaning, did you create the form using "New Form", or the "New Form Project"?
Let us know which one.
I've tried both ways. The original way I found the problem was with an existing "form project". After a whole lot of troubleshooting, I wanted to eliminate the potential that my original project was somehow to blame, so I created a new form using "New Form" -> Blank form.
From there I simply added a single button control to the form that would run the code (which is what I've provided) to test the situation with the cleanest scenario possible. Again, even with this very simple setup, the cmdlet freezes/hangs.
So again, I tried the exact same code that I put in the "1 button form" in a regular powershell file (.ps1) and the code worked perfectly. This is how I've determined that the cmdlet works as intended from a .ps1 file, but it demonstrates the freeze/hang behavior in any form/GUI scenario (.psf).