Instantly share code, notes, and snippets.
Last active
May 5, 2020 14:39
Manage NAV Application Server instances service state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function StartStopNSTs($action) | |
{ | |
Import-Module "${env:ProgramFiles(x86)}\Microsoft Dynamics NAV\100\RoleTailored Client\Microsoft.Dynamics.Nav.Model.Tools.psd1" -WarningAction SilentlyContinue | out-null | |
Import-Module "$env:ProgramFiles\Microsoft Dynamics NAV\100\Service\NavAdminTool.ps1" -WarningAction SilentlyContinue | Out-Null | |
Import-Module "${env:ProgramFiles(x86)}\Microsoft Dynamics NAV\100\RoleTailored Client\Microsoft.Dynamics.Nav.Apps.Tools.psd1" -WarningAction SilentlyContinue | Out-Null | |
$NSTs = Get-NAVServerInstance | Where-Object -Property 'State' -eq 'Running' | |
if(!$NSTs) { | |
Write-Output "No NSTs found" | |
} | |
else | |
{ | |
$NSTsCnt = Get-NAVServerInstance | Where-Object -Property 'State' -eq 'Running' | Measure-Object | |
Write-Output "NSTs found in: $($NSTsCnt.Count)" | |
if($action -eq "running") | |
{ | |
Write-Output $NSTs | |
return | |
} | |
Foreach ($NST in $NSTs) | |
{ | |
if($action -eq "start") | |
{ | |
$NST | Set-NAVServerInstance -Start | |
Write-Host "Starting $($NST.DisplayName)" | |
} | |
elseif($action -eq "stop") | |
{ | |
$NST | Set-NAVServerInstance -Stop | |
Write-Host "Stopping $($NST.DisplayName)" | |
} | |
elseif($action -eq "restart") | |
{ | |
$NST | Set-NAVServerInstance -Restart | |
Write-Host "Restarting $($NST.DisplayName)" | |
} | |
else | |
{ | |
write-host "Unknown action" | |
} | |
} | |
} | |
} | |
## Make sure you don't run this in production carelessly | |
StartStopNSTs "running" #this is harmless :) | |
## Other flavors below... | |
# StartStopNSTs "start" | |
# StartStopNSTs "stop" | |
# StartStopNSTs "restart" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment