1
+ <#
2
+ . SYNOPSIS
3
+ Script for installing and launching cosmos emulator
4
+
5
+ . DESCRIPTION
6
+ This script downloads, installs and launches cosmosdb-emulator.
7
+
8
+ . PARAMETER EmulatorMsiUrl
9
+ Uri for downloading the cosmosdb-emulator
10
+
11
+ . PARAMETER StartParameters
12
+ Parameter with which to launch the cosmosdb-emulator
13
+
14
+ . PARAMETER Stage
15
+ Determines what part of the script to run. Has to be either Install or Launch
16
+ #>
17
+ [CmdletBinding ()]
18
+ Param (
19
+ [string ] $EmulatorMsiUrl = " https://aka.ms/cosmosdb-emulator" ,
20
+ [string ] $StartParameters ,
21
+ [Parameter (Mandatory = $True )]
22
+ [ValidateSet (' Install' , ' Launch' )]
23
+ [string ] $Stage
24
+ )
25
+
26
+ $targetDir = Join-Path $Env: Temp AzureCosmosEmulator
27
+ $logFile = Join-Path $Env: Temp log.txt
28
+ $productName = " Azure Cosmos DB Emulator"
29
+ $emulator = (Join-Path $targetDir (Join-Path $productName " Microsoft.Azure.Cosmos.Emulator.exe" ))
30
+
31
+ if ($Stage -eq " Install" )
32
+ {
33
+ $downloadTryCount = 0
34
+ New-Item $targetDir - Type Directory
35
+ New-Item $logFile - Type File
36
+ do
37
+ {
38
+ # Download and Extract Public Cosmos DB Emulator
39
+ Write-Host " Downloading and extracting Cosmos DB Emulator - $EmulatorMsiUrl "
40
+ Write-Host " Target Directory $targetDir "
41
+ Write-Host " Log File $logFile "
42
+
43
+ $downloadTryCount ++
44
+ Write-Host " Download Try Count: $downloadTryCount "
45
+ Remove-Item - Path (Join-Path $targetDir ' *' ) - Recurse
46
+ Clear-Content - Path $logFile
47
+
48
+ $installProcess = Start-Process msiexec - Wait - PassThru - ArgumentList " /a $EmulatorMsiUrl TARGETDIR=$targetDir /qn /liew $logFile "
49
+ Get-Content $logFile
50
+ Write-Host " Exit Code: $ ( $installProcess.ExitCode ) "
51
+ }
52
+ while (($installProcess.ExitCode -ne 0 ) -and ($downloadTryCount -lt 3 ))
53
+
54
+ if (Test-Path (Join-Path $Env: LOCALAPPDATA CosmosDbEmulator))
55
+ {
56
+ Write-Host " Deleting Cosmos DB Emulator data"
57
+ Remove-Item - Recurse - Force $Env: LOCALAPPDATA \CosmosDbEmulator
58
+ }
59
+
60
+ Write-Host " Getting Cosmos DB Emulator Version"
61
+ $fileVersion = Get-ChildItem $emulator
62
+ Write-Host $emulator $fileVersion.VersionInfo
63
+ }
64
+
65
+ if ($Stage -eq " Launch" )
66
+ {
67
+ Write-Host " Launching Cosmos DB Emulator"
68
+ if (! (Test-Path $emulator )) {
69
+ Write-Error " The emulator is not installed where expected at '$emulator '"
70
+ return
71
+ }
72
+
73
+ $process = Start-Process $emulator - ArgumentList " /getstatus" - PassThru - Wait
74
+ switch ($process.ExitCode ) {
75
+ 1 {
76
+ Write-Host " The emulator is already starting"
77
+ return
78
+ }
79
+ 2 {
80
+ Write-Host " The emulator is already running"
81
+ return
82
+ }
83
+ 3 {
84
+ Write-Host " The emulator is stopped"
85
+ }
86
+ default {
87
+ Write-Host " Unrecognized exit code $ ( $process.ExitCode ) "
88
+ return
89
+ }
90
+ }
91
+
92
+ $argumentList = " "
93
+ if (-not [string ]::IsNullOrEmpty($StartParameters )) {
94
+ $argumentList += , $StartParameters
95
+ } else {
96
+ # Use the default params if none provided
97
+ $argumentList = " /noexplorer /noui /enablepreview /disableratelimiting /enableaadauthentication"
98
+ }
99
+
100
+ Write-Host " Starting emulator process: $emulator $argumentList "
101
+ $process = Start-Process $emulator - ArgumentList $argumentList - ErrorAction Stop - PassThru
102
+ Write-Host " Emulator process started: $ ( $process.Name ) , $ ( $process.FileVersion ) "
103
+
104
+ $Timeout = 600
105
+ $result = " NotYetStarted"
106
+ $complete = if ($Timeout -gt 0 ) {
107
+ $start = [DateTimeOffset ]::Now
108
+ $stop = $start.AddSeconds ($Timeout )
109
+ {
110
+ $result -eq " Running" -or [DateTimeOffset ]::Now -ge $stop
111
+ }
112
+ }
113
+ else {
114
+ {
115
+ $result -eq " Running"
116
+ }
117
+ }
118
+
119
+ do {
120
+ $process = Start-Process $emulator - ArgumentList " /getstatus" - PassThru - Wait
121
+ switch ($process.ExitCode ) {
122
+ 1 {
123
+ Write-Host " The emulator is starting"
124
+ }
125
+ 2 {
126
+ Write-Host " The emulator is running"
127
+ $result = " Running"
128
+ return
129
+ }
130
+ 3 {
131
+ Write-Host " The emulator is stopped"
132
+ }
133
+ default {
134
+ Write-Host " Unrecognized exit code $ ( $process.ExitCode ) "
135
+ }
136
+ }
137
+ Start-Sleep - Seconds 5
138
+ }
139
+ until ($complete.Invoke ())
140
+ Write-Error " The emulator failed to reach Running status within ${Timeout} seconds"
141
+ }
0 commit comments