variables not passing in jobs

Ask your Windows 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.
Locked
User avatar
JohnTitor
Posts: 119
Joined: Fri Nov 17, 2017 8:49 pm

variables not passing in jobs

Post by JohnTitor » Mon Feb 04, 2019 10:16 am

Code: Select all

$paramAddJobTracker = @{
Name = 'JobNameInstallApp'
JobScript = {
#--------------------------------------------------
#TODO: Set a script block
#Important: Do not access form controls from this script block.

Param ($Computer) #Pass any arguments using the ArgumentList parameter
#Grab latest version of application from the server
$Global:App= Get-ChildItem "\\server\folder" -Recurse -Include *.exe | sort-object LastWriteTime | select-object -last 1
#Leave just the name of the application
$Global:AppName = $App.Name
#Copy application to PC
robocopy $App.DirectoryName \\$Computer\C$\temp /if *.exe
#psexec -accepteula -s -h \\$Computer "C:\temp\$AppName.exe" /Q
#psexec -accepteula -s -h \\$Computer "C:\temp\$AppName.exe" /Q

#--------------------------------------------------
}
ArgumentList = $Computer
CompletedScript = {
Param ($Job)
$results = Receive-Job -Job $Job
#Find installation log ***********************************************
$Global:AppLog = "\\$Computer\C$\windows\Logs\$AppName.log"
#Check if file exist
#if (!([System.IO.File]::Exists($AppLog))) { start-sleep -seconds 3 }
}

}
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.

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

Re: variables not passing in jobs

Post by jvierra » Mon Feb 04, 2019 10:38 am

Actually "AppName" only exists inside the job. "Global" does not make it visible outside of the job because the job runs in another process.

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

Re: variables not passing in jobs

Post by jvierra » Mon Feb 04, 2019 11:07 am

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
JohnTitor
Posts: 119
Joined: Fri Nov 17, 2017 8:49 pm

Re: variables not passing in jobs

Post by JohnTitor » Mon Feb 04, 2019 11: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?

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

Re: variables not passing in jobs

Post by jvierra » Mon Feb 04, 2019 11:25 am

JohnTitor wrote:
Mon Feb 04, 2019 11: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
JohnTitor
Posts: 119
Joined: Fri Nov 17, 2017 8:49 pm

Re: variables not passing in jobs

Post by JohnTitor » Mon Feb 04, 2019 12:30 pm

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

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

Re: variables not passing in jobs

Post by jvierra » Mon Feb 04, 2019 12:38 pm

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
JohnTitor
Posts: 119
Joined: Fri Nov 17, 2017 8:49 pm

Re: variables not passing in jobs

Post by JohnTitor » Mon Feb 04, 2019 12:39 pm

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.

Locked