Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

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

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
Senior IT System Engineer
Asked:
Senior IT System Engineer
  • 3
  • 2
1 Solution
 
SubsunCommented:
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
 
Senior IT System EngineerIT ProfessionalAuthor Commented:
Subsun,

Thanks for the reply.

So what can be used for 2008 R2 file servers ?
0
 
SubsunCommented:
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
 
Senior IT System EngineerIT ProfessionalAuthor Commented:
You are awesomely amazing Subsun !

It works and I've never thought that is possible with Powershell scripting in one go.
0
 
Senior IT System EngineerIT ProfessionalAuthor Commented:
Yes, it works exceeding my expectations :-)
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now