# Starting a remote job

**URL:** <https://forums.powershell.org/t/starting-a-remote-job/8375>\
**Category:** PowerShell Help\
**Created:** [April 6, 2017, 9:32am UTC](https://forums.powershell.org/t/starting-a-remote-job/8375 "2017-04-06T09:32:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marius1956](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@marius1956](https://forums.powershell.org/u/marius1956)\
**Post date:** [April 6, 2017, 9:32am UTC](https://forums.powershell.org/t/starting-a-remote-job/8375/1 "2017-04-06T09:32:18Z")

</div>

I need to start a job on a remote computer and make it terminate asynchronously.  
In other words, after successfully establishing a remote session I want to start a job on the remote computer and let it go on even after the session is closed, because it can take a long time.  
The code I wrote looks like:

```
#
# Here I create my remote session $my_session
#
invoke-Command -Session $my_session -ScriptBlock {
        $my_job = Start-Job -Name "remote job" -ScriptBlock {
            $my_logfile = "c:\myfolder\mylog.txt"
            $my_message = "I started"
            $my_message >> $my_logfile 
            
            $my_message = "I terminated"
            $my_message >> $my_logfile 
            }
       } 
#
```

Something must be wrong because I don’t see any log created on the target server and the code in the scriptblock is simply terminated.  
How can create a job so that it can be execuded and terminated asynchronously?  
How can I see the reasons becose the job fails?  
Regards  
marius

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [April 6, 2017, 9:34am UTC](https://forums.powershell.org/t/starting-a-remote-job/8375/2 "2017-04-06T09:34:22Z")

</div>

Yeah, won’t work. Jobs run within the process, and once you close the session, the process ends - so, no job. What you want to do instead is set up a PSScheduledJob and have it run, like, a minute later or right then. Scheduled Jobs exist out-of-process, and persist their results to disk.

Your other alternative is to disconnect your session, which leaves it running, and then reconnect later to get your results. But a disconnected session will eventually time out and close.

---

<div class="post-metadata">

**Author:** ![marius1956](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@marius1956](https://forums.powershell.org/u/marius1956)\
**Post date:** [April 7, 2017, 5:27am UTC](https://forums.powershell.org/t/starting-a-remote-job/8375/3 "2017-04-07T05:27:39Z")

</div>

Thank you for the usefull suggestion.  
I made some tests and it workd fine.  
However, I discovered that PSScheduledJob is available only since PowerShell 3, while I need to use PowerShell 2.0: any other idea?  
Regards  
marius

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:38pm UTC](https://forums.powershell.org/t/starting-a-remote-job/8375/4 "2024-05-16T20:38:56Z")

</div>


