In this post I will show how you can toggle maintenance mode on or off using PowerShell. The script is intended for OneBox environments. Just paste it into a new ps1 file for future use, or run it through PowerShell ISE.
DISCLAIMER: Don't run this unless you are prepared to take the heat from restarting the entire web application. It stops and starts the web server.
function ToggleMaintenanceMode() { $parm = @{ ServerInstance = 'localhost' Database = 'AxDB' Query = "UPDATE SQLSYSTEMVARIABLES SET [VALUE] = IIF([VALUE]=1, 0, 1) WHERE PARM = 'CONFIGURATIONMODE'" } Get-Service "W3SVC" | Stop-Service -Force Invoke-Sqlcmd @parm Get-Service "W3SVC" | Start-Service $parm.Query = "SELECT [VALUE] FROM SQLSYSTEMVARIABLES WHERE PARM = 'CONFIGURATIONMODE'" $result = Invoke-Sqlcmd @parm [int]$value = $result.Value Write-Output "Configuration mode $(('disabled','enabled')[$value])" } ToggleMaintenanceMode
The script shows you how you can easily run SQL commands, and even retrieve values back to your PowerShell script.
Enjoy!
No comments:
Post a Comment