header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Closing Open Files
Last Post 14 May 2008 12:58 PM by halr9000. 21 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
dshirshUser is Offline
New Member
New Member
Posts:7
Avatar

--
03 Jan 2008 08:15 PM  

Trying to develop a script to go out and close all open files using the filename(or portion thereof) as the search parameter. I had thought that get-process would provide the needed data but I can't seem to get the objects necessary to isolate the filenames. I suspect it's the handles that need walked to find actual file system connections.  Am I getting warm here ??? TIA

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
03 Jan 2008 08:18 PM  
You could use handle.exe to find the open files, but I am not sure what you mean by close. Can you clarify?
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
dshirshUser is Offline
New Member
New Member
Posts:7
Avatar

--
03 Jan 2008 08:25 PM  

I'm trying to pull this off without using an external executable. When I say close file I mean to break the link between the user who has the file open and the file itself.  Basically, I'm trying to accomplish the same thing as if I went into the Computer Management MMC and then into Open Files. 

EBGreenUser is Offline
New Member
New Member
Posts:63
Avatar

--
03 Jan 2008 08:34 PM  
I would have to think that there would be significant risk for data loss in this.
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
03 Jan 2008 08:47 PM  
You realize that the files you see Computer Management are remotely open files right? Is that what your looking for?
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
dshirshUser is Offline
New Member
New Member
Posts:7
Avatar

--
03 Jan 2008 08:50 PM  

Remote files are correct. The nature of the application is such that closing file connections is not an issue. The files are being accessed from a file server so all are remote access.

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
03 Jan 2008 09:04 PM  
Maybe I am wrong and hopefully someone will correct me, but I don't think easily do what your wanting. I think handle.exe is your best bet.

Why do you want to close these connections?
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
dshirshUser is Offline
New Member
New Member
Posts:7
Avatar

--
03 Jan 2008 09:08 PM  

We have a legacy database application and when we go to run the daily index rebuilds and backup it will fail if there are any open files. We currrently have to manually go in and close the open files that the application uses. After that is done the rebuilds and backup run fine. It's basically a case of a few users who forget to log out and leave their connections active.

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
03 Jan 2008 09:14 PM  
I will give people more time to respond to you. If you dont get a better response I will help you use handle.exe
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
04 Jan 2008 01:21 PM  
This is an interesting one. I looked to see what vbscript resources were out there and came acorss this (don't get excited, it doesn't have the solution): http://www.scriptinganswers.com/Archive/articles/CloseopenAccessDB.htm. Further searching shows that this question has been asked time and time again but I don't see any answers. It can be done using 'net file', but I can't seem to find the mechanism that is used so that we can go after that using .NET.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
dshirshUser is Offline
New Member
New Member
Posts:7
Avatar

--
07 Jan 2008 06:33 PM  
Caught a blurb where someone in VB was utilizing WMI and using resource.user and resource.path to grab this info to display. Can WMI be used to close the open file links as well?
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
07 Jan 2008 06:39 PM  
What WMI class?
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
dshirshUser is Offline
New Member
New Member
Posts:7
Avatar

--
07 Jan 2008 06:50 PM  
Here's a URL to a VB script that allegedly does this. Some users say it doesn't.

http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0216.mspx

Outdated object reference maybe??
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
07 Jan 2008 06:54 PM  
Not outdated, but not WMI either... that references a COM interface. The problem is that you can actuall access that in Powershell but it does not act the same. I am posting an question about this in another group that includes MS dev.
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
dshirshUser is Offline
New Member
New Member
Posts:7
Avatar

--
18 Jan 2008 07:47 PM  
Any luck from the developers side on this issue?
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
18 Jan 2008 07:53 PM  
No Luck. I got a response, but it wasnt the one you were hoping for I am sure.

I would say you have three options
1) Use a VBScript
2) Use VBScript in Powershell
3) I would guess you could use IronPython in Powershell as well.
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
mjolnirUser is Offline
New Member
New Member
Posts:12
Avatar

--
23 Jan 2008 01:17 AM  

As an alternative to closing the files, how about logging off the users?  If you can find the users that are logged onto the server, you can set the "Enforce logoff when allowed logon hours expire" on the domain, and then script setting the allowed logon hours for those users that are still connected.  Once they've been logged off, the files should be closed, and you can re-set the allowed logon hours back to their normal settings, and run your backups.

rbuhrtUser is Offline
New Member
New Member
Posts:2
Avatar

--
07 Feb 2008 04:37 PM  
Dim ServerName
Dim FileName

ServerName = "servername" 'example. fileserver.microsoft.local or fileserver
FileName = "local file path" 'example. D:\Group\Users.xls

Set objConnection = GetObject("WinNT://" & ServerName & "/LanmanServer")
Set colSessions = objConnection.Sessions
Set colResources = objConnection.Resources
Set Shell = CreateObject("WScript.Shell")

On Error Resume Next

For Each objResource in colResources
If objResource.Path = FileName Then
Shell.Run "net files "& objResource.Name & " /close",1,True
End If
Next
rbuhrtUser is Offline
New Member
New Member
Posts:2
Avatar

--
07 Feb 2008 04:41 PM  

The above script I posted will work on Windows 2000,XP,2003.
Define your variables and save it as a vbscript (.vbs)

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
07 Feb 2008 09:50 PM  
Yeah, net files will work, but still no luck finding a way to do it using .NET, WMI, ADSI or COM. :(
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
DyJohnnYUser is Offline
New Member
New Member
Posts:1
Avatar

--
14 May 2008 10:21 AM  

Hi all,

 

Not an expert at this, but why not use psfile utility from Sysinternals to accomplish this?

I've been using this successfully in the past, in our environment. it only works for remote files, just FYI  

 

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
14 May 2008 12:58 PM  
Well, this being a powershell forum, we were trying to find the "most native" way to solve the problem. So that means roughly in this order: cmdlet, .NET object, WMI, ADSI, COM, console executable (i.e. psfile). But definitely use what works. It just turns out that for some weird reason there's never been an integrated way to do this. On the other hand, there are much worse fates than being forced to use sysinternals utilities. :)
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer