# Run in background

**URL:** <https://forums.powershell.org/t/run-in-background/13239>\
**Category:** PowerShell Help\
**Created:** [October 5, 2019, 2:49am UTC](https://forums.powershell.org/t/run-in-background/13239 "2019-10-05T02:49:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![incognitotom](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@incognitotom](https://forums.powershell.org/u/incognitotom)\
**Post date:** [October 5, 2019, 2:49am UTC](https://forums.powershell.org/t/run-in-background/13239/1 "2019-10-05T02:49:10Z")

</div>

Hi,

I have a script that does a number of things. One of these is to restore a DB. The script gets executed during ARM Template deployment, it works perfectly but the deployment takes ages while waiting for the DB to restore and eventually times out.

So I thought I would place the DB restore in a Start-Job script block so that the DB would continue restoring in the background and the job would complete successfully. When running this is my session it works as expected, but during the deployment, it doesn’t work. I’m not really sure why, but I assume it’s because the script is executed under the system account and maybe doesn’t work this way?

So I’m after some advice about what can be done, maybe a better way of executing the script, maybe a way to initial the DB restore but maybe throw a custom output? Once the DB restore is executed the DB will run in the background regardless of the PowerShell session running.

Many thanks in advance 🙂

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![emiltanev](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@emiltanev](https://forums.powershell.org/u/emiltanev)\
**Post date:** [October 6, 2019, 3:44am UTC](https://forums.powershell.org/t/run-in-background/13239/2 "2019-10-06T03:44:04Z")

</div>

Have you tried:

`Invoke-Command localhost -scriptblock { your script here } -asjob`

---

<div class="post-metadata">

**Author:** ![incognitotom](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@incognitotom](https://forums.powershell.org/u/incognitotom)\
**Post date:** [October 7, 2019, 3:59am UTC](https://forums.powershell.org/t/run-in-background/13239/3 "2019-10-07T03:59:42Z")

</div>

Thanks Emil. I have tried this but I get the same results as when running start-job

This is tricky ☹

---

<div class="post-metadata">

**Author:** ![masterofthehat](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/masterofthehat/32/5116_2.png) [@masterofthehat](https://forums.powershell.org/u/masterofthehat)\
**Post date:** [October 7, 2019, 9:09am UTC](https://forums.powershell.org/t/run-in-background/13239/4 "2019-10-07T09:09:14Z")

</div>

Sounds like the user that the script is executing as doesn’t have rights to something, as you guessed. Have you tried using your account in the -Credential parameter on Start-Job to see if that makes a difference? Some code and some more details on how the script is executed may help, too.

---

<div class="post-metadata">

**Author:** ![incognitotom](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@incognitotom](https://forums.powershell.org/u/incognitotom)\
**Post date:** [October 8, 2019, 4:22am UTC](https://forums.powershell.org/t/run-in-background/13239/5 "2019-10-08T04:22:10Z")

</div>

Hi Charles,

 Below is the part of the script/function that I need to execute

```
$Scripts = Get-ChildItem "C:\WindowsAzure\Setup\AppSetup\SQLScripts"
        foreach ($Script in $Scripts.Name){
                 Invoke-Sqlcmd -ServerInstance localhost\DBINSTANCE -Username sa -Password ********** -Querytimeout 0 -InputFile "C:\WindowsAzure\Setup\AppSetup\SQLScripts\$script" -ErrorAction SilentlyContinue
          }

```
    $smo = 'Microsoft.SqlServer.Management.Smo.'
    $wmi = new-object ($smo + 'Wmi.ManagedComputer')
    $uri = "ManagedComputer[@Name='NODENAME']/ ServerInstance[@Name='INSTANCENAME']/ServerProtocol[@Name='Tcp']"
    $Tcp = $wmi.GetSmoObject($uri)
    $Tcp.IsEnabled = $true
    $Tcp.Alter()
    $wmi.GetSmoObject($uri + "/IPAddress[@Name='IPAll']").IPAddressProperties
    $wmi.GetSmoObject($uri + "/IPAddress[@Name='IPAll']").IPAddressProperties[1].Value="51232"
    $wmi.GetSmoObject($uri + "/IPAddress[@Name='IPAll']").IPAddressProperties
    $Tcp.Alter()
    $SQLService = Get-Service -Name 'MSSQL$INSTANCENAME' | Restart-Service</pre>

```

If I wrap this in a Start-Job scriptBlock or invoke-command -asJob it fails when being deployed into Azure. If I run the same script in a session they work every way. Its only when deploying via ARM that it fails to execute.

The ARM Template deployment justs download the script and executes it.

Thanks

&nbsp;
```

---

<div class="post-metadata">

**Author:** ![incognitotom](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@incognitotom](https://forums.powershell.org/u/incognitotom)\
**Post date:** [October 8, 2019, 4:24am UTC](https://forums.powershell.org/t/run-in-background/13239/6 "2019-10-08T04:24:10Z")

</div>

Not sure why it’s showing all the red ☹ Hopefully you can make it out

---

<div class="post-metadata">

**Author:** ![emiltanev](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@emiltanev](https://forums.powershell.org/u/emiltanev)\
**Post date:** [October 8, 2019, 4:38am UTC](https://forums.powershell.org/t/run-in-background/13239/7 "2019-10-08T04:38:40Z")

</div>

Have you been able to get the error? Why is the error action above?

---

<div class="post-metadata">

**Author:** ![incognitotom](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@incognitotom](https://forums.powershell.org/u/incognitotom)\
**Post date:** [October 8, 2019, 4:45am UTC](https://forums.powershell.org/t/run-in-background/13239/8 "2019-10-08T04:45:27Z")

</div>

It’s because one of the SQL scripts throws a validation error, so I want it to continue regardless. That won’t cause it would it? I don’t get any actual errors from deployment.

&nbsp;

---

<div class="post-metadata">

**Author:** ![emiltanev](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@emiltanev](https://forums.powershell.org/u/emiltanev)\
**Post date:** [October 8, 2019, 4:54am UTC](https://forums.powershell.org/t/run-in-background/13239/9 "2019-10-08T04:54:50Z")

</div>

It must be a credential issue, so using credential should help rule that out. Another cause may be some variable not being defined in the scriptblock. You could try to Start-Job with -filename where the file name is your script that is failing.

---

<div class="post-metadata">

**Author:** ![incognitotom](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@incognitotom](https://forums.powershell.org/u/incognitotom)\
**Post date:** [October 9, 2019, 8:51am UTC](https://forums.powershell.org/t/run-in-background/13239/10 "2019-10-09T08:51:48Z")

</div>

Ive pass through the creds, but it still doesn’t work. I can’t work this out.

```
$password = ConvertTo-SecureString "MyPassword" -AsPlainText -Force
        $Cred = New-Object System.Management.Automation.PSCredential ('MyUser', $password)

        Start-Job -Credential $Cred -ScriptBlock {

            $Scripts = Get-ChildItem "C:\WindowsAzure\DatabaseSetup\SQLScripts"
            foreach ($Script in $Scripts.Name){
                   Invoke-Sqlcmd -ServerInstance localhost\InstanceName -Username sa -Password ********** -Querytimeout 0 -InputFile "C:\WindowsAzure\DatabaseSetup\SQLScripts\$script" -ErrorAction SilentlyContinue
               }
       
    }
```

---

<div class="post-metadata">

**Author:** ![emiltanev](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@emiltanev](https://forums.powershell.org/u/emiltanev)\
**Post date:** [October 9, 2019, 9:51am UTC](https://forums.powershell.org/t/run-in-background/13239/11 "2019-10-09T09:51:45Z")

</div>

What does Receive-Job with -Keep give you?

---

<div class="post-metadata">

**Author:** ![incognitotom](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@incognitotom](https://forums.powershell.org/u/incognitotom)\
**Post date:** [October 9, 2019, 10:38am UTC](https://forums.powershell.org/t/run-in-background/13239/12 "2019-10-09T10:38:41Z")

</div>

Funny thing is the job isn’t even listed, so it looks like its not even being trigger ☹

---

<div class="post-metadata">

**Author:** ![incognitotom](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@incognitotom](https://forums.powershell.org/u/incognitotom)\
**Post date:** [October 10, 2019, 4:32am UTC](https://forums.powershell.org/t/run-in-background/13239/13 "2019-10-10T04:32:16Z")

</div>

I logged the attempt to start the job, and below is the output. So it looks like its trying to start the job but failing. I don’t seem to be able to find anymore info to why it’s failing. ☹

&nbsp;

JobStateInfo : Failed  
Finished : System.Threading.ManualResetEvent  
InstanceId : 3eabbdfd-c13d-4047-82d7-ea7a9babbf5b  
Id : 6  
Name : Job6  
ChildJobs : {Job7}  
PSBeginTime : 10/10/2019 10:56:11 AM  
PSEndTime : 10/10/2019 10:56:12 AM  
PSJobTypeName : BackgroundJob  
Output : {}  
Error : {}  
Progress : {}  
Verbose : {}  
Debug : {}  
Warning : {}  
Information : {}  
State : Failed

---

<div class="post-metadata">

**Author:** ![emiltanev](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@emiltanev](https://forums.powershell.org/u/emiltanev)\
**Post date:** [October 10, 2019, 10:12am UTC](https://forums.powershell.org/t/run-in-background/13239/14 "2019-10-10T10:12:57Z")

</div>

What do you have here?

`Get-Job -id 6 | Select -ExpandProperty ChildJobs | Select *`

---

<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:31pm UTC](https://forums.powershell.org/t/run-in-background/13239/15 "2024-05-16T20:31:14Z")

</div>


