I use MongoDB both on my server and on my Windows desktop in development mode.
But I don’t need it to work all the time.
And I’m too lazy to go deep into the system panels and switch it on and off permanently.
Moreover, it is impossible to remember the current state of the service.
Yes, I remember with pleasure the service from the apache server with a notification icon in the taskbar.
So I decided to write a powershell script to automate this small task.
This is it, step by step.
Allow MessageBox component (to show gui dialogs).
if ($null -eq ('System.Windows.MessageBox' -as [type])) {
Add-Type -AssemblyName PresentationFramework
}
Try to find the MongoDB system service by name.
$Service = Get-Service -Displayname '*MongoDB Server*'
if (!$Service) {
$Message = 'MongoDB service not found'
Write-Error $Message
[void][System.Windows.MessageBox]::Show($Message, 'Error!', 'OK', 'Error')
exit 1
}
Detect the current status of the service.
$PrevStatus = $Service.Status -eq 'Running'
$NextStatus = -Not $PrevStatus
Try to change the state (it’s good to put this code within try…catch construction to process the errors).
if ($NextStatus) {
Start-Service -InputObject $Service
}
else {
Stop-Service -InputObject $Service
}
The most intriguing part is to change the shortcut icon to indicate the current status (~/OneDrive/Desktop/MongoDB Service.lnk
is the shortcut link on the desktop).
$ShortcutPath = $HOME + '\OneDrive\Desktop\MongoDB Service.lnk';
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutPath)
if ($Shortcut -And $Shortcut.TargetPath) {
$DisabledIcon = '%SystemRoot%\System32\imageres.dll,100'
$EnabledIcon = '%SystemRoot%\System32\imageres.dll,101'
$Icon = if ($NextStatus) {$EnabledIcon} else {$DisabledIcon}
$Shortcut.IconLocation = $Icon
$Shortcut.Save()
}
We’ve used two icons from default windows set (.../System32/imageres.dll
):
- #100: ‘Disabled: a red shield with a cross’,
- #101: ‘Enabled: a green shield with a check mark’.
And the final part is to display the operation status dialog.
$MessageTitle = 'MongoDB Service'
$MessagePrefix = 'Service has been'
$MessagePostfix = if ($NextStatus) {'started'} else {'stopped'}
$MessageText = $MessagePrefix + ' ' + $MessagePostfix
[void][System.Windows.MessageBox]::Show(
$MessageText,
$MessageTitle,
'OK',
'Information'
)
The shortcut and the icon
It’s supposed that icon is located on the windows desktop under the name MongoDB Service.lnk
.
And the target is (~/sbin/toggle-mongodb-service.ps1
is our script name):
powershell
-WindowStyle hidden
-ExecutionPolicy Bypass
-File "%HOME%\sbin\toggle-mongodb-service.ps1"
It’s required to switch the ‘Run as Administrator’ checkbox on:
And, finally, we could have different icons on the desktop for each status:
Links
TODO
It’s required to add ability to update the service state on demand/on the system start, because at the moment it saves only the last state, what couldn’t be always actual. But there is a solution, if the «manual» mode was set for the service, it will restore its last state.