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

Re: New PsSession with a Progress Bar

Post by bhnuser »

jvierra wrote: Tue Feb 12, 2019 3: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.

This works very good for me. Thank you very much!
The only thing i changed is the variable $global:users from

Code: Select all

$global:users += $Job | Receive-Job
to

Code: Select all

$global:users = Receive-Job -Job $job
because my listbox adds all time i clicked on search all unsers.
And i removed the row [array]$global:users += $Job | Receive-Job in the UpdateScript.
So i will try it now with the progress bar while starting the script. I will receive this post, if i get it works.
User avatar
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

Re: New PsSession with a Progress Bar

Post by bhnuser »

So i am back at this topic and hope i can solve my issue with you.
I would like to start a new session with New-PSSession but the loadtime is more than 20 seconds. So i would like to a an animated button in my script to show the user that the process is active. But how i can start a job with the following script:

Code: Select all

$ExchangeSession = {
	$PWord = ConvertTo-SecureString -String "P@ssword" -AsPlainText -Force
	$userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "username", $PWord
	if (!(Get-PSSession | where { $_.ComputerName -eq "servername" }))
	{
		$ExSession = New-PSSession -ConfigurationName microsoft.exchange -Credential $userCredential -AllowRedirection -Authentication Kerberos -ConnectionUri "http://servername/powershell" -WarningAction SilentlyContinue
		if ($?)
		{
			Import-PSSession $ExSession -ErrorAction SilentlyContinue
		}
	}
}

$job = start-job -Name "Exchange" -ScriptBlock $ExchangeSession
If i execute this i can see the properties with $test = Receive-Job $job but locally i dont have any session. Is this possible or how can i show the user that the process is active?
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 »

What you are trying todo cannot be done. A job runs in a separate process and processes are isolated from each other.
User avatar
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

Re: New PsSession with a Progress Bar

Post by bhnuser »

jvierra wrote: Thu Feb 21, 2019 7:10 am What you are trying todo cannot be done. A job runs in a separate process and processes are isolated from each other.
Too bad. Is not it possible to indicate progress or otherwise in any other way while the code is running (as above)?
Currently only the button is disabled and no more.
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 create a session in a job. The session is unusable outside of the job nd no. there is no way to add a progress bar. When running "import-session" in a form (not a job) a progress bar will be automatically shown if it is required.
User avatar
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

Re: New PsSession with a Progress Bar

Post by bhnuser »

bhnuser wrote: Tue Feb 12, 2019 11:51 pm
jvierra wrote: Tue Feb 12, 2019 3: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.

This works very good for me. Thank you very much!
The only thing i changed is the variable $global:users from

Code: Select all

$global:users += $Job | Receive-Job
to

Code: Select all

$global:users = Receive-Job -Job $job
because my listbox adds all time i clicked on search all unsers.
And i removed the row [array]$global:users += $Job | Receive-Job in the UpdateScript.
So i will try it now with the progress bar while starting the script. I will receive this post, if i get it works.

I've get a problem with this part in my tool. The Problem is, that i got the ActiveDirectory module localy on my computer. If i deploy an executable file and run it on an other computer 'without the ActiveDirectory module' tells me powershell that the function Get-ADUser does not exist. I understand the problem that the module at program start only in the session remains and can not be passed to a background job. But is there a way to do a background job with its own / custom functions?
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 have to have the module (RSAT) installed on any system that needs to use it. It cannot be passed to a remote system.
User avatar
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

Re: New PsSession with a Progress Bar

Post by bhnuser »

jvierra wrote: Mon Feb 25, 2019 2:16 am You have to have the module (RSAT) installed on any system that needs to use it. It cannot be passed to a remote system.
Is there no way to pass the Get-ADUser cmdlet on the job? Finally, the cmdlets are stored in a psm1 in the temp folder.

If it does not work, is there any way to permanently save the imported ActiveDirectory module as a module on the computer?
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 »

get-module <module name>| save-module <path>

Copying a module to a machine that does not have RSAT installed will not work. When importing the module commands are modified to cause them to be executed through the remoting system. The module will not work locally. It will only work when remoting.
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