Functions/UserManagement/Set-InitialUserStatusUsingLogFile.ps1

<#
.Synopsis
    Sets all users with the state enabled in the supplied .json file to enabled in Business Central.
 
.DESCRIPTION
    The .json file should contain the properties UserSecurityID, UserName, State of the Business Central user.
    Usually this cmdlet is used together with Set-AllUsersDisabled and/or New-BCUserStateJsonFile.
    Above cmdlets write a Json file to the Program Data\4ps\BcUsers folder and default this cmdlets reads the most recent generated json file from that location.
 
.Example
    Set-InitialUserStatusUsingLogFile -ServerInstance 'BC170'
 
.Example
    Set-InitialUserStatusUsingLogFile -ServerInstance 'BC170' -PathBcUsers 'c:\temp\users.json'
#>


function Set-InitialUserStatusUsingLogFile {
    param
    (
        [parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true)]
        [string] $ServerInstance,
        [string] $PathBcUsers = (Join-Path -Path $env:ProgramData -ChildPath '4ps\BcUsers')
    )

    $UsersServerInstance = Get-NAVServerUser -ServerInstance $ServerInstance | Select-Object -Property UserSecurityID, UserName, State
    $JsonFile = (Get-ChildItem -Path $PathBcUsers | Sort-Object LastWriteTime -Descending)[0]

   
   if ((Test-Path -Path $JsonFile.FullName) -eq $false){
        Write-Host 'No log file found'
        return
    }

    $UserStatusData = Get-Content -Path  $JsonFile.FullName | ConvertFrom-JSON 
    $UsersJson = $UserStatusData | Select-Object -Property UserSecurityID, UserName, State
    $UsersEnabled = $UsersJson | Where-Object {$_.State -eq "Enabled"}
    
    foreach  ($User in $UsersServerInstance){
        if($User.UserSecurityID -in $UsersEnabled.UserSecurityID){ 
            Set-NAVServerUser -UserName $User.UserName -ServerInstance $ServerInstance -State 'Enabled'           
        } 
    }   
 }

 Export-ModuleMember -Function Set-InitialUserStatusUsingLogFile