使用 PowerShell 监控共享文件访问
Monitor shared file access using PowerShell
背景
使用 Windows' MMC 的共享文件夹功能,我能够查看通过共享文件夹访问的任何打开的文件。
这给了我一个静态视图;我想编写一个脚本(最好是 PowerShell)来监视此共享并记录谁连接到什么以及何时归档的信息。
我是 运行 Windows Server 2008 R2,很遗憾 Get-SmbOpenFile
不可用。
问题
有没有办法使用 PowerShell 查看 Windows 2008 R2 中通过共享打开的所有文件?
改用net file
和net session
:
& net file | Where-Object {
$_ -match '^(\d+)\s+(.*)\s+(\w+)\s+(\d+)\s*$'
} | ForEach-Object {
New-Object -Type PSObject -Property @{
'ID' = $matches[1]
'File' = $matches[2].Trim()
'User' = $matches[3]
'Locks' = $matches[4]
}
}
& net session | Where-Object {
$_ -match '^(\S+)\s+(\w+)\s+(.*)\s+(\d+)\s+(\S+)\s*$'
} | ForEach-Object {
New-Object -Type PSObject -Property @{
'Client' = $matches[1]
'User' = $matches[2]
'Type' = $matches[3].Trim()
'Open' = $matches[4]
'Idle' = $matches[5]
}
}
背景
使用 Windows' MMC 的共享文件夹功能,我能够查看通过共享文件夹访问的任何打开的文件。 这给了我一个静态视图;我想编写一个脚本(最好是 PowerShell)来监视此共享并记录谁连接到什么以及何时归档的信息。
我是 运行 Windows Server 2008 R2,很遗憾 Get-SmbOpenFile
不可用。
问题
有没有办法使用 PowerShell 查看 Windows 2008 R2 中通过共享打开的所有文件?
改用net file
和net session
:
& net file | Where-Object {
$_ -match '^(\d+)\s+(.*)\s+(\w+)\s+(\d+)\s*$'
} | ForEach-Object {
New-Object -Type PSObject -Property @{
'ID' = $matches[1]
'File' = $matches[2].Trim()
'User' = $matches[3]
'Locks' = $matches[4]
}
}
& net session | Where-Object {
$_ -match '^(\S+)\s+(\w+)\s+(.*)\s+(\d+)\s+(\S+)\s*$'
} | ForEach-Object {
New-Object -Type PSObject -Property @{
'Client' = $matches[1]
'User' = $matches[2]
'Type' = $matches[3].Trim()
'Open' = $matches[4]
'Idle' = $matches[5]
}
}