?
Solved

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

Posted on 2016-09-05
5
Medium Priority
?
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 8

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 2000 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 8

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 8

Author Closing Comment

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

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
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 …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

719 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