<# Copyright 2020 Laserfiche Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. #> <# .SYNOPSIS This script enumerates active sessions for the specified repository, using the current Windows Account for authentication. #> param ( # Specifies the LFS server name [Parameter(Mandatory = $true)] [string]$serverName, # Specifies the repository name [Parameter(Mandatory = $true)] [string]$repoName, # Option to use secure port, defaults to false [Parameter(Mandatory = $false)] [boolean]$useTLS = $false, [Parameter(Mandatory = $false)] [string]$usernameFilter = '' ) #Load RepositoryAccess library from GAC try { [System.Reflection.Assembly]::LoadWithPartialName('Laserfiche.RepositoryAccess') | Out-Null Write-Host 'Successfully loaded the Laserfiche Repository Access (RA) library from the GAC' -ForegroundColor Green } catch { $exceptionMessage = $_.Exception.Message Write-Host 'Failed to load the Laserfiche Repository Access (RA) library from the GAC with exception message:' -ForegroundColor Red Write-Host "$exceptionMessage" -ForegroundColor Red Write-Host 'Exiting' -ForegroundColor Red Exit } # Set port for connecting to LFS $lfsPort = 80 if ($useTLS) { $lfsPort = 443 } $server = New-Object -TypeName Laserfiche.RepositoryAccess.Server($serverName, $lfsPort, $useTLS) $repo = $server.GetRepositories() | Where-Object { $_.Name -eq $repoName } if (!$repo) { Write-Host "Repository $repo was not found, exiting" -ForegroundColor Red Exit } # Login to repository using current user's Window's credentials $session = New-Object -TypeName Laserfiche.RepositoryAccess.Session $session.Connect($repo) try { $session.LogIn() Write-Host "`nSuccessfully logged into repository $($repo.Name) with current Windows credentials`n" -ForegroundColor Green } catch { Write-Host "Unable to log into repository $($repo.Name) with current Windows credentials" -ForegroundColor Red Exit } # Get sessions $run = $true $myRepo = New-Object -TypeName Laserfiche.RepositoryAccess.Repository($session) $filterUsers = (![string]::IsNullOrWhiteSpace($usernameFilter)) Do { $sessionList = $null $sessionList = [System.Collections.ArrayList]@() $activeSessions = $null $activeSessions = [Laserfiche.RepositoryAccess.Repository]::EnumSessions($session) if ($filterUsers) { foreach ($activeSession in $activeSessions) { if ($activeSession.UserName -eq $usernameFilter) { $sessionList.Add($activeSession.UserName) | Out-Null } } } else { foreach ($activeSession in $activeSessions) { $sessionList.Add($activeSession.UserName) | Out-Null } } $timestamp = $null $timestamp = Get-Date -Format "HH:mm:ss MM/dd/yy" Write-Host "`n" Write-Host "[$timestamp]" $sessionList | Group-Object -NoElement Write-Host "`n" Write-Host 'Press to run again or any other key to exit: ' $runAgain = [console]::ReadKey() [String]$inputChar = $runAgain.KeyChar if ($inputChar.ToUpper() -ne 'Y') { $run = $false } else { $myRepo.Refresh() } } while ($run) # Cleanup $session.Close() $server.Dispose() Write-Host "`nRepository session listing script complete" -ForegroundColor Green