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
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2
Post
by apowershelluser » Fri Feb 16, 2018 12:52 pm
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
$( [ ADSI] "WinNT://$server/Administrators,group" ) .psbase.Invoke( 'Members' ) | foreach { $_ .GetType( ) .InvokeMember( 'ADspath' , 'GetProperty' , $null , $_ , $null ) .Replace( 'WinNT://' , '' ) }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Fri Feb 16, 2018 8:30 pm
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.
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2
Post
by apowershelluser » Sat Feb 17, 2018 4:23 am
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
Post
by jvierra » Sat Feb 17, 2018 6:04 am
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
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2
Post
by apowershelluser » Sat Feb 17, 2018 6:44 am
I appreciate this a lot!
I’ll have to look into this tonight and see the differences.
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2
Post
by apowershelluser » Sun Feb 18, 2018 7:35 am
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
Post
by jvierra » Sun Feb 18, 2018 8:33 am
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
}
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2
Post
by apowershelluser » Sun Feb 18, 2018 10:31 am
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
Post
by jvierra » Sun Feb 18, 2018 10:36 am
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.
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2
Post
by apowershelluser » Sun Feb 18, 2018 11:02 am
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