summaryrefslogtreecommitdiff
path: root/cpp/src/tests/background.ps1
blob: 096379860a8ecc7b3c8fd664abd437703c8e8fc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# From http://ps1.soapyfrog.com/2007/01/22/running-pipelines-in-the-background/
# Copyright © 2006-2009 Adrian Milliner
param(
    [scriptblock] $script,  # scriptblock to run
    [switch] $inconsole      # don't create a new window
)

# break out of the script on any errors
trap { break }

# encode the script to pass to the child process...
$encodedString = [convert]::ToBase64String(
    [Text.Encoding]::Unicode.GetBytes([string] $script))

# create a new process
$p = new-object System.Diagnostics.Process

# create a startinfo object for the process
$si = new-object System.Diagnostics.ProcessStartInfo
$si.WorkingDirectory = $pwd

if ($inconsole)
{ 
    $si.UseShellExecute = $false
}
Else
{
    $si.UseShellExecute = $true
}

# set up the command and arguments to run
$si.FileName = (get-command powershell.exe).Definition
$si.Arguments = "-encodedCommand $encodedString"

# and start the powershell process
[diagnostics.process]::Start($si)