Asked by:
powershell scope variable

General discussion
-
hello:
I want to retrieve a variable $i in nested foreach in the name disk-$i, the problem is that i loose the value of $i on disk-$i,how can i preserve this variable for example
foreach ($i in (1..$n)){
get-item folder-$i
get-job | foreach {set-disk disk-$i}
...
}Saturday, June 3, 2017 12:55 PM
All replies
-
Unfortunately you code does not make mush sense. What are you trying to do?
foreach ($i in (1 .. $n)) { Write-Host $i get-item "folder-$i" # ?????? get-job | ForEach-Object { set-disk -Number $i } }
The Get-Job loop makes no sense.
\_(ツ)_/
Saturday, June 3, 2017 1:35 PM -
hello it was just an example here is : i loose the variable value $i on write-host "folder name folder-$i"
foreach ($i in (1..$n)) {
Copy-Item -Path $source\folder -Destination $detin\folder-$i -Recurse(Get-Content -Path $source\$folder-$i\Drives\script.ps1) | ForEach-Object {
write-host "folder name folder-$i"
$_ -Replace "folder", "folder-$i"} | out-file list.txt
thank you
Saturday, June 3, 2017 1:45 PM -
Works fine for me:
foreach ($i in (1 .. 10)) { #Write-Host $i 1..3 | ForEach-Object { Write-Host $i #set-disk -Number $i } }
\_(ツ)_/
Saturday, June 3, 2017 1:54 PM -
hello
i test your code and it work, the logic of scope variable is respected, why i loose my values on sales-$i
foreach ($i in (1..$n)) {
Copy-Item -Path $source -Destination $dest-$i -Recurse
Get-Content -Path $dest-$i\file.txt | ForEach-Object {$_ -Replace "sales", "sales-$i"} | out-file sales.txtthank you
Saturday, June 3, 2017 6:51 PM -
You are not saving them anywhere.
$I = 999 Get-Content -Path $dest-$i\file.txt | ForEach-Object { $_ -Replace "sales", "sales-$i" }
\_(ツ)_/
- Edited by jrv Saturday, June 3, 2017 7:26 PM
Saturday, June 3, 2017 7:23 PM -
thank youSaturday, June 3, 2017 9:16 PM