Invoke-Item with Jobs

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
StillLearning
Posts: 39
Joined: Mon Mar 05, 2018 7:38 am

Invoke-Item with Jobs

Post by StillLearning » Mon Mar 12, 2018 6:16 pm

Hello,

We have about 12 servers where a users backup could be. Looking for the quickest way to do that. So far I have this.
  1. Add-JobTracker -Name 'JobName' `
  2.  
  3.                    -JobScript {
  4.  
  5.         #--------------------------------------------------
  6.  
  7.         #TODO: Set a script block
  8.  
  9.         #Important: Do not access form controls from this script block.
  10.  
  11.        
  12.  
  13.         Param ($Argument1) #Pass any arguments using the ArgumentList parameter
  14.  
  15.         workflow FindFolder
  16.  
  17.         {
  18.  
  19.             param (
  20.  
  21.                
  22.  
  23.                 [string[]]$Computers,
  24.  
  25.                 [string[]]$suffixes,
  26.  
  27.                 [string[]]$user
  28.  
  29.             )
  30.  
  31.             $user = 'John1'
  32.  
  33.             $folders = get-content \\server\scripts\servers.txt
  34.  
  35.             foreach -parallel ($folder in $folders)
  36.  
  37.             {
  38.  
  39.                 if (test-path $folder\$user -ErrorAction SilentlyContinue)
  40.  
  41.                 { Invoke-Item $folder\$user }
  42.  
  43.                         else {}#I don't know how to handle this just yet.  I want a popup
  44.  
  45.  
  46.  
  47.             }
  48.  
  49.         }
  50.  
  51.         FindFolder
Within the ISE no matter the folder, it's within 5 seconds. In the job form it's almost 30 seconds. I'm using this on a button click to be clear on my intent.

Click a button, seaches all of the folders in the text file to find the folder, if it doesn't exist on any server, throw an error.

User avatar
jvierra
Posts: 12663
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Invoke-Item with Jobs

Post by jvierra » Mon Mar 12, 2018 6:27 pm

You cannot do a popup in a job.

Return status objects and display them in the main form.

User avatar
StillLearning
Posts: 39
Joined: Mon Mar 05, 2018 7:38 am

Re: Invoke-Item with Jobs

Post by StillLearning » Mon Mar 12, 2018 7:16 pm

I appreciate the info.
This doesn’t have to be in a job, just trying not to freeze the form while looping through servers. Any tips?

User avatar
jvierra
Posts: 12663
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Invoke-Item with Jobs

Post by jvierra » Mon Mar 12, 2018 7:29 pm

Use the JobTracker. The form will not freeze.

User avatar
StillLearning
Posts: 39
Joined: Mon Mar 05, 2018 7:38 am

Re: Invoke-Item with Jobs

Post by StillLearning » Tue Mar 13, 2018 7:15 am

Hi hmm. You're right.

now to figure out why it's faster in the ISE than in a Job.

Maybe a ::DoEvent would be a better solution for this.

User avatar
StillLearning
Posts: 39
Joined: Mon Mar 05, 2018 7:38 am

Re: Invoke-Item with Jobs

Post by StillLearning » Tue Mar 13, 2018 3:37 pm

‌FindFolder.psf
(33.68 KiB) Downloaded 24 times
I'm still facing issues with the speed in regards to the ISE and to a JobForm. Here is my form so maybe I'm doing this wrong.

I noticed I have 34 servers to ping, not 12 :/ (and yes, all of them are live, I double checked today so)

still with all of those servers, we're coming up on 30 seconds because the job has to run through every server, it doesn't just stop when it finds. I've noticed this is also true of the ISE, it just happens to display the windows explorer before the workflow is complete and the workflow processes much quicker in the ISE

I've also looked into sequence{} but the results are typically the same

Thanks!

User avatar
jvierra
Posts: 12663
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Invoke-Item with Jobs

Post by jvierra » Tue Mar 13, 2018 5:03 pm

The following line does nothing in a job or a workflow in a job:

Invoke-Item $folder\$user -ErrorAction SilentlyContinue

What is it supposed to do?

User avatar
StillLearning
Posts: 39
Joined: Mon Mar 05, 2018 7:38 am

Re: Invoke-Item with Jobs

Post by StillLearning » Tue Mar 13, 2018 6:53 pm

Well it launches windows explorer with to the folder I’m searching for. I’m not sure why you’re saying it does nothing :)

It works perfectly fine for me in the ISE and PSS on two PCs

Thanks again for your time!

User avatar
jvierra
Posts: 12663
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Invoke-Item with Jobs

Post by jvierra » Tue Mar 13, 2018 6:59 pm

Why would you do that? What purpose does launching Explorer over and over in parallel in a job serve?

User avatar
StillLearning
Posts: 39
Joined: Mon Mar 05, 2018 7:38 am

Re: Invoke-Item with Jobs

Post by StillLearning » Tue Mar 13, 2018 7:50 pm

The thinking behind this code.
  1. foreach -parallel ($folder in $folders)
  2.  
  3.             {
  4.  
  5.                 if (Test-Path $folder\$user)#test-path all of the servers in a workflow, and if it exist, open it.
  6.  
  7.                 {
  8.  
  9.                     Invoke-Item $folder\$user -ErrorAction SilentlyContinue #open
  10.  
  11.                 }
  12.  
  13.             }
This is the error code I started to get if I did test-path -isvalid, which is probably the same error I suppressed.

Microsoft.PowerShell.Management\Invoke-Item : Cannot find path '\\server\datacapture' because it does not exist.
At FindUsersFolder:16 char:16
+
+ CategoryInfo : ObjectNotFound: (\\miami.company.net\datacapture\johnsmith:String) [Invoke-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.InvokeItemCommand
+ PSComputerName : [localhost]

So now how/if possible can I have the job stop processing once the folder is found and explorer opened. That has to be the difference in the ISE vs PSS, or I'm just dumb.

Locked