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

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION 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.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
jstedler
Posts: 17
Last visit: Tue Jan 31, 2023 10:46 am

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

Post by jstedler »

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.     param
  3.     (
  4.         [Parameter(Mandatory = $true)]
  5.         [string]$TagName,
  6.         [Parameter(Mandatory = $true)]
  7.         [string]$CategoryName,
  8.         [Parameter(Mandatory = $true)]
  9.         [string]$Entity
  10.     )
  11.     # Verify tag exists and create if needed.
  12.     try {
  13.         # Check if the tag exists.
  14.         $tag = Get-Tag -Name $TagName -Category $CategoryName -ErrorAction Stop
  15.         write-host "Tag [$TagName] (Category: $CategoryName) already exists..."
  16.     } catch {
  17.         # Create the tag.
  18.         write-host "Tag [$TagName] (Category: $CategoryName) does not exist... Creating..."
  19.         try {
  20.             $tag = New-Tag -Name $TagName -Category $CategoryName -ErrorAction Stop
  21.             write-host "`t[$TagName] (Category: $CategoryName) created successfully!"
  22.         } catch {
  23.             write-host "`t***** ERROR [$TagName] (Category: $CategoryName) not created! *****"
  24.             write-host $Error[0]
  25.         }
  26.     }
  27.    
  28.     # Assign a tag to the specified object.
  29.     try {
  30.         $null = New-TagAssignment -Tag $tag -Entity $Entity -ErrorAction Stop
  31.         Write-Host "`tSuccessfully assigned tag [$TagName] (Category: $CategoryName) to [$Entity]!"
  32.     } catch {
  33.         write-host "`t***** ERROR [$TagName] (Category: $CategoryName) not assigned! *****"
  34.         write-host $Error[0]
  35.     }
  36.    
  37.     write-host ''
  38. }
  39.  
  40. $button_Go_Click = {
  41.    
  42.     Connect-VIServer '<vCenterServer>' -User '<UID>' -Password '<PASSWORD>'
  43.    
  44.     $VMname = '<VM Name>'
  45.    
  46.     $OwnerTag = 'Generic User (jstest)'
  47.     $CategoryName = 'Owner'
  48.     Assign-Tags -TagName $OwnerTag -CategoryName $CategoryName -Entity $VMName
  49.    
  50.     $ManagerTag = 'Tom Thoma (thoma)'
  51.     $CategoryName = 'Manager'
  52.     Assign-Tags -TagName $ManagerTag -CategoryName $CategoryName -Entity $VMName
  53.    
  54.     $TemplateTag = 'CUCM2500_Test'
  55.     $CategoryName = 'Template'
  56.     Assign-Tags -TagName $TemplateTag -CategoryName $CategoryName -Entity $VMName
  57.    
  58.     Disconnect-VIServer * -Confirm:$false
  59. }
Below is the same code, only in .ps1 form (minus the GUI/button) - this works!!!
  1. function Assign-Tags {
  2.     param
  3.     (
  4.         [Parameter(Mandatory = $true)]
  5.         [string]$TagName,
  6.         [Parameter(Mandatory = $true)]
  7.         [string]$CategoryName,
  8.         [Parameter(Mandatory = $true)]
  9.         [string]$Entity
  10.     )
  11.     # Verify tag exists and create if needed.
  12.     try {
  13.         # Check if the tag exists.
  14.         $tag = Get-Tag -Name $TagName -Category $CategoryName -ErrorAction Stop
  15.         write-host "Tag [$TagName] (Category $CategoryName) already exists..."
  16.     } catch {
  17.         # Create the tag.
  18.         write-host "Tag [$TagName] (Category $CategoryName) does not exist... Creating..."
  19.         try {
  20.             $tag = New-Tag -Name $TagName -Category $CategoryName -ErrorAction Stop
  21.             write-host "`t[$TagName] (Category $CategoryName) created successfully!"
  22.         } catch {
  23.             write-host "`t***** ERROR [$TagName] (Category $CategoryName) not created! *****"
  24.             write-host $Error[0]
  25.         }
  26.     }
  27.    
  28.     # Assign a tag to the specified object.
  29.     try {
  30.         $null = New-TagAssignment -Tag $tag -Entity $Entity -ErrorAction Stop
  31.         Write-Host "`tSuccessfully assigned tag [$TagName] (Category $CategoryName) to [$Entity]!"
  32.     } catch {
  33.         write-host "`t***** ERROR [$TagName] (Category $CategoryName) not assigned! *****"
  34.         write-host $Error[0]
  35.     }
  36.    
  37.     write-host ''
  38. }
  39.  
  40.    
  41. Connect-VIServer '<vCenterServer>' -User '<UID>' -Password '<PASSWORD>'
  42.  
  43. $VMname = '<VM Name>'
  44.  
  45. $OwnerTag = 'Generic User (jstest)'
  46. $CategoryName = 'Owner'
  47. Assign-Tags -TagName $OwnerTag -CategoryName $CategoryName -Entity $VMName
  48.  
  49. $ManagerTag = 'Tom Thoma (thoma)'
  50. $CategoryName = 'Manager'
  51. Assign-Tags -TagName $ManagerTag -CategoryName $CategoryName -Entity $VMName
  52.  
  53. $TemplateTag = 'CUCM2500_Test'
  54. $CategoryName = 'Template'
  55. Assign-Tags -TagName $TemplateTag -CategoryName $CategoryName -Entity $VMName
  56.  
  57. Disconnect-VIServer * -Confirm:$false
User avatar
Alexander Riedel
Posts: 8488
Last visit: Tue Apr 16, 2024 8:42 am
Answers: 20
Been upvoted: 37 times

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

Post by Alexander Riedel »

On which line does it actually hang?
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
jstedler
Posts: 17
Last visit: Tue Jan 31, 2023 10:46 am

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

Post by jstedler »

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: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

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

Post by davidc »

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
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

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

Post by mxtrinidad »

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: 17
Last visit: Tue Jan 31, 2023 10:46 am

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

Post by jstedler »

davidc wrote: Tue Dec 26, 2017 10: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: 17
Last visit: Tue Jan 31, 2023 10:46 am

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

Post by jstedler »

mxtrinidad wrote: Tue Dec 26, 2017 10: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: 17
Last visit: Tue Jan 31, 2023 10:46 am

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

Post by jstedler »

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.             param
  3.             (
  4.             $tag,
  5.             [string]$entity
  6.             )
  7.             New-TagAssignment -Tag $tag -Entity $entity
  8.     } -ArgumentList $tag, $Entity
At this point, I'm completely stuck without anymore ideas to try. Can someone please help?
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

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

Post by mxtrinidad »

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: 17
Last visit: Tue Jan 31, 2023 10:46 am

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

Post by jstedler »

mxtrinidad wrote: Tue Jan 09, 2018 6: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).
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.