Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Resolve DNS query failed errors for Exchange
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now