Microsoft Virtualization Discussions

Find CIFS session of named open file

wippel
31,319 Views

Please tell me, how to find the CIFS session of an open file and how to terminate it.

7 REPLIES 7

cknight
31,319 Views

I don't see any Data ONTAP APIs that do what you seek.  Get-NaCifsSession can tell you about users and volumes being accessed, but not files.  And I don't see any API to terminate an individual session.  You might experiment with manipulating the CIFS share ACLs, but otherwise you would have to call Disable-NaCifs, which is admittedly rather drastic.

wippel
31,319 Views

ONTAP command

>cifs sessions *

shows all open files.

ONTAP command

>cifs terminate [workstation]

allows to terminate a single session.

mh_sh_team
31,319 Views

You can do it from the Windows' "Computer Management" interface. Open it (compmgmt.msc) and connect to your filer/vfiler (by right-clicking the "Computer Management (Local)" in the first line of the left half of the screen -> connect to another computer). Down there you can find "Shared Folders" -> "Open Files". You can see there all the open files and sessions to your filer/vfiler and by right-clicking the specific open file you want to terminate, a terminate option will appear (don't remember the exact option, but it's there).

Hope it's helpful and what you asked for.

fjohn
31,319 Views

Sure enough, that works.  Also means that it should be possible via WMI, which by extension means PowerShell. 

J

wippel
31,319 Views

I had no luck finding a way to do close a file in .NET, WMI, ADSI or COM.

Do you know a way?

Mike_Pedigo
24,232 Views

I found this thread and found it helpful for 7-mode and wanted to update for cDOT:

 

You can use a combination of the following 2 commands to get the same information in cDOT at the CLI:

cifs session show -instance  (user info)

cifs session file show -instance   (open file info)

 

Use the connection ID field to link between the two outputs to establish the files a particular user has open.

 

To close a session or file you could then use:

 

cifs session close

cifs session file close

 

At least at 8.3+ you can also use the MMC to gather this information and close files/sessions if you have the appropriate permissions.

 

I am unsure about other methods like Powershell.

FelipeMafra
23,571 Views

Hello,

 

I know it is a little late to send you this message but maybe someone else needs it.

 

I have a script that copy folders from a source UNC path to a destination and closes open sessions of exe files. This scripts if for 7-Mode filers but you can change it for your needs.

 

<#    
    .SYNOPSIS
        This is a function to copy system files from a source folder to a target folder.

    .DESCRIPTION
        It is used to copy files from a source folder to a target folder. If any .EXE file is in use it closes the session before copy operation.

    .PARAMETER Source
        Is an UNC source path for system's files (\\filer\share\folder\sub-subfolder\...).

    .PARAMETER Target
        Is an UNC target path for system's files (\\filer\share\folder\sub-subfolder\...).

    .EXAMPLE
        Copy-SystemFiles.ps1 -Source \\filer1\share1\folder\sub-folder\third-level-folder \\filer2\share2\folder
        Will copy every file in the souce folder to target folder closing every executable .EXE files before copy operation.

#>

Param(
    [Parameter(Mandatory=$true)]
    [string]$Source,

    [Parameter(Mandatory=$true)]
    [string]$Target
)

function Get-InternalPath {
    Param(
        [Parameter(Mandatory = $true)]
        [string]$Path
    )

    $PathArray = $Path.Split('\')
    $Filer = $PathArray[2]

    Connect-NaController -Name $Filer |Out-Null

    $MountPoint = (Get-NaCifsShare -Share $PathArray[3]).MountPoint
    $FullPath = "C:$MountPoint"
    
    for ($i = 4; $i -lt $PathArray.Length; $i++) {
        $FullPath += "\$($PathArray[$i])"
    }
    return $($FullPath.Replace('/','\'))
}

try{
    Get-ChildItem -Path $Source|ForEach-Object {
        $FileName=$_.FullName
        
        if($FileName.EndsWith(".exe")) {
            $TargetFiler = $Target.Split("\")[2]

            cmd /c "openfiles /Disconnect /S $TargetFiler /A * /OP $(Get-InternalPath -Path $Target)" |Out-Null
        }

        Copy-Item -Recurse -Force -Path $FileName -Destination $Target -Verbose
    }
}
catch{
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Write-Output $FailedItem
        Write-Output $ErrorMessage
}

Public