Running responsive gui with wokflows

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.
Post Reply
User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Running responsive gui with wokflows

Post by dlaurora » Wed Sep 11, 2019 1:00 pm

Hello Guys,

I have the below code, I'm trying to start multiple Azure Vms using workflow so I can use foreach with parallel property and start all at the same time, I tried to add it to a jobscript, but it only starts the first VM of the txt file, I have added the Params for the workflow, i've tested it printing the data into a textbox and it shows all the VMs listed inside the txt but, inside the jobscrpit it only start the first VM.

It is possible to do this? or I will have to run multiple jobs ? if that the case, can I assign, with a foreach, the vm as the name of the jobs?

Code: Select all

$VMs = Get-Content -Path $FileBrowser.FileName
		
		Add-JobTracker `
					   -Name "StartMultipleVms" `
					   -ArgumentList $VMs `
					   -JobScript {
			
			Param ($VMs)
			
			$servers = $Vms
			
			workflow Start-Azure-VM
			{
				
				Param ($VMs)
				
				ForEach -parallel ($VM in $VMs)
				{
					
					$res = (get-azvm | where { $_.Name -eq $vm }).ResourceGroupName
					If ((Get-AzVM -Status -ResourceGroupName $res -Name $vm).Statuses.DisplayStatus[1] -eq "VM Running")
					{
						Write-Output "`nVM $vm is already online."
					}
					else
					{
						
						$startvm = Start-AzVM -ResourceGroupName $res -Name $VM 
						
						Write-Output "VM $VM Was correctly started"
						
					}
				}
			}
			Start-Azure-VM $VMs
			
		} -CompletedScript {
			
			$results = Receive-Job -Name "StartMultipleVms"
			$EstadoVM.Text = $results | Out-String
			}
Thanks, best regards.

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

Re: Running responsive gui with wokflows

Post by jvierra » Wed Sep 11, 2019 2:08 pm

Try it like this to see what you get as a result.

Code: Select all

$VMs = Get-Content -Path $FileBrowser.FileName

Add-JobTracker -Name StartMultipleVms -ArgumentList $VMs -JobScript {
    Param ($VMs)
    
    workflow StartAzureVM {
        Param ($VMs)
        ForEach -parallel ($VM in $VMs) {
            Get-AzVm $vm
        }
    }
    StartAzureVM $VMs
    
} -CompletedScript {
    Param($job)
    $results = $job | Receive-Job
    $EstadoVM.Text = $results | Select name, ResourceGroup | Out-String
}

User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Re: Running responsive gui with wokflows

Post by dlaurora » Thu Sep 12, 2019 3:49 am

Hello,

Thanks for your reply, I've tried that and it is the same, it only brings me the info of the first value of the .txt, it is like the foreach isn't working. Also, don't know why, perhaps it is a property that I've modified, but the textbox got unresponsive I can't even scroll down :P

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

Re: Running responsive gui with wokflows

Post by jvierra » Thu Sep 12, 2019 9:51 am

I wanted you to use my code as written and not just assume that you know the answer.

Try the code in my post above. If that still fails then try it this way and see what happens:

Debugging requires patience.

User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Re: Running responsive gui with wokflows

Post by dlaurora » Thu Sep 12, 2019 11:04 am

I really copy your code and replace it in my form, then test it, still getting info about the first vm of the txt, I'm thinking that the Jobtracker stops as soon the first job of the workflow ends, I've tested the workflow outside the form in PS ISE, and what i've notice is that after finishing the first VM it returns its information and start with the next one.
Attachments
Screenshot_1.png
Screenshot_1.png (79.61 KiB) Viewed 59 times

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

Re: Running responsive gui with wokflows

Post by jvierra » Thu Sep 12, 2019 12:12 pm

Did you try the second version that does not call the VM? If that one fails then you have an issue with the form. Try a new form and carefully copy and add the code without changing anything else.

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

Re: Running responsive gui with wokflows

Post by jvierra » Thu Sep 12, 2019 12:28 pm

A little testing shows that even a simple loop no longer works when run as a job and uses a parallel task.

Even this fails at a command prompt.

Code: Select all

$VMs = 'one','two','three'

Start-Job -Name StartMultipleVms -ArgumentList $VMs -ScriptBlock {
    Param ($VMs)
    
    workflow StartAzureVM {
        Param ($VMs)
        ForEach  ($VM in $VMs) {
            #Get-AzVm $vm
            "Processing $VM"
        }
    }
    StartAzureVM $VMs
    
}
The issue is an old bug in the way PS handles array arguments. The following will work: (notice this change "-ArgumentList @(,$VMs)")

Code: Select all

Add-JobTracker  -Name StartMultipleVms -ArgumentList @(,$VMs) -ScriptBlock {
    Param ($VMs)
    
    workflow StartAzureVM {
        Param ($VMs)
        ForEach -parallel ($VM in $VMs) {
            #Get-AzVm $vm
            "Processing $VM"
        }
    }
    StartAzureVM $VMs
    
} -CompletedScript {
    Param($job)
    $results = $job | Receive-Job
    $EstadoVM.Text = $results | Select name, ResourceGroup | Out-String
}

User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Re: Running responsive gui with wokflows

Post by dlaurora » Fri Sep 13, 2019 4:41 am

With your correction adding the @(,$VMs) I receive the below error

ERROR: You cannot call a method on a null-valued expression.
WorkflowVMs.psf (171, 3): ERROR: At Line: 171 char: 3
ERROR: + [void]$JobTrackerList.Add($psObject)
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (:) [], RuntimeException
ERROR: + FullyQualifiedErrorId : InvokeMethodOnNull
ERROR:

User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Re: Running responsive gui with wokflows

Post by dlaurora » Fri Sep 13, 2019 5:59 am

I was able to fix the error after replacing the entire jobtracker framework, and now it shows me information about all the VMS, really thanks a lot for your help, this was pretty helpfull! :)

Post Reply