{"id":16549,"date":"2024-01-23T20:58:47","date_gmt":"2024-01-23T20:58:47","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=16549"},"modified":"2024-02-16T10:28:04","modified_gmt":"2024-02-16T10:28:04","slug":"variables-empty-in-foreach-object-parallel","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=16549","title":{"rendered":"Access caller variables from threads (PS)"},"content":{"rendered":"<p>In PowerShell, threads getting executed in separate run spaces. To access variables from the caller scope, you have to use the &#8216;using&#8217; scope modifier.<\/p>\n<hr>\n<pre>\r\nfunction DownloadFiles([string]$WebstoreUrl, [string]$FileList)\r\n{\r\n    Get-Content $FileList | ForEach-Object -Parallel {\r\n        Invoke-WebRequest \"$using:WebstoreUrl\/$_\" -OutFile $_\r\n    } -ThrottleLimit 10\r\n}\r\n<\/pre>\n<hr>\n<p>For safety reasons, you can&#8217;t mutate these variables directly. There is a workaround by assigning it to another variable, but better synchronize the access.<\/p>\n<pre>\r\nfunction DownloadFiles([string]$WebstoreUrl, [string]$FileList)\r\n{\r\n    $NoError = $true\r\n    $SyncObject = [System.Object]::new()\r\n\r\n    Get-Content $FileList | ForEach-Object -Parallel {\r\n        try {\r\n            Invoke-WebRequest \"$using:WebstoreUrl\/$_\" -OutFile $_\r\n        } catch {\r\n            [System.Threading.Monitor]::Enter($SyncObject)\r\n            try {\r\n                Write-Output \"Download error for $using:WebstoreUrl\/$_\"\r\n                $NoError = $using:NoError\r\n                $NoError = $false\r\n            } finally {\r\n                [System.Threading.Monitor]::Exit($SyncObject)\r\n            }\r\n        }\r\n    } -ThrottleLimit 10\r\n\r\n    return $NoError\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In PowerShell, threads getting executed in separate run spaces. To access variables from the caller scope, you have to use<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[78],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/16549"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16549"}],"version-history":[{"count":14,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/16549\/revisions"}],"predecessor-version":[{"id":16576,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/16549\/revisions\/16576"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16549"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}