Running a Control Set Job with different credentials

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 6 years and 4 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
mark106
Posts: 27
Last visit: Mon Apr 15, 2024 2:26 am
Has voted: 1 time

Running a Control Set Job with different credentials

Post by mark106 »

I have a similar query to the post here: viewtopic.php?t=7004

Inside my PowerShell GUI I invoke a PowerShell job because the particular task takes a few seconds to complete and it also requires alternative credentials.

As mentioned in the referenced post, this causes a console window to appear for the duration - which I'd like to remove/hide.

I'd like to use the Job Control Set (as recommended in the other post) but I can't see how to run the job with different credentials.

Any help gratefully received.

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

Re: Running a Control Set Job with different credentials

Post by jvierra »

Edit the function and add a "Credential" parameter.

The control set is designed to allow you to modify the example code.
User avatar
mark106
Posts: 27
Last visit: Mon Apr 15, 2024 2:26 am
Has voted: 1 time

Re: Running a Control Set Job with different credentials

Post by mark106 »

Hi,

Thanks for your quick reply.

I'm afraid I'm struggling to work out how to do what you describe. I basically would like to run the whole job as a different user. So, using a normal job I would do something like:

Code: Select all

$thejob = start-job -ScriptBlock {
		
     # Do a thing that takes a while ................

	} -Name "exampleJob" -Credential $credentials 
.... but I can't see how to do the same thing when using the Control Set Job Button.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Running a Control Set Job with different credentials

Post by jvierra »

Edit the Add-JobTracker function to add the parameter.

Code: Select all

function Add-JobTracker {    
    Param (
        [ValidateNotNull()]
        [Parameter(Mandatory = $true)]
        [string]$Name,
        [ValidateNotNull()]
        [Parameter(Mandatory = $true)]
        [ScriptBlock]$JobScript,
        $ArgumentList = $null,
        [ScriptBlock]$CompletedScript,
        [ScriptBlock]$UpdateScript,
        [pscredential]$credential
    )
    
    #Start the Job
    $jobArguments = @{
        Name = $Name 
        ScriptBlock = $JobScript
        ArgumentList =  $ArgumentList
    }
    if($credential){ $jobArguments.Add('Credential',$credential)
    $job = Start-Job @jobArguments
 
User avatar
mark106
Posts: 27
Last visit: Mon Apr 15, 2024 2:26 am
Has voted: 1 time

Re: Running a Control Set Job with different credentials

Post by mark106 »

Thank you for your reply - it's much appreciated. I wouldn't have worked that out myself.

It's working now (i.e. I seem to be able to spawn a job, using the Control Set Job, with alternate credentials) so that's great. Unfortunately, I still get a PowerShell window appear while the job is in progress (even if I don't put anything in the job (-Jobscript parameter)). Is there any way to hide/silence this? The PowerShell window only seems to appear on jobs run with different credentials.

Image
Attachments
elevated job.jpg
elevated job.jpg (45.22 KiB) Viewed 3944 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 a Control Set Job with different credentials

Post by jvierra »

I have never seen that. There is no reason for that to happen. I cannot reproduce the issue either. perhaps you are building with the wrong host.
User avatar
mark106
Posts: 27
Last visit: Mon Apr 15, 2024 2:26 am
Has voted: 1 time

Re: Running a Control Set Job with different credentials

Post by mark106 »

Ahh. It seems that if the job is run with alternate credentials then it shows a window. If there's no credential parameter, no window.

https://stackoverflow.com/questions/270 ... credential

Not yet found a way to suppress it but I've used $form1.Topmost = $true when invoking the job (and then set it back to $false in -CompletedScript) - this stops the window overlaying my exe (which is fine for my purposes). :)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Running a Control Set Job with different credentials

Post by jvierra »

No. A job does not create a window even with alternate credentials.
User avatar
mark106
Posts: 27
Last visit: Mon Apr 15, 2024 2:26 am
Has voted: 1 time

Re: Running a Control Set Job with different credentials

Post by mark106 »

Hi,

I see this behaviour on all of the machines that I have tried in our corporate environment (all Windows 7 - PowerShell v3 & v5) - and I see the same thing when using the ISE too. So, I wonder if it's something peculiar to the OS build / GP settings etc...... The forum posters in the above link seemed to experience the same thing. Anyway, I'm happy with the workaround but if I find out why this occurs I'll post an update.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Running a Control Set Job with different credentials

Post by jvierra »

What behavior? Open a PowerShell prompt and create a job with credentials. There will be no extra windows.

Note that you cannot place forms code in the job script.
This topic is 6 years and 4 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