0

当我们将更改推送到文件共享时,有时用户会让应用程序保持打开状态,这反过来又使文件从共享中保持打开状态——它被锁定,我们无法替换它。我想找到与此类文件对应的共享会话并在覆盖文件之前在 PowerShell 脚本中终止它们(这是只读的东西,主要是 .DLL 文件)。

这与找到在共享文件夹 MMC 中打开的文件,然后关闭那里的相应会话相同,但我需要以编程方式远程执行多台服务器。

4

2 回答 2

1

除了新的 Powershell 4.0/Win2012 cmdlet,我找不到任何东西,所以我编写了自己的脚本,我认为值得分享:

# Use "net files" to get the ID's of all files and directories on this computer 
# that are being accessed from a network share.
$openFiles = net files | 
    where { $_ -match "^(?<id>\d+)" } | 
    foreach {
        # Use "net files <id>" to list all information about this share access as a key/value list, and 
        # create a new PSObject with all this information in its properties.
        $result = new-object PSObject
        net files $matches["id"] | 
            where { $_ -match "^(?<key>.*[^\s])\s{2,}(?<value>.+)$" } | 
            foreach { 
                Add-Member -InputObject $result -MemberType NoteProperty -Name $matches["key"] -Value $matches["value"] 
            }
        return $result
    }

# Now that we know everything that's being accessed remotely, close all that 
# apply to our application folder.
$openFiles | 
    where { $_.Path -like "C:\MySharedFolder\MyApp*" } | 
    foreach { net files ($_."File Id") /close }
于 2014-12-09T17:19:41.383 回答
0

网络文件 /close有什么问题?

于 2012-05-04T06:28:59.407 回答