ISE vs Jobform

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 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
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

ISE vs Jobform

Post by apowershelluser »

So I'm trying to return if the logged on user is an Admin
Sometimes this script/form works in 1 sec, others it's 30 seconds, sometimes it fails
If I run this in ISE, the results always return under 3 seconds
  1. $([ADSI]"WinNT://$server/Administrators,group").psbase.Invoke('Members') | foreach { $_.GetType().InvokeMember('ADspath', 'GetProperty', $null, $_, $null).Replace('WinNT://', '') }
GetAdminADSI.psf
(63.91 KiB) Downloaded 134 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ISE vs Jobform

Post by jvierra »

Easy way.

if(net user $env:USERNAME | ?{$_ -match 'administrators'}){'Admin'}else{'not admin'}

If you are running this from a login script than any method can take up to 5 minutes. Login scripts are delayed to accelerate the logon process.
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: ISE vs Jobform

Post by apowershelluser »

Hello and thanks for your reply. My results are from me just running a form on a PC that’s sitting right next to me on wireless. It just baffles me as this is very odd behavior. When I have much longer jobs that process as expected on the same client.

I worked on this a little more and had to make the decision of putting It in the beginning of my button event and outside of a job. This was the only way I could guarantee a correct result.

I do apologize for not making my scenario apperant in that I’m running this against remote clients.

Knowing this now, do you have another solution?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ISE vs Jobform

Post by jvierra »

I fixed your script. You had a few errors in coding that, when fixed, allowed it to work.
Attachments
GetAdminADSI.psf
(56.07 KiB) Downloaded 119 times
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: ISE vs Jobform

Post by apowershelluser »

I appreciate this a lot!
I’ll have to look into this tonight and see the differences.
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: ISE vs Jobform

Post by apowershelluser »

The changes just seem to be formatting and removed the $ from before ADSI and changed foreach to ForEach-Object

Am I not seeing everything?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ISE vs Jobform

Post by jvierra »

Many subtle difference. The Invoke works the way I posted it. The job must be launched correctly so I made that more explicit.

Look more closely:

Code: Select all

$jobscript = {
	Param ($PC)
	#--------------------------------------------------
	#TODO: Set a script block
	#Important: Do not access form controls from this script block.
	
	([ADSI]("WinNT://$PC/Administrators,group")).Invoke('Members') | 
    ForEach-Object { 
        ($_.GetType()).InvokeMember('ADspath', 'GetProperty', $null, $_, $null).Replace('WinNT://', '') 
    }
}

$updateScript = {
	Param ($Job)
	#Animate the Button
	if ($null -ne $buttonStartJob.ImageList)
	{
		if ($buttonStartJob.ImageIndex -lt $buttonStartJob.ImageList.Images.Count - 1)
		{
			$buttonStartJob.ImageIndex += 1
		}
		else
		{
			$buttonStartJob.ImageIndex = 0
		}
	}
}
$completedScript =  {
	Param ($Job)
	$results = Receive-Job -Job $Job
	$admins = $results
	if ($admins -match $user)
	{
		$textbox3.Text = "IsAdmin"
	}
	else
	{
		$textbox3.Text = "IsNotAdmin"
	}
	
	#Enable the Button
	$buttonStartJob.ImageIndex = -1
	$buttonStartJob.Enabled = $true
}

$buttonStartJob_Click={
	
	$buttonStartJob.Enabled = $false
    $PC = $env:COMPUTERNAME
	Add-JobTracker -Name 'JobNameAdministrators' -JobScript $jobscript -UpdateScript $updateScript -CompletedScript $completedScript -ArgumentList $PC
}
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: ISE vs Jobform

Post by apowershelluser »

Just to note: this does work as I need it. I guess I do need to look more closely.

As always, thank you. Once I find the others I’ll let you know.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ISE vs Jobform

Post by jvierra »

You need to remember that a job cannot reference any controls or variables in the form. All things must be passed as arguments to the job.

The hang was caused by the long timeout with ADSI when trying to access a non-responsive or non-existent computer.
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: ISE vs Jobform

Post by apowershelluser »

Odd, I know that. Maybe I just missed it.

I have around 8 jobs running on my start job event. This was the only one returning mismatched results.

Now it’s tying it all together and figuring out how to detect all 8 jobs are complete and display my results all at one time
This topic is 6 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