Could somebody help with understanding multithreading in remote powershell? I am trying to copy a file to remote computer using only WinRM. Here is the algorithm:
The problem is that I get "The process cannot access the file, because it is used by another process" type of errors when executing remote powershell blocks. It looks like powershell executes my script blocks in parallel. But I need sequential execution. Can I override this?
Full code example is available here.
Each time you create a remote PowerShell session, it is using a different PowerShell process to execute the request. This is why you are running into file contention issues. You should consider using the same remote session for each operation rather than creating a new one each time you want to append a chunk.
BTW if you are on (or when you get to) V5, you can use $s = New-PSSession remoteComputerName; Copy-Item src.txt C:\dst.txt -ToSession $s
to copy a file to a remote computer.