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.
This topic is 4 years and 6 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
dlaurora
Posts: 31
Last visit: Wed Oct 02, 2019 12:51 pm

Running responsive gui with wokflows

Post by dlaurora »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Running responsive gui with wokflows

Post by jvierra »

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
Last visit: Wed Oct 02, 2019 12:51 pm

Re: Running responsive gui with wokflows

Post by dlaurora »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Running responsive gui with wokflows

Post by jvierra »

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
Last visit: Wed Oct 02, 2019 12:51 pm

Re: Running responsive gui with wokflows

Post by dlaurora »

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 2551 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Running responsive gui with wokflows

Post by jvierra »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Running responsive gui with wokflows

Post by jvierra »

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
Last visit: Wed Oct 02, 2019 12:51 pm

Re: Running responsive gui with wokflows

Post by dlaurora »

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
Last visit: Wed Oct 02, 2019 12:51 pm

Re: Running responsive gui with wokflows

Post by dlaurora »

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! :)
This topic is 4 years and 6 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