Code runs at least twice

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 7 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
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Re: Code runs at least twice

Post by gutihz »

Here is the block under shown
  1. $Child_Shown = {
  2. $regBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $CompName).OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment")
  3.     $Domain = $regBase.GetValue("Domain")
  4.         $Region = $Domain.Substring(0, 2)
  5.     $Suffix = $regBase.GetValue("Suffix")
  6.     $DivisionSite = $CompName.substring(0, 6)
  7.     $Site = $RegBase.Getvalue("Site").Substring(0, 2)
  8.     $Owner = $regBase.GetValue("Owner")
  9.  
  10.     Bitlocker_Status $CompName
  11.     Check-OfferHelp $CompName
  12.     Check-admins $CompName
  13.     Log_History $CompName
  14.     $statusbar.Text = "Done...!"
  15. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Code runs at least twice

Post by jvierra »

I need a PSF file that display your issue. Create the least amount of code necessary that demonstrates the problem.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Code runs at least twice

Post by jvierra »

Try this:

Change the event from "Shown" to "Activated"

Use this:
  1. $Child_Activated = {
  2.     if(-not $script:activated){
  3.         $script:activated = $true
  4.         $regBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $CompName).OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment")
  5.         $Domain = $regBase.GetValue("Domain")
  6.         $Region = $Domain.Substring(0, 2)
  7.         $Suffix = $regBase.GetValue("Suffix")
  8.         $DivisionSite = $CompName.substring(0, 6)
  9.         $Site = $RegBase.Getvalue("Site").Substring(0, 2)
  10.         $Owner = $regBase.GetValue("Owner")
  11.        
  12.         Bitlocker_Status $CompName
  13.         Check-OfferHelp $CompName
  14.         Check-admins $CompName
  15.         Log_History $CompName
  16.         $statusbar.Text = "Done...!"
  17.     }
  18. }
User avatar
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Re: Code runs at least twice

Post by gutihz »

Hi Jvierra,

Here is a very basic psf form.
This still takes a long time (till the functions finished) to load.

Thanks for your help.
Attachments
child.psf
(67.78 KiB) Downloaded 155 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Code runs at least twice

Post by jvierra »

Why are you copying PsExcc to the remote? It serves no purpose.
Why are you using xcopy? Just use Copy-Item.
What is GetInfo?

Why are you using PsExec at all?

If this is slow then you may have network issues.
User avatar
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Re: Code runs at least twice

Post by gutihz »

Why are you copying PsExcc to the remote? It serves no purpose.
--> For the user to be able to use it later when they need it.
Why are you using xcopy? Just use Copy-Item.
--> I had issues with copy-item before. I found that xcopy works better.
What is GetInfo?
--> Getinfo is a PowerShell script that collects the information and I grab that information once it completes.

Why are you using PsExec at all?
--> I first started trying to get all the information I wanted using wmi but it kept giving errors. So I figured I do it locally and grab all the info

If this is slow then you may have network issues.
--> I know for a fact that we don't have any network issues. Also, my test machine is a VM on my local machine.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Code runs at least twice

Post by jvierra »

There is no reason for Copy-Item to not work ix xcopy works.
You can use PsExec but any slowness is not due to PowerShell.

What info are you trying to get? Perhaps you exe is not compatible.

There is no technical reason for this to be slow. If you use a job the screen will still be empty until the job completes so there is really nothing you can do about that.
User avatar
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Re: Code runs at least twice

Post by gutihz »

I'm trying to check local groups on the remote computer and add if missing user names I want them to be in.

I have 4 functions like the one below. Doesn't matter what I did, the form either stops responding while the functions are running or doesn't pop-up until it finishes. That's why I keep trying other things.

I tried running the functions in a start-job (Not sure if I did that right) but even that didn't work.
  1. $J = Start-Job -ScriptBlock ${function:Check_Admins} -ArgumentList $ComputerName $UserName
  2. Wait-Job $J
  1. function Check_admins ([string]$ComputerName, $UserName)
  2. {
  3.     $AdminMembers = @()
  4.     $AdminGroup = [ADSI]"WinNT://$ComputerName/Administrators,group"
  5.     $Admin = @($AdminGroup.psbase.Invoke("Members"))
  6.     $Admin | ForEach-Object {
  7.         $AdminMembers += $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
  8.     }
  9.     foreach ($Admins in $AdminMembers)
  10.     {
  11.         IF ($txtboxadmins.Text.Contains($Admins) -eq $true)
  12.         {
  13.             Out-Null
  14.         }
  15.         Else
  16.         {
  17.         $txtboxadmins.AppendText($Admins)
  18.         $txtboxadmins.AppendText("`n")
  19.         }
  20.     }
  21. IF ($txtboxadmins.Text.Contains($UserName) -eq $true)
  22.     {
  23.         $txtboxadmins.Find($UserName)
  24.         $txtboxadmins.SelectionColor = 'Brown'
  25.     }
  26.     ELSE
  27.     {
  28.         Try
  29.         {
  30.             $AdminGroup = [ADSI]"WinNT://$ComputerName/Administrators,group"
  31.                         $AdminGroup.psbase.Invoke("Add", ([ADSI]"WinNT://$UserName").path)
  32.             $txtboxadmins.AppendText($UserName)
  33.             $txtboxadmins.Find($UserName)
  34.             $txtboxadmins.SelectionColor = 'Brown'
  35.            
  36.         }
  37.         Catch
  38.         {
  39.             $txtboxadmins.Text = "ERROR adding $UserName"
  40.         }
  41.     }
  42. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Code runs at least twice

Post by jvierra »

A form will always stop responding when it is in an event. When the event complete the form will be available for new events. This is how Windows works.

Read the following to better understand how forms work: https://www.sapien.com/blog/2012/05/16/ ... ive-forms/

Also review all of the following posts: https://www.sapien.com/blog/topics/user ... istrators/

These will help you understand how to use forms and what you can expect with forms behavior.
User avatar
jazz albert
Posts: 16
Last visit: Sat Nov 05, 2016 3:09 pm

Re: Code runs at least twice

Post by jazz albert »

gutihz wrote:I tried the shown event too. The child form pops-up after everything has finished.
I don't swing that way. It might be due to something else went wrong!
This topic is 7 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