Different script execution between GUI and Non-GUI

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

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 8 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.
Locked
User avatar
SanSiets
Posts: 11
Last visit: Tue May 04, 2021 3:50 pm

Different script execution between GUI and Non-GUI

Post by SanSiets »

Hi all,

I have a script:
$creds = Get-Credential
Login-AzureRmAccount -Credential $creds -ServicePrincipal -TenantId 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$VMname = "xx-xx-xxxxx"
$ResourceGroupName = "xxxxxx"
Start-AzureRmVm -ResourceGroupName $ResourceGroupName -Name $VMname

This script runs fine as a script and my VMware machine is started. But when I use the exact same code in the GUI it doesn't work. I don't get an error, script just freezes.

Hope that someone has a solution for this.

Greetings,
Sander
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Different script execution between GUI and Non-GUI

Post by jvierra »

Can you post a sample of the code that fails?
User avatar
SanSiets
Posts: 11
Last visit: Tue May 04, 2021 3:50 pm

Re: Different script execution between GUI and Non-GUI

Post by SanSiets »

Hi jvierra,

The code is in my original post.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Different script execution between GUI and Non-GUI

Post by jvierra »

Have you stepped through with the debugger to see where it is hanging?
How are you running it in the form? Is it a button or some other control or event?
User avatar
UsernameIsAlreadyTaken
Posts: 2
Last visit: Thu Jun 29, 2017 7:49 pm

Re: Different script execution between GUI and Non-GUI

Post by UsernameIsAlreadyTaken »

This started doing this for me too, which is how I found this post while searching around for a fix. At least I know it's not me or my local machine.

The Start-AzureRmVm line just freezes forever. All other lines before it setting up the Start-AzureRmVm line work. There are no buttons like the other guy asked, just real simple code like in your example.

You're actually more lucky than me, as it locks up no matter if I run the line in the gui or not. The vm starts fine, but it totally locks up powershell, breaking or hitting the stoprun button doesn't unfreeze it. I have to totally kill powershell to get out of it. It used to work a few weeks ago, so M$ must have made a change that locks it up. Like you, I sure hope somebody finds a solution.

BTW, what location is your vm in? Mine is in West US 2, and it seems to take an abnormal amount of time to start and stop the vm.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Different script execution between GUI and Non-GUI

Post by jvierra »

What threading model are you using? What version of WMF?

More information would help to find a solution although many PS modules cannot be run from GUI.
User avatar
UsernameIsAlreadyTaken
Posts: 2
Last visit: Thu Jun 29, 2017 7:49 pm

Re: Different script execution between GUI and Non-GUI

Post by UsernameIsAlreadyTaken »

I've answered my own question, I hate it when I do that! (OK, actually I don't... :P )

start-job {
$creds = Get-Credential
Login-AzureRmAccount -Credential $creds -ServicePrincipal -TenantId 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$VMname = "xx-xx-xxxxx"
$ResourceGroupName = "xxxxxx"
Start-AzureRmVm -ResourceGroupName $ResourceGroupName -Name $VMname
}

This will pull up the login prompt for you, and then lets the Start-AzureRmVm lock up in the background, yuk, yuk. Don't try to 'stop-job' afterwards, it locks powershell up again...
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Different script execution between GUI and Non-GUI

Post by jvierra »

No one has mentioned "Start-Job" in this thread. If you have an issue with this then please open a new thread with the details of your issue. This user has a different issue.
User avatar
andylyonette
Posts: 4
Last visit: Tue Jan 11, 2022 2:03 pm

Re: Different script execution between GUI and Non-GUI

Post by andylyonette »

I'm seeing the same issue with Get-AzureRmRoleAssignment - just locked up the app.

If I run it in debug mode I can see it gets stuck straight away:
DEBUG: 17:44:38 - GetAzureRoleAssignmentCommand begin processing with ParameterSet 'ScopeWithSignInNameParameterSet'.
DEBUG: 17:44:38 - using account id '###UserID###'..

compared with just running it in a PS console window:
DEBUG: 17:49:47 - GetAzureRoleAssignmentCommand begin processing with ParameterSet 'ScopeWithSignInNameParameterSet'.
DEBUG: 17:49:48 - using account id '##UserId###'...
DEBUG: [Common.Authentication]: Authenticating using Account: '###UserID###, environment: 'AzureCloud', tenant: '###TenantID###'
DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
...... and so on

The user is prompted to log into Azure interactively using Login-AzureRmAccount and other Azure cmdlets such as Get-AzureRmSubscription work fine.

I'm running Windows 10 Pro build 15063 x64 and have tested with multiple version of the PowerSHell module released this year. What do I need to do next to try and narrow this down further?

Thanks,
Andy
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Different script execution between GUI and Non-GUI

Post by jvierra »

The outstanding question is still - What threading model are you using.
Also simplify this to a simple form with one button. Cop script to clipboard and paste into CLI PowerShell. Does the same thing happen?
This topic is 6 years and 8 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.
Locked