function Get-OpenFilesOnServers {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string[]]$Servers
)
$allOpenFiles = @()
foreach ($server in $Servers) {
Write-Verbose "Опрос сервера: $server"
try {
$result = Invoke-Command -ComputerName $server -ScriptBlock {
Get-SmbOpenFile -ErrorAction Stop | ForEach-Object {
# Определяем режим открытия на основе маски Permissions
$isWrite = ($_.Permissions -band 0x40000000) -ne 0 -or
($_.Permissions -band 0x00000002) -ne 0 -or
($_.Permissions -band 0x00000004) -ne 0
$openMode = if ($isWrite) { 'Write+Read' } else { 'Read' }
[PSCustomObject]@{
Server = $env:COMPUTERNAME
FileId = $_.FileId
SessionId = $_.SessionId
ClientComputerName = $_.ClientComputerName
ClientUserName = $_.ClientUserName
Path = $_.Path
ShareRelativePath = $_.ShareRelativePath
Locks = $_.Locks
OpenMode = $openMode
Permissions = $_.Permissions
}
}
} -ErrorAction Stop
$allOpenFiles += $result
}
catch {
Write-Warning "Не удалось получить данные с сервера $server : $_"
}
}
if ($allOpenFiles.Count -gt 0) {
$allOpenFiles | Format-Table -AutoSize
}
else {
Write-Host "На указанных серверах открытых файлов не обнаружено."
}
}