New PsSession with a Progress Bar

Ask your Windows 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.
User avatar
bhnuser
Posts: 37
Joined: Fri Apr 06, 2018 7:35 am

New PsSession with a Progress Bar

Post by bhnuser » Thu Feb 07, 2019 12:09 am

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

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

Re: New PsSession with a Progress Bar

Post by jvierra » Thu Feb 07, 2019 12:18 am

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: 37
Joined: Fri Apr 06, 2018 7:35 am

Re: New PsSession with a Progress Bar

Post by bhnuser » Thu Feb 07, 2019 12:25 am

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

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

Re: New PsSession with a Progress Bar

Post by jvierra » Thu Feb 07, 2019 12:31 am

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.

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

Re: New PsSession with a Progress Bar

Post by jvierra » Thu Feb 07, 2019 12:36 am

bhnuser wrote:
Thu Feb 07, 2019 12:25 am
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.

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

Re: New PsSession with a Progress Bar

Post by jvierra » Thu Feb 07, 2019 9:43 am

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: 37
Joined: Fri Apr 06, 2018 7:35 am

Re: New PsSession with a Progress Bar

Post by bhnuser » Tue Feb 12, 2019 1:52 am

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

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

Re: New PsSession with a Progress Bar

Post by jvierra » Tue Feb 12, 2019 4:00 am

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.

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

Re: New PsSession with a Progress Bar

Post by jvierra » Tue Feb 12, 2019 4:04 am

You cannot access forms control from a "JobScript" as it runs in a different process.

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

Re: New PsSession with a Progress Bar

Post by jvierra » Tue Feb 12, 2019 4:25 am

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.

Locked