Skip to main content
powershell/PSUseUsingScopeModifierInNewRunspaces is a PSScriptAnalyzer diagnostic emitted by tally for PowerShell snippets embedded in Dockerfiles.

Description

If a scriptblock is intended to be run in a new runspace, variables inside it should use the $using: scope modifier, or be initialized within the scriptblock. This applies to:
  • Invoke-Command- Only with the ComputerName or Session parameter.
  • Workflow { InlineScript {} }
  • Foreach-Object - Only with the Parallel parameter
  • Start-Job
  • Start-ThreadJob
  • The Script resource in DSC configurations, specifically for the GetScript, TestScript and SetScript properties.

How to Fix

Within the ScriptBlock, instead of just using a variable from the parent scope, you have to add the using: scope modifier to it.

Examples

Problematic code

Correct code

More correct examples

Source

This rule documentation is adapted from Microsoft’s PSScriptAnalyzer documentation for UseUsingScopeModifierInNewRunspaces, licensed under CC BY 4.0.