PowerShell Job Shows "Completed" Immediately After Starting When Using A Variable As The -Counter Parameter

Created on 28 Apr 2020  路  2Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

Run a Get-Counter job providing a variable that contains the string of the counter you wish to capture for the "-Counter" parameter as per the below example.

$t = "\LogicalDisk(C:)\% Free Space"
$diskJob = Start-Job -ScriptBlock {Get-Counter -Counter "$t" -SampleInterval 10 -MaxSamples 25 | foreach {$_.CounterSamples} }

Expected behavior

Same behavior as if a string was typed out such as the below.

$diskJob = Start-Job -ScriptBlock {Get-Counter -Counter "\LogicalDisk(C:)\% Free Space" -SampleInterval 10 -MaxSamples 25 | foreach {$_.CounterSamples} }

Actual behavior

Job shows completed immediately.
image

Environment data

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
PSVersion                      5.1.14393.2430                                                                                                                                                                                                                    
PSEdition                      Desktop                                                                                                                                                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                                           
BuildVersion                   10.0.14393.2430                                                                                                                                                                                                                   
CLRVersion                     4.0.30319.42000                                                                                                                                                                                                                   
WSManStackVersion              3.0                                                                                                                                                                                                                               
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                                               
SerializationVersion           1.1.0.1                                                                                                                                                                                                                           


Issue-Question Resolution-Answered

Most helpful comment

Jobs run in a different process and don't inherit variables automatically. If you use the using modifier, the value will be retrieved from the parent process (though it will be serialized).

$t = "\LogicalDisk(C:)\% Free Space"
$diskJob = Start-Job -ScriptBlock {
    Get-Counter -Counter $using:t -SampleInterval 10 -MaxSamples 25 |
        ForEach-Object { $_.CounterSamples }
}

All 2 comments

Jobs run in a different process and don't inherit variables automatically. If you use the using modifier, the value will be retrieved from the parent process (though it will be serialized).

$t = "\LogicalDisk(C:)\% Free Space"
$diskJob = Start-Job -ScriptBlock {
    Get-Counter -Counter $using:t -SampleInterval 10 -MaxSamples 25 |
        ForEach-Object { $_.CounterSamples }
}

Jobs run in a different process and don't inherit variables automatically. If you use the using modifier, the value will be retrieved from the parent process (though it will be serialized).

$t = "\LogicalDisk(C:)\% Free Space"
$diskJob = Start-Job -ScriptBlock {
    Get-Counter -Counter $using:t -SampleInterval 10 -MaxSamples 25 |
        ForEach-Object { $_.CounterSamples }
}

Oh wow, that is indeed the solution. Very interesting that Jobs run in a different process, I didn't see that mentioned in the docs anywhere when I was researching. Thanks!

Was this page helpful?
0 / 5 - 0 ratings