Skip to content

Instantly share code, notes, and snippets.

@yaleman
Created February 12, 2016 02:27
Show Gist options
  • Save yaleman/94f5b5feb24f2a18ba95 to your computer and use it in GitHub Desktop.
Save yaleman/94f5b5feb24f2a18ba95 to your computer and use it in GitHub Desktop.
# Get-openfiles.ps1
# Connects to a remote server and queries users with open folders
# Created: 2016-02-12
# Created by: James Hodgkinson based on work from Jeff Patton
# TODO: Make this runnable from the command line
$hostname = CHANGETHIS
$pathtocheck = CHANGETHISTOO
# https://gallery.technet.microsoft.com/scriptcenter/ef8de213-45b6-4751-8c77-01d1b6623e16
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
Description
-----------
This example shows the basic usage of this command.
.NOTES
FunctionName : Get-OpenFiles
Created by : Jeff Patton
Date Coded : 09/26/2011 13:01:38
.LINK
https://code.google.com/p/mod-posh/wiki/ComputerManagement#Get-OpenFiles
#>
[CmdletBinding()]
Param
(
$ComputerName = (hostname)
)
Begin
{
$OpenFiles = @()
$Server = [adsi]"WinNT://$($ComputerName)/LanmanServer"
$Resources = $Server.PSBase.Invoke("Resources")
}
Process
{
foreach ($Resource in $Resources)
{
Try
{
$UserResource = New-Object -TypeName PSobject -Property @{
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)
}
}
Catch
{
}
$OpenFiles += $UserResource
}
}
End
{
Return $OpenFiles
}
}
get-openfiles -computername $hostname | foreach-object $_ {
if( $_.Path.StartsWith( $pathtocheck ) ) {
write-host $_.Path "is opened by" $_.User
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment