Functions/UserManagement/Set-AllUsersDisabled.ps1

<#
.Synopsis
    Sets all Business Central users to state 'disabled' to prevent them from accessing Business Central with exception of the user running the script.
.DESCRIPTION
    This cmdlet executes the following actions:
     - Imports the Business Central Powershell modules corresponding with the supplied BC Server Instance version.
     - Reads the users from the BC Server Instances. The ServerInstance must be running to execute this.
     - Saves the current user status to a JSON file in the ProgramData folder. You need elevated rights to write in this folder.
     - Set all users except the user running the script to status disabled.
 
    This script is usefull to for example prevent users from accessing Business Central during an upgrade process.
.Example
    Set-AllUsersDisabled -ServerInstance 'BC170'
 
.Example
    'BC170' | Set-AllUsersDisabled
#>


function Set-AllUsersDisabled {
    [CmdletBinding()]
    param 
    (
        [parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true)]
        [string] $ServerInstance
    )

    Import-BcModule -ServerInstance $ServerInstance -ManagementModule
    #Creates a Log file before all Users are set to Disabled
    New-BCUserStateJsonFile -ServerInstance $ServerInstance
 
    $AllUsers = Get-NAVServerUser -ServerInstance $ServerInstance


    foreach  ($User in $AllUsers){
        if ($User.UserName -eq (whoamI)){
            Set-NAVServerUser -UserName $User.UserName -ServerInstance $ServerInstance -State Enabled
        }

        else{
            Set-NAVServerUser -UserName $User.UserName -ServerInstance $ServerInstance -State Disabled
        }
    }
}


Export-ModuleMember -Function Set-AllUsersDisabled