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.
User avatar
gutihz
Posts: 38
Joined: Fri Mar 20, 2015 1:42 pm

Re: Code runs at least twice

Post by gutihz »

Here is the block under shown
  1. $Child_Shown = {
  2.  
  3. $regBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $CompName).OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment")
  4.  
  5.     $Domain = $regBase.GetValue("Domain")
  6.  
  7.         $Region = $Domain.Substring(0, 2)
  8.  
  9.     $Suffix = $regBase.GetValue("Suffix")
  10.  
  11.     $DivisionSite = $CompName.substring(0, 6)
  12.  
  13.     $Site = $RegBase.Getvalue("Site").Substring(0, 2)
  14.  
  15.     $Owner = $regBase.GetValue("Owner")
  16.  
  17.  
  18.  
  19.     Bitlocker_Status $CompName
  20.  
  21.     Check-OfferHelp $CompName
  22.  
  23.     Check-admins $CompName
  24.  
  25.     Log_History $CompName
  26.  
  27.     $statusbar.Text = "Done...!"
  28.  
  29. }

jvierra
Posts: 14545
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 14545
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Code runs at least twice

Post by jvierra »

Try this:

Change the event from "Shown" to "Activated"

Use this:
  1. $Child_Activated = {
  2.  
  3.     if(-not $script:activated){
  4.  
  5.         $script:activated = $true
  6.  
  7.         $regBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $CompName).OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment")
  8.  
  9.         $Domain = $regBase.GetValue("Domain")
  10.  
  11.         $Region = $Domain.Substring(0, 2)
  12.  
  13.         $Suffix = $regBase.GetValue("Suffix")
  14.  
  15.         $DivisionSite = $CompName.substring(0, 6)
  16.  
  17.         $Site = $RegBase.Getvalue("Site").Substring(0, 2)
  18.  
  19.         $Owner = $regBase.GetValue("Owner")
  20.  
  21.        
  22.  
  23.         Bitlocker_Status $CompName
  24.  
  25.         Check-OfferHelp $CompName
  26.  
  27.         Check-admins $CompName
  28.  
  29.         Log_History $CompName
  30.  
  31.         $statusbar.Text = "Done...!"
  32.  
  33.     }
  34.  
  35. }

User avatar
gutihz
Posts: 38
Joined: Fri Mar 20, 2015 1:42 pm

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 76 times

jvierra
Posts: 14545
Joined: Tue May 22, 2007 9:57 am
Contact:

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
Joined: Fri Mar 20, 2015 1:42 pm

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: 14545
Joined: Tue May 22, 2007 9:57 am
Contact:

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
Joined: Fri Mar 20, 2015 1:42 pm

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.  
  3. Wait-Job $J
  1. function Check_admins ([string]$ComputerName, $UserName)
  2.  
  3. {
  4.  
  5.     $AdminMembers = @()
  6.  
  7.     $AdminGroup = [ADSI]"WinNT://$ComputerName/Administrators,group"
  8.  
  9.     $Admin = @($AdminGroup.psbase.Invoke("Members"))
  10.  
  11.     $Admin | ForEach-Object {
  12.  
  13.         $AdminMembers += $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
  14.  
  15.     }
  16.  
  17.     foreach ($Admins in $AdminMembers)
  18.  
  19.     {
  20.  
  21.         IF ($txtboxadmins.Text.Contains($Admins) -eq $true)
  22.  
  23.         {
  24.  
  25.             Out-Null
  26.  
  27.         }
  28.  
  29.         Else
  30.  
  31.         {
  32.  
  33.         $txtboxadmins.AppendText($Admins)
  34.  
  35.         $txtboxadmins.AppendText("`n")
  36.  
  37.         }
  38.  
  39.     }
  40.  
  41. IF ($txtboxadmins.Text.Contains($UserName) -eq $true)
  42.  
  43.     {
  44.  
  45.         $txtboxadmins.Find($UserName)
  46.  
  47.         $txtboxadmins.SelectionColor = 'Brown'
  48.  
  49.     }
  50.  
  51.     ELSE
  52.  
  53.     {
  54.  
  55.         Try
  56.  
  57.         {
  58.  
  59.             $AdminGroup = [ADSI]"WinNT://$ComputerName/Administrators,group"
  60.  
  61.                         $AdminGroup.psbase.Invoke("Add", ([ADSI]"WinNT://$UserName").path)
  62.  
  63.             $txtboxadmins.AppendText($UserName)
  64.  
  65.             $txtboxadmins.Find($UserName)
  66.  
  67.             $txtboxadmins.SelectionColor = 'Brown'
  68.  
  69.            
  70.  
  71.         }
  72.  
  73.         Catch
  74.  
  75.         {
  76.  
  77.             $txtboxadmins.Text = "ERROR adding $UserName"
  78.  
  79.         }
  80.  
  81.     }
  82.  
  83. }

jvierra
Posts: 14545
Joined: Tue May 22, 2007 9:57 am
Contact:

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
Joined: Fri Nov 04, 2016 2:32 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!

Locked