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.
Running a Control Set Job with different credentials
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.
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.
Re: Running a Control Set Job with different credentials
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:
.... but I can't see how to do the same thing when using the Control Set Job Button.
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
Re: Running a Control Set Job with different credentials
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
Re: Running a Control Set Job with different credentials
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.
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.
- Attachments
-
- elevated job.jpg (45.22 KiB) Viewed 3944 times
Re: Running a Control Set Job with different credentials
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.
Re: Running a Control Set Job with different credentials
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).
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).
Re: Running a Control Set Job with different credentials
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.
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.
Re: Running a Control Set Job with different credentials
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.
Note that you cannot place forms code in the job script.