Multi-Form - credential passing

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.
Locked
User avatar
lontru
Posts: 58
Joined: Fri Aug 18, 2017 4:36 am

Multi-Form - credential passing

Post by lontru » Wed Jan 02, 2019 12:00 am

Hi working on a tool :D
I want this functionality:
Main form get user credential : get-credential
Launch a child form with these credential.

So somehow do a runas on the childForm would be great but how?

Code: Select all

$buttonCallChildForm_Click={
	#TODO: Place custom script here
	if((Show-ChildForm_psf) -eq 'OK')
	{
		
	}
}
The child form is a tool for deleting device in SCCM so the user need access right to the configmgr module and the cmsite psdrive.
What solutions is possible to achieve above functionality
Attachments
2019-01-02_07-51-43.gif
2019-01-02_07-51-43.gif (45.17 KiB) Viewed 635 times
Multi-Form - credential passing.zip
(104.04 KiB) Downloaded 23 times

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

Re: Multi-Form - credential passing

Post by jvierra » Wed Jan 02, 2019 4:22 am

You cannot do this. You can only start a completely new process with new credentials.

User avatar
lontru
Posts: 58
Joined: Fri Aug 18, 2017 4:36 am

Re: Multi-Form - credential passing

Post by lontru » Thu Jan 03, 2019 12:50 am

Have sleep on this and come up with to ways

1. Call another exe with start-process -credential
a single form exe created from psf can you pass param to this exe or do you need to build it as a project?

2. Convert the project main to support param.
Main call it self with start-process -credential but with param to start up the right sub psf inside the main.

way 1 would be easy to do but then i have a lot of files to manage. way 2 keep it all in same project....simpler to manage

is above solutions possible?
Attachments
jewoV5Hu7W.png
jewoV5Hu7W.png (59.92 KiB) Viewed 592 times

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

Re: Multi-Form - credential passing

Post by jvierra » Thu Jan 03, 2019 12:58 pm

Here is perhaps a better way to do this. See the attached project.

THis is the only way to pass objects to a new process. With Start-Process you can only pass strings.
Attachments
Test-NewCreds.zip
(86.34 KiB) Downloaded 26 times

User avatar
lontru
Posts: 58
Joined: Fri Aug 18, 2017 4:36 am

Re: Multi-Form - credential passing

Post by lontru » Fri Jan 04, 2019 3:57 am

it work like a charm...

have work a little with your demo...is there a way open more then one sub psf form? right now it lock so you need to close a open subform before you can start a new subform?

Code: Select all

$buttonCallChildForm_Click={
    $sb = ${function:Show-ChildForm_psf}
    $obj = [pscustomobject]@{Text = 'Hello World!'}
    $job = Start-Job -ScriptBlock $sb -Credential $cred -ArgumentList $obj
    $job | Wait-Job | Remove-Job
}

$buttonGetCredential_Click={
	$script:cred = Get-Credential -ErrorAction SilentlyContinue
	$buttonGetCredential.Text = $cred.UserName
}
Attachments
Test-NewCreds.zip
(263.3 KiB) Downloaded 23 times

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

Re: Multi-Form - credential passing

Post by jvierra » Fri Jan 04, 2019 6:14 am

lontru wrote:
Fri Jan 04, 2019 3:57 am

have work a little with your demo...is there a way open more then one sub psf form
Comment the line:

$job | Wait-Job | Remove-Job

You can now open multiple forms. Run a timer to remove jobs that complete when the child form is closed.

User avatar
lontru
Posts: 58
Joined: Fri Aug 18, 2017 4:36 am

Re: Multi-Form - credential passing

Post by lontru » Tue Jan 08, 2019 3:20 am

_How do i perform a cleanup routine when the childform is closing?

So i need to pass a unique id to the childform as a argument and pass that to remove-job when the child form is closing?

Or does all the jobs disappear when the mainform closeds?

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

Re: Multi-Form - credential passing

Post by jvierra » Tue Jan 08, 2019 6:14 am

You can give a job a name.
When the job is complete you can detect that it is complete and remove it.

Locked