New PsSession with a Progress Bar

Ask your PowerShell-related questions, including questions on cmdlet development!
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 5 years and 1 month 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
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

New PsSession with a Progress Bar

Post by bhnuser »

Hey,
at the moment i try to add a progress bar to my project. This progress bar should show the progress of a connection to my AD- and Exchange-Server.

I have already looked up here in the forum but can not find a solution for me.

How the progress bar can synchronous with the progress of the function[Connection to the AD-Server] below?

Code: Select all

$global:ADSession = New-PSSession -ComputerName $DomainController -Credential $Credentials
		if ($?)
		{
			Invoke-Command $ADSession -Scriptblock { Import-Module ActiveDirectory }
			Import-PSSession $ADSession -ErrorAction Stop
		} 
Thanks,
bhnuser
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New PsSession with a Progress Bar

Post by jvierra »

Unfortunately I have no idea wat you are trying to ask.

You have many jobs running at once. If you use all of them to set the progress it will be very jumpy.

A workflow can do all of what you are doing and set the progress bar correctly.

The JobTracker control set can manage many jobs but it is not intended to do what you are trying to do.
User avatar
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

Re: New PsSession with a Progress Bar

Post by bhnuser »

Im so sorry. But what i like to ask is how to build a progress bar like this: viewtopic.php?f=12&t=12486&p=67236&hili ... ion#p67236
MDinice has a progress bar showing the progress of a connection to the server. This is what i would like to ad to my project because i and the employee can not see the progress of the connection and still wait without any response / progress.

MDinice does this with the Measure-Command. But i would like to have it synchron between the progress bar and the build of the connection
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New PsSession with a Progress Bar

Post by jvierra »

Here is how to run multiple remote jobs. There is no need for sessions or any other items. Just run the jobs and gather the results.

Code: Select all

PS C:\scripts> $job = Invoke-Command -ScriptBlock {dir c:\} -AsJob -ComputerName ws701,ws703, sbs01
PS C:\scripts> $job.ChildJobs

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
9      Job9                            Completed     True            ws701                dir c:\
10     Job10                           Completed     True            ws703                dir c:\
11     Job11                           Completed     True            sbs01                dir c:\


PS C:\scripts> Get-Job | Receive-Job


    Directory: C:\


Mode                LastWriteTime         Length Name                                                        PSComputerName
----                -------------         ------ ----                                                        --------------
d-----        3/21/2013   7:39 PM                apps                                                        ws701
d-----        3/31/2017   6:01 PM                dell                                                        ws701
d-----        3/21/2013   9:01 PM                Drivers                                                     ws701
d-----        3/21/2013   9:15 PM                Intel                                                       ws701
Each row comes back with the target system name attached. All errors and issues are summarized in the "Job" object's "ChildJobs". Every job runs in parallel on the remote systems.

Using the UpdateScript to read the results will allow you to correctly set the progress bar without conflict.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New PsSession with a Progress Bar

Post by jvierra »

bhnuser wrote: Wed Feb 06, 2019 11:25 pm Im so sorry. But what i like to ask is how to build a progress bar like this: viewtopic.php?f=12&t=12486&p=67236&hili ... ion#p67236
MDinice has a progress bar showing the progress of a connection to the server. This is what i would like to ad to my project because i and the employee can not see the progress of the connection and still wait without any response / progress.

MDinice does this with the Measure-Command. But i would like to have it synchron between the progress bar and the build of the connection
Why do you need a connection? Your code uses WMI which does its own remoting more efficiently.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New PsSession with a Progress Bar

Post by jvierra »

There are a couple of links in the topic you are referencing. They explain how to use the ProgressBar control.
1. Since the process bar is animated, it might take a little time before the animation completes. This also depends on how you are handling the loop with the progress bar. Refer to the following article:

https://info.sapien.com/index.php/guis/ ... sive-loops

You can try adding a little delay after the progress is set to 100% by using a timer control to close the form.

https://info.sapien.com/index.php/guis/ ... er-control
The topic you referenced was never completed. The user abandoned what he was trying to do because it doesn't make much sense. You code and use of multiple jobs does not match the design and implementation of a progress bar. The best you can do is to provide incremental output that is consumed in the UpdateScript code and have that update the progress bar. I can think of no other way to use your code to do what you are asking.

I also cannot understand why you want to do this. The connections locally are very fast. The topic you referenced can take time because they are connections to the cloud (Azure). Even those are fast and would not require a progress bar.

Here is a direct link to the ProgressBar documentation https://info.sapien.com/index.php/guis/ ... ar-control
User avatar
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

