How Can I List Open Sessions and Open Files on a Computer?

I need to update frequently a piece of software on a set of servers. The software is updated just by copying new files into a directory. A piece of cake using Powershell.

The problem is that the directory is shared and users run the software from there – so when I try to copy new version of files over the old one, I’m told that some files are open and cannot be replaced. I need a way to find those open files and close them. I know it can be achieved using  Share and Storage Management console – but thats not scriptable.

I searched and found this Ed Wilsons’s post

How Can I List Open Sessions and Open Files on a Computer? – Hey, Scripting Guy! Blog – Site Home – TechNet Blogs.

and this script Jeffrey S. Patton’s mod-posh tools

I took the best bits off them and function looks as this

Function Get-OpenFiles
{
<#
.SYNOPSIS
    Get a list of files open on the server
.DESCRIPTION
    This function returns a list of files open on a given server. The output is
    similar to that of the Manage Open Files from the Share and Storage Management
    console.
.PARAMETER ComputerName
    The NetBIOS or FQDN of the computer
.EXAMPLE
    Get-OpenFiles -ComputerName fs

    User          Path                              LockCount
    ----          ----                              ---------
    User1         F:\Users\User1\Documents\Data\...         0
    User2         P:\Public                                 0
            
#>
[CmdletBinding()]

    
    Param (
        [Parameter(Mandatory=$false,
                ValueFromPipeline=$true,
                ValueFromPipelineByPropertyName=$true,
                Position=0)]
        [Alias("DnsHostName")] 
        [string[]]$ComputerName = $env:COMPUTERNAME
    )


    Begin
    {
        
    }

    Process
    {
        
        foreach ($c in $ComputerName) {
            $Server = [adsi]"WinNT://$($c)/LanmanServer"
            $Resources = $Server.PSBase.Invoke("Resources")
            
            foreach ($Resource in $Resources)
            {
                Try
                {
                    
                    $prop = @{User = $Resource.GetType().InvokeMember("User","GetProperty",$null,$Resource,$null)
                                Path = $Resource.GetType().InvokeMember("Path","GetProperty",$null,$Resource,$null)
                                LockCount = $Resource.GetType().InvokeMember("LockCount","GetProperty",$null,$Resource,$null)
                                Computername = $c
                            }

                    $UserResource = New-Object -TypeName PSobject -Property @prop
                }
                Catch
                {
                    #catch exception
                }

                #return to output
                $UserResource
            }
        }
    }
    
    End
    {
        
    }
} # end of function

I will test it and then add the possibility to close those open files….

Advertisements

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

smsagent

Tips, tricks and time-savers for the Windows and ConfigMgr administrator

To The Point

Anything about Technology and Business

Brian's Power Windows Blog

Microsoft in the Enterprise. Windows, Hyper-V, Exchange, SQL, and more!

PowerScripting Podcast

Shownotes and links for the PowerScripting Podcast, a podcast to help people learn Windows Powershell

Learn Powershell | Achieve More

What is this Powershell of which you speak?

%d bloggers like this: