Solved

Need some help in modifying Powershell script to show open files in File server

Posted on 2016-09-05
5
108 Views
Last Modified: 2016-09-07
People,

I have several File servers running Windows Server 2008 R2 and 2012 R2 accessed by multiple terminal Servers (RDSH).

Can anyone here please assist me in how to modify the Powershell script so that I can find who is using or opening certain files ?

$Computername = read-host "Enter computername"
invoke-command -computername $Computername -Scriptblock {get-SMBOpenFiles} | Out-gridview

Open in new window


resolve-DNSName 10.188.8.12

Open in new window


The IP address is to convert the result from the OutGridview, so I do not know how to combine them together.

This is the error that I'm getting:

The term 'get-SMBOpenFiles' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (get-SMBOpenFiles:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : PRODFS-04-VM

Thanks in advance.
0
Comment
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 41786402
Get-SmbOpenFile work only on windows 2012 onwards.

The IP address is to convert the result from the OutGridview, so I do not know how to combine them together.

To combine the commands you may create a custom object.. Example..
$Computername = read-host "Enter computername"
Invoke-Command -ComputerName $Computername -ScriptBlock {Get-SmbOpenFile | Select ClientUserName,@{N="Source";E={(Resolve-DnsName $_.ClientComputerName).NameHost}}}

Open in new window

0
 
LVL 7

Author Comment

by:Senior IT System Engineer
ID: 41786962
Subsun,

Thanks for the reply.

So what can be used for 2008 R2 file servers ?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 41787980
Not really.. As per my knowledge there are commands like Openfiles and Net file to list open files, but it wont show you the source IP from where user is connected from.

I have a function which I use for this purpose, it may not be 100% accurate as I am combining the session and open file details by matching username. You can check and see if it works for you..

$Computername = read-host "Enter computername"
Function Get-OpenFiles {
Param ( 
  $ComputerName = (hostname) 
)  
$Server = [adsi]"WinNT://$($ComputerName)/LanmanServer"
$Resources = $Server.PSBase.Invoke("Resources") 
$Sessions = Get-WmiObject win32_serverconnection -computerName $ComputerName
$Resources | %{ 
	Try 
	{ 
	$user = $_.GetType().InvokeMember("User","GetProperty",$null,$_,$null)
	$Path = $_.GetType().InvokeMember("Path","GetProperty",$null,$_,$null)
	$Source = $Sessions | ?{$_.UserName -eq $user} | Select -exp ComputerName -Unique
	New-Object -TypeName PSobject -Property @{ 
		User = $user
		Path = $Path
		Source = $Source -join ","
		}
 }
 Catch {}
 }
}

Get-OpenFiles -ComputerName $Computername

Open in new window

0
 
LVL 7

Author Comment

by:Senior IT System Engineer
ID: 41788854
You are awesomely amazing Subsun !

It works and I've never thought that is possible with Powershell scripting in one go.
0
 
LVL 7

Author Closing Comment

by:Senior IT System Engineer
ID: 41788855
Yes, it works exceeding my expectations :-)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question