Re: New PsSession with a Progress Bar

Post by bhnuser »

Yeah i see this is not easy as i thought. The Problem is, that i create 3 PS-Sessions and while i start the programm it takes about 30 seconds and this is a little bit frustrating if you do not see any progress on the start.

But i found maybe an other way to display the progress. I refered to the post:
https://www.sapien.com/blog/2012/05/16/ ... ive-forms/
and your help by using start-job.

Firstly i try it with an animated button while i select specific user with Get-ADUser. Maybe someone has a solution, how i can use the arguments to pass them to the Job. Here are the variables that should be passed to the job:

Code: Select all

$domaincontroller1 = server1.company.com
$domaincontroller2 = server2.company.com
$userProperties = "*"
Code-Snippet of my tool:

Code: Select all

$buttonUserSearch_Click = {
	ClearUserTab
	if ($textboxUsername.Text.Length -gt 2)
	{
		try
		{
			$buttonUserSearch.Enabled = $false
			#Create a New Job using the Job Tracker
			Add-JobTracker -Name "JobName" `
						   -JobScript {
				$user = '*' + $textboxUsername.Text + '*'
				if ($radiobutton1.Checked -eq $true) { @(Get-ADUser -Server $domaincontroller1 -Filter {Name -like "$user"} -Properties $userProperties | sort Name -ErrorAction Stop) }
				if ($radiobutton2.Checked -eq $true) { @(Get-ADUser -Server $domaincontroller2 -Filter {Name -like "$user"} -Properties $userProperties | sort Name -ErrorAction Stop) }
			}`
			-CompletedScript {
				Param ($Job)
				$global:users = Receive-Job -Job $job
				for ($i = 0; $i -lt $users.length; $i++)
				{
					[void]$listUsername.Items.Add($users[$i].Name)
				}
				#Enable the Button
				$buttonUserSearch.ImageIndex = -1
				$buttonUserSearch.Enabled = $true
			}`
			-UpdateScript {
				Param ($Job)

				#Animate the Button
				if ($buttonUserSearch.ImageIndex -lt $buttonUserSearch.ImageList.Images.Count - 1)
				{
					$buttonUserSearch.ImageIndex += 1
				}
				else
				{
					$buttonUserSearch.ImageIndex = 0
				} 
			}
		}
		catch { }
		$statusBar.Text = "Ready"
		return $users
	}
	else { $statusBar.Text = "Can not search without some information!" }
}
On this base i woul create a progress bar for the ps-session. But first i would have this part running. Any suggest or solutions?

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

Re: New PsSession with a Progress Bar

Post by jvierra »

A progress bar and job cannot be used to connect a session. The session will not be available if created in a job.
There is no way to do what you ask. You can just output each step of the connection to a textbox.

Your code does not do what you say. It is not creating any sessions. It is getting back users. If you don't save them in a variable then send them to the pipeline and return them in the Update script.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New PsSession with a Progress Bar

Post by jvierra »

You cannot access forms control from a "JobScript" as it runs in a different process.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New PsSession with a Progress Bar

Post by jvierra »

Here is how you will have to do this to get the users and maintain the results while animating the button.

Code: Select all

$jobScript = {
    Param(
        $Server,
        $User
    )
    $userProperties = 'set required properties here'
    Get-ADUser -Server $Server -Filter "Name -like '$User'" -Properties $userProperties
}
$updateScript = {
    Param ($Job)
    # return users here
    [array]$global:users += $Job | Receive-Job
    if ($buttonUserSearch.ImageIndex -lt $buttonUserSearch.ImageList.Images.Count - 1) {
        $buttonUserSearch.ImageIndex += 1
    } else {
        $buttonUserSearch.ImageIndex = 0
    }
}

$completedScript = {
    Param ($Job)
    # get remaining results
    [array]$global:users += $Job | Receive-Job
    $buttonUserSearch.ImageIndex = -1
    $buttonUserSearch.Enabled = $true
}

$buttonUserSearch_Click = {
    $buttonUserSearch.Enabled = $false
    $jobparams = @{
        Name         = 'UserSearch'
        JobScript    = $jobScript
        UpdateScript = $updateScript
        CompletedScript = $completedScript
    }
    # the following should be set by the "Checked" event.
    if ($radiobuttonLenze.Checked -eq $true) {
        $server = 'server1.company.com'
    }else{
        $server = 'server2.company.com'
    }
    Add-JobTracker @jobparams -ArgumentList $server, $textboxUsername.Text
    $statusBar.Text = 'Ready'
}
The textbox validation should be done in the textbox validating event.
This topic is 5 years and 1 month 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