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.
User avatar
SanSiets
Posts: 7
Joined: Tue Feb 14, 2017 6:28 am

Different script execution between GUI and Non-GUI

Post by SanSiets » Mon Jun 19, 2017 4:10 am

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

User avatar
jvierra
Posts: 11363
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Different script execution between GUI and Non-GUI

Post by jvierra » Mon Jun 19, 2017 11:12 am

Can you post a sample of the code that fails?

User avatar
SanSiets
Posts: 7
Joined: Tue Feb 14, 2017 6:28 am

Re: Different script execution between GUI and Non-GUI

Post by SanSiets » Wed Jun 21, 2017 3:19 am

Hi jvierra,

The code is in my original post.

User avatar
jvierra
Posts: 11363
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Different script execution between GUI and Non-GUI

Post by jvierra » Wed Jun 21, 2017 3:23 am

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
Joined: Thu Jun 29, 2017 5:57 pm

Re: Different script execution between GUI and Non-GUI

Post by UsernameIsAlreadyTaken » Thu Jun 29, 2017 6:14 pm

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.

User avatar
jvierra
Posts: 11363
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Different script execution between GUI and Non-GUI

Post by jvierra » Thu Jun 29, 2017 7:24 pm

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
Joined: Thu Jun 29, 2017 5:57 pm

Re: Different script execution between GUI and Non-GUI

Post by UsernameIsAlreadyTaken » Thu Jun 29, 2017 7:49 pm

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...

User avatar
jvierra
Posts: 11363
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Different script execution between GUI and Non-GUI

Post by jvierra » Thu Jun 29, 2017 7:57 pm

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
Joined: Mon Feb 02, 2015 10:01 am

Re: Different script execution between GUI and Non-GUI

Post by andylyonette » Fri Jun 30, 2017 9:51 am

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

User avatar
jvierra
Posts: 11363
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Different script execution between GUI and Non-GUI

Post by jvierra » Fri Jun 30, 2017 1:31 pm

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?

Locked