简体   繁体   English

Openfiles查询以查看打开的文件

[英]Openfiles query to see open files

How can I call the Openfiles.exe (which is on the server 2008 file server) remotely from a computer to see which files are open by the users? 如何从计算机远程调用Openfiles.exe(位于服务器2008文件服务器上)以查看用户打开哪些文件? I also need to have it login as the domain admin user in the parameters. 我还需要在参数中以域管理员用户身份登录。

Openfiles can do a server direct. Openfiles可以直接执行服务器。 You don't have to run it remotely. 您不必远程运行它。

From Help openfiles /query /? 来自帮助openfiles /query /?

OPENFILES /Query /S system /U username /P password /NH

A way to do it in PowerShell and to allow for searching would be with this small function. 在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
}

It allows you to call Get-OpenFiles Server Filename and it will show you the results. 它允许您调用Get-OpenFiles Server Filename ,它将显示结果。 The caveat is that you have a folder called C:\\temp. 需要注意的是,您有一个名为C:\\ temp的文件夹。

So if I do Get-OpenFiles TestServer Hardware I get the below. 因此,如果我使用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

Here is a solution without creating temporary files. 这是一个没有创建临时文件的解决方案。

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命令Get-SmbOpenFile执行此操作。

how to run application remotely by using Power Shell https://technet.microsoft.com/en-us/library/dd819505.aspx 如何使用Power Shell远程运行应用程序https://technet.microsoft.com/en-us/library/dd819505.aspx

your PS or BAT script will be: 您的PS或BAT脚本将是:

openfiles.exe > c:\temp\openfiles.txt

of course better use different output folder and file name. 当然最好使用不同的输出文件夹和文件名。

Alternative way: https://technet.microsoft.com/en-ca/sysinternals/bb897553.aspx -- PsExec -- PsExec is a light-weight telnet-replacement that lets you execute processes on other systems, complete with full interactivity for console applications, without having to manually install client software. 替代方式: https ://technet.microsoft.com/en-ca/sysinternals/bb897553.aspx - PsExec - PsExec是一个轻量级的telnet替代品,允许您在其他系统上执行进程,并具有完整的交互性控制台应用程序,无需手动安装客户端软件。

You can use a remote delegated powershell session for this. 您可以使用远程委派的PowerShell会话。

Use Delegated Administration and Proxy Functions 使用委派管理和代理功能

You can delegate the admin credentials in RunAs setting of the session configuration, and constrain the session to only being able to run the openfiles.exe. 您可以在会话配置的RunAs设置中委派管理员凭据,并将会话限制为只能运行openfiles.exe。 Then you can assign permission to use to session to selected groups or users. 然后,您可以将权限分配给选定组或用户的会话。 This enables you to let people run cmdlets or programs that require domain admin authority, without giving them the domain admin credentials. 这使您可以让人们运行需要域管理员权限的cmdlet或程序,而无需为其提供域管理员凭据。

No need to create a CSV file and have to remove it later, or be concerned about its' size. 无需创建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

If you're using powershell 1.0 then add |select -skip 8| 如果你使用的是PowerShell 1.0,那么添加| select -skip 8 | before the convertfrom-csv portion. 在convertfrom-csv部分之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM