当我们将更改推送到文件共享时,用户偶尔会打开一个应用程序,从而保持文件从共享中打开 - 它已被锁定,我们无法替换它。我想在覆盖文件之前找到与这些文件相对应的共享会话并在PowerShell脚本中将它们删除(这是只读的东西,主要是.DLL文件。)
这与查找共享文件夹MMC中打开的文件,然后在那里关闭相应的会话相同,但我需要以编程方式和远程方式为多个服务器执行此操作。
答案 0 :(得分:0)
net file /close出了什么问题?
答案 1 :(得分:0)
除了新的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 }