Monthly Archives: June 2015

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….

Get list of all the junctions present on a disk volume

list of all the junctions present on a disk volume can be obtained by executing dir /aL /s C:\, where "C:" is the volume to scan

to create a junctions use mklink command as described in my other post Hardlinks, Junctions, Symlinks to files or folders

If you prefer Powershell (like me) then notice that since version 5 the commandlets New-Item, Remove-Item, Get-ChildItem support symbolic links

smsagent

Scripts, tools and tips, mostly around Microsoft SCCM and EMS

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?