PowerShell script to rebuild the Snow MongoDB Windows service
Use this script to recreate the SnowMongoDB Windows service for Snow License Manager. The script detects the Snow License Manager installation, validates the MongoDB executable path, recreates the service with the expected configuration, and then verifies and starts it.
What the script does
1. Detects Snow License Manager installation
- Uses Get-CimInstance to find the installation path of Snow License Manager.
- If not found, logs an error and stops execution.
2. Service configuration
- Defines:
- Service Name: SnowMongoDB
- Display Name: Snow Software MongoDB Service
- Description: Storage of cached Snow License Manager objects
- Startup Type: Automatic
- Builds paths for:
- MongoDB executable (
mongod.exe) - Database directory
- Log file for MongoDB
- MongoDB executable (
3. Validation
- Checks if
mongod.exeexists in the expected location. - If missing, logs an error and terminates.
4. Service creation
- Constructs the binPath for the service, including MongoDB options (--auth, --dbpath, --logpath, --service).
- Runs
sc.execreate to register the service with Windows Service Control Manager. - Implements retry logic with alternative quoting if the first attempt fails.
- Adds a description using
sc.exedescription.
5. Verification
- Executes
sc.exeqcto query the service configuration. - Logs the output and checks for errors.
Prerequisites
- Run PowerShell as Administrator.
- Ensure the Snow License Manager installation exists on the target machine.
# ==============================
# Snow License Manager MongoDB Service Setup V1.3
# ==============================
# Log file path
$logFile = "C:\Temp\SnowMongoDB_ServiceSetup.log"
if (-not (Test-Path (Split-Path $logFile))) {
New-Item -ItemType Directory -Path (Split-Path $logFile) -Force
}
# Logging function
function Write-Log {
param (
[string]$Message,
[string]$Level = "INFO"
)
$timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
$entry = "$timestamp [$Level] $Message"
Write-Host $entry
Add-Content -Path $logFile -Value $entry
}
# Detect installation path using CIM
$slmInstallLoc = (Get-CimInstance -ClassName Win32_Product | Where-Object { $_.Name -like "Snow License*" } | Select-Object -ExpandProperty InstallLocation)
if (-not $slmInstallLoc) {
Write-Log "ERROR: Snow License Manager installation not found."
throw "Snow License Manager installation not found."
}
# Ensure trailing backslash
if ($slmInstallLoc[-1] -ne '\') {
$slmInstallLoc += '\'
}
# Service details
$serviceName = "SnowMongoDB"
$displayName = "Snow Software MongoDB Service"
$description = "Storage of cached SLM objects"
$startupType = "auto"
# Build paths
$binExe = Join-Path $slmInstallLoc 'Services\MongoDB\bin\mongod.exe'
$dbPath = Join-Path $slmInstallLoc 'Services\MongoDB\data'
$logPath = Join-Path $slmInstallLoc 'Services\MongoDB\log\mongod.log'
# Validate executable exists
if (-not (Test-Path $binExe)) {
Write-Log "ERROR: MongoDB executable not found at $binExe"
throw "MongoDB executable missing."
}
# Build binPath
$binPath = '"{0}" --auth --dbpath="{1}" --logpath="{2}" --service' -f $binExe, $dbPath, $logPath
# Commands
$createCmd = 'sc.exe create ' + $serviceName + ' binPath= "' + $binPath + '" start= ' + $startupType + ' DisplayName= "' + $displayName + '"'
$descCmd = 'sc.exe description ' + $serviceName + ' "' + $description + '"'
# Idempotency check
if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue) {
Write-Log "Service already exists. Skipping creation."
} else {
Write-Log "Creating service with command: $createCmd"
$createResult = cmd /c $createCmd
$exitCode = $LASTEXITCODE
Add-Content -Path $logFile -Value ($createResult | Out-String)
# Retry logic if first attempt fails
if ($exitCode -ne 0) {
Write-Log "WARNING: First attempt failed with exit code $exitCode. Retrying with alternative quoting..."
$binPathRetry = '\"{0}\" --auth --dbpath=\"{1}\" --logpath=\"{2}\" --service' -f $binExe, $dbPath, $logPath
$createCmdRetry = 'sc.exe create ' + $serviceName + ' binPath= "' + $binPathRetry + '" start= ' + $startupType + ' DisplayName= "' + $displayName + '"'
Write-Log "Retrying with command: $createCmdRetry"
$createResult = cmd /c $createCmdRetry
$exitCode = $LASTEXITCODE
Add-Content -Path $logFile -Value ($createResult | Out-String)
if ($exitCode -ne 0) {
Write-Log "ERROR: Retry failed with exit code $exitCode."
Write-Log "Output: $createResult"
throw "Service creation failed with exit code $exitCode"
}
}
# Run description command
cmd /c $descCmd | Add-Content -Path $logFile
}
# Verification
Write-Log "Verifying service configuration..."
$verifyResult = cmd /c "sc.exe qc $serviceName"
Add-Content -Path $logFile -Value ($verifyResult | Out-String)
if ($verifyResult -match "FAILED") {
Write-Log "ERROR: Service verification failed."
throw "Service verification failed."
}
# ==============================
# Summary, start and confirmation
# ==============================
function Show-ServiceSummary {
param([string]$Name)
# Read service properties via CIM (safe & fast)
$svc = Get-CimInstance -ClassName Win32_Service -Filter "Name='$Name'" -ErrorAction SilentlyContinue
if (-not $svc) {
Write-Log "ERROR: Service '$Name' not found when summarising." -Level ERROR
Write-Host "Service '$Name' not found." -ForegroundColor Red
return
}
# Dependencies
$deps = $svc.Dependencies
if (-not $deps -or $deps.Count -eq 0) {
$depsText = "(none)"
} else {
$depsText = ($deps -join ", ")
}
# Description fallback (PowerShell 5.1 friendly)
$descText = if ($null -eq $svc.Description -or '' -eq $svc.Description) { "(not set)" } else { $svc.Description }
# Print summary to console
Write-Host ""
Write-Host "================ Service Creation Summary ================"
Write-Host ("Name : {0}" -f $svc.Name)
Write-Host ("DisplayName : {0}" -f $svc.DisplayName)
Write-Host ("Path : {0}" -f $svc.PathName)
Write-Host ("Description : {0}" -f $descText)
Write-Host ("Dependencies: {0}" -f $depsText)
Write-Host ("StartType : {0}" -f $svc.StartMode)
Write-Host ("Logon : {0}" -f $svc.StartName)
Write-Host "=========================================================="
Write-Host ""
}
function Start-ServiceAndConfirm {
param(
[string]$Name,
[int]$TimeoutSec = 30
)
Write-Log "Starting service '$Name'..."
try {
Start-Service -Name $Name -ErrorAction Stop
Write-Log "Service '$Name' start command issued."
} catch {
Write-Log "ERROR: Start-Service failed: $($_.Exception.Message)" -Level ERROR
Write-Host ("ERROR: Start-Service failed: {0}" -f $_.Exception.Message) -ForegroundColor Red
throw
}
# Wait until Running or timeout
$elapsed = 0
do {
Start-Sleep -Seconds 2
$svc = Get-Service -Name $Name -ErrorAction SilentlyContinue
$elapsed += 2
} while ($svc -and $svc.Status -ne 'Running' -and $elapsed -lt $TimeoutSec)
if ($svc -and $svc.Status -eq 'Running') {
Write-Log "Service '$Name' is running successfully."
Write-Host ("SUCCESS: Service '{0}' is Running." -f $Name) -ForegroundColor Green
return $true
} else {
Write-Log "ERROR: Service '$Name' failed to reach 'Running' in $TimeoutSec s. Current status: $($svc.Status)" -Level ERROR
Write-Host ("ERROR: Service '{0}' did not reach 'Running' (status: {1})." -f $Name, $svc.Status) -ForegroundColor Red
return $false
}
}
# If we have the raw SC output from creation, show it to the screen as well
if ($createResult) {
Write-Host ""
Write-Host "===== Raw 'sc.exe create' Output ====="
($createResult | Out-String).Trim() | Write-Host
Write-Host "======================================"
Write-Host ""
}
# Show the current service configuration as seen by SCM
Show-ServiceSummary -Name $serviceName
# Attempt start and confirm outcome
$ok = Start-ServiceAndConfirm -Name $serviceName -TimeoutSec 30
if (-not $ok) {
throw "Service '$serviceName' failed to start or confirm running."
}Was this helpful?
© 2026 Flexera. All rights reserved.