Well, here's a show stopper for scripting:
Enter-PSSession -ComputerName $remote_computer
Enter-PSSession
should only ever be used interactively at the console. In a script, you should new up a PSSession and use that with Invoke-Command e.g.:
$session = New-PSSession $remote_computer
Invoke-Command -Session $session -ScriptBlock { ..your script here ...}
Remove-PSSession $session
Note that any parameters that the script requires, need to be passed in explicity e.g.:
Invoke-Command -Session $session -ScriptBlock {param($name) ..your script here ...} -Arg 'A Name'
Your script is complex enough that I would put the "remote execution" bits in a file say remote.ps1
and then pass that to the remote computer for execution e.g.:
Invoke-Command -Session $session -FilePath .\remote.ps1
The cool thing about this approach is that PowerShell will get the contents of the "local" script file and send it to the remote computer where it is executed. That way, you don't have to worry about deploying the script to the remote computers or dealing with second hop issues because the script is on a network share.