【发布时间】:2015-07-20 15:37:37
【问题描述】:
如何从计算机远程调用 Openfiles.exe(位于服务器 2008 文件服务器上)以查看用户打开了哪些文件?我还需要让它在参数中以域管理员用户身份登录。
【问题讨论】:
标签: powershell command-line vbscript command-prompt dsquery
如何从计算机远程调用 Openfiles.exe(位于服务器 2008 文件服务器上)以查看用户打开了哪些文件?我还需要让它在参数中以域管理员用户身份登录。
【问题讨论】:
标签: powershell command-line vbscript command-prompt dsquery
无需创建 CSV 文件并在以后删除它,或者担心它的大小。
openfiles /s MyFileSrv /query /fo CSV /v /u admin1 /p myPass | convertfrom-csv |?{$_.'Open File (Path\executable)' -match "MyFolder"} |select 'Accessed By', 'Open Mode', 'Open File (Path\executable)
Accessed By Open Mode Open File (Path\executable)
----------- --------- ---------------------------
robinsonl Write + Read D:\Dept\MyFolder
smithd Read D:\Dept\MyFolder\info
如果您使用的是 powershell 1.0,则添加 |select -skip 8|在 convertfrom-csv 部分之前。
【讨论】:
您现在可以使用 PowerShell 命令 Get-SmbOpenFile 执行此操作。
【讨论】:
这是一个无需创建临时文件的解决方案。
function Get-OpenFile
{
Param
(
[string]$ComputerName
)
$openfiles = openfiles.exe /query /s $computerName /fo csv /V
$openfiles | ForEach-Object {
$line = $_
if ($line -match '","')
{
$line
}
} | ConvertFrom-Csv
}
Get-OpenFile -ComputerName server1
【讨论】:
在 PowerShell 中执行此操作并允许搜索的方法是使用这个小函数。
function Get-OpenFiles {
cls
openfiles /query /s $args[0] /fo csv /V | Out-File -Force C:\temp\openfiles.csv
$search = $args[1]
Import-CSV C:\temp\openfiles.csv | Select "Accessed By", "Open Mode", "Open File (Path\executable)" | Where-Object {$_."Open File (Path\executable)" -match $search} | format-table -auto
Remove-Item C:\temp\openfiles.csv
}
它允许你打电话给Get-OpenFiles Server Filename,它会告诉你结果。需要注意的是,您有一个名为 C:\temp 的文件夹。
所以如果我这样做 Get-OpenFiles TestServer Hardware 我会得到以下结果。
Accessed By Open Mode Open File (Path\executable)
----------- --------- ---------------------------
NFDJWILL Read N:\Network Services\Documentation\Hardware.xlsx
NFDJWILL Write + Read N:\Network Services\Documentation\Hardware.xlsx
NFDJWILL Read N:\Network Services\Documentation\Hardware.xlsx
【讨论】:
Openfiles 可以直接做服务器。您不必远程运行它。
来自帮助openfiles /query /?
OPENFILES /Query /S system /U username /P password /NH
【讨论】:
username@domain或domain\username。
openfiles /query | findstr /i /c:"some part of a folder name"
openfiles.exe /query /S Serenity /U "Serenity\David Candy" /p "Pass" /FO csv /v | findstr /i /C:"Documents" > C:\test\test.csv
/c:"documents" c: 不是驱动器号。不要改变它。输入findstr /?
您可以为此使用远程委托的 powershell 会话。
Use Delegated Administration and Proxy Functions
您可以在会话配置的 RunAs 设置中委派管理员凭据,并将会话限制为只能运行 openfiles.exe。然后,您可以将使用会话的权限分配给选定的组或用户。这使您可以让人们运行需要域管理员权限的 cmdlet 或程序,而无需向他们提供域管理员凭据。
【讨论】:
如何使用 Power Shell 远程运行应用程序 https://technet.microsoft.com/en-us/library/dd819505.aspx
您的 PS 或 BAT 脚本将是:
openfiles.exe > c:\temp\openfiles.txt
当然最好使用不同的输出文件夹和文件名。
替代方式: https://technet.microsoft.com/en-ca/sysinternals/bb897553.aspx -- PsExec -- PsExec 是一个轻量级的 telnet 替代品,可让您在其他系统上执行进程,并与控制台应用程序完全交互,而无需手动安装客户端软件。
【讨论】: