Page 2 of 2

Re: Code runs at least twice

Posted: Wed Oct 12, 2016 10:05 am
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. }

Re: Code runs at least twice

Posted: Wed Oct 12, 2016 10:26 am
by jvierra
I need a PSF file that display your issue. Create the least amount of code necessary that demonstrates the problem.

Re: Code runs at least twice

Posted: Wed Oct 12, 2016 10:41 am
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. }

Re: Code runs at least twice

Posted: Thu Oct 13, 2016 7:22 am
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.

Re: Code runs at least twice

Posted: Thu Oct 13, 2016 8:09 am
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.

Re: Code runs at least twice

Posted: Thu Oct 13, 2016 9:06 am
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.

Re: Code runs at least twice

Posted: Thu Oct 13, 2016 9:28 am
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.

Re: Code runs at least twice

Posted: Thu Oct 13, 2016 12:29 pm
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. }

Re: Code runs at least twice

Posted: Thu Oct 13, 2016 1:01 pm
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.

Re: Code runs at least twice

Posted: Sat Nov 05, 2016 2:39 pm
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!