Solved

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

Posted on 2016-09-05
5
182 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 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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 …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

623 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