variables not passing in jobs

Ask your PowerShell-related questions, including questions on cmdlet development!
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 5 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
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

variables not passing in jobs

Post by apowershelluser »

  1. $paramAddJobTracker = @{
  2.         Name      = 'JobNameInstallApp'
  3.         JobScript = {
  4.             #--------------------------------------------------
  5.             #TODO: Set a script block
  6.             #Important: Do not access form controls from this script block.
  7.            
  8.             Param ($Computer) #Pass any arguments using the ArgumentList parameter
  9.                        #Grab latest version of application from the server
  10.             $Global:App= Get-ChildItem "\\server\folder" -Recurse -Include *.exe | sort-object LastWriteTime | select-object -last 1
  11.                         #Leave just the name of the application
  12.             $Global:AppName = $App.Name
  13.             #Copy application to PC
  14.                         robocopy $App.DirectoryName \\$Computer\C$\temp /if *.exe
  15.             #psexec -accepteula -s -h \\$Computer "C:\temp\$AppName.exe" /Q
  16.             #psexec -accepteula -s -h \\$Computer "C:\temp\$AppName.exe" /Q
  17.            
  18.             #--------------------------------------------------
  19.         }
  20.         ArgumentList = $Computer
  21.         CompletedScript = {
  22.             Param ($Job)
  23.             $results = Receive-Job -Job $Job
  24.                         #Find installation log ***********************************************
  25.             $Global:AppLog = "\\$Computer\C$\windows\Logs\$AppName.log"
  26.                         #Check if file exist
  27.             #if (!([System.IO.File]::Exists($AppLog))) { start-sleep -seconds 3 }
  28.         }
  29.        
  30.     }
See the stars next to #Find installation log? See where I bring $AppName.log? It cannot find the name
Even if I do
[void][System.Windows.Forms.MessageBox]::Show("$AppName", 'AppInstaller')

The dialog box is blank.

Any help? I even updated powershell studio and created a new blank form.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: variables not passing in jobs

Post by jvierra »

Actually "AppName" only exists inside the job. "Global" does not make it visible outside of the job because the job runs in another process.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: variables not passing in jobs

Post by jvierra »

I think this is closer to what you are trying to do:

Code: Select all


$paramAddJobTracker = @{
    Name      = 'JobNameInstallApp'
    JobScript = {
        Param ($Computer) #Pass any arguments using the ArgumentList parameter
        $app = Get-ChildItem "\\server\folder" -Recurse -Include *.exe | 
            sort-object LastWriteTime | 
            select-object -last 1
        robocopy $app.DirectoryName \\$Computer\C$\temp /if *.exe
        $app
    }
    ArgumentList = $Computer
    CompletedScript = {
        Param ($Job)
        $app = Receive-Job -Job $Job
        $appName = $app.Name
        $global:AppLog = "\\$Computer\C$\windows\Logs\$appName.log"
    }
}
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: variables not passing in jobs

Post by apowershelluser »

I left the global in there on accident. I was just trying something...

So what you're saying. If I have a variable in the -JobScript scriptblock, it should work in the -CompletedScript scriptblock?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: variables not passing in jobs

Post by jvierra »

JohnTitor wrote: Mon Feb 04, 2019 10:17 am I left the global in there on accident. I was just trying something...

So what you're saying. If I have a variable in the -JobScript scriptblock, it should work in the -CompletedScript scriptblock?
No. THe "JobScript" runs in isolation as a job. The "UpdateScript" and "CompletedScript" run in the current forms context (process) and cannot access variables in the job and the job cannot access variables in the form. You can pass arguments to the "JobScript" and receive variables and objects form the job in the completed or update code with "Receive-Job".

Run you code at a PS prompt to inspect the commands and how they work. When you have a clear idea of how the job subsystem works you will understand how you have to structure your code. Running at a prompt is also a very good way to test the job code to be sure it does what you want.
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: variables not passing in jobs

Post by apowershelluser »

Well I've figured I'm just a giant dummy. I moved the entire script to the JobScript part. I was just making it too hard for myself
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: variables not passing in jobs

Post by jvierra »

Yes. That is one way to solve the issue but it still doesn't solve the issue of the messagebox displaying the appname.
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: variables not passing in jobs

Post by apowershelluser »

I was just using the message box to show if the variable was displaying... A placeholder if you will.

It's all working as it should now.
This topic is 5 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