fileunlock_tool

The purpose of this script is to unlock a file located on a shared folder. The server which hosting the shared folder is configured in the variable $nas_server
This script works on Windows 2008/Windows 7 and uses the tool “openfiles.exe” (http://technet.microsoft.com/en-us/library/bb490961.aspx)
The script users will need, at least, to be a member of the “Power users” group on the nas server

To avoid any problems regarding the length of the paths, please check and set your powershell console screen width to “500” :

fileunlock_prop

Script :

clear-host
# Default variables and initialization
$nas_server = "nas-server01"
$temp_folder = "$Env:temp"
$temp_file = $temp_folder+"\unlock.csv"
$temp_file_format = $temp_folder+"\unlock_format.csv"

if (Test-Path $temp_file) {
	Remove-Item -Force $temp_file
}
if (Test-Path $temp_file_format) {
	Remove-Item -Force $temp_file_format
}

# Load the Winforms assembly
[reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")

# Create the form
$form = New-Object Windows.Forms.Form

# Create the tabcontrol
$tabcontrol = New-Object windows.Forms.TabControl
$tabpage_default = New-Object windows.Forms.TabPage
$tabpage_default.Text = "File unlock tool"
$tabcontrol.width = 1800
$tabcontrol.Height = 540

#Set the dialog title
$form.text = "File unlock tool"
$form.Width = 1800
$form.Height = 600
$form.FormBorderStyle = "FixedSingle"
$form.ControlBox = $false

# Create the label control and set text, size and location
$label_filename = New-Object Windows.Forms.Label
$label_filename.Location = New-Object Drawing.Point 10,50
$label_filename.Size = New-Object Drawing.Point 80,15
$label_filename.text = "File to unlock"

# Create TextBox and set text, size and location
$textfield_filename = New-Object Windows.Forms.TextBox
$textfield_filename.Location = New-Object Drawing.Point 90,45
$textfield_filename.Size = New-Object Drawing.Point 160,15

$gridview = New-Object Windows.Forms.DataGridview
$gridview.DataBindings.DefaultDataSourceUpdateMode = 0
$gridview.Name = "grouplist"
$gridview.DataMember = ""
$gridview.TabIndex = 1
$gridview.Location = New-Object Drawing.Point 300,40
$gridview.Size = New-Object Drawing.Point 1475,440
$gridview.readonly = $true
$gridview.AutoSizeColumnsMode = 'AllCells'
$gridview.SelectionMode = 'FullRowSelect'
$gridview.MultiSelect = $false
$gridview.RowHeadersVisible = $false
$gridview.allowusertoordercolumns = $true

# Create Button and set text and location
$button = New-Object Windows.Forms.Button
$button.text = "Unlock"
$button.Location = New-Object Drawing.Point 100,230

$button_exit = New-Object Windows.Forms.Button
$button_exit.text = "Exit"
$button_exit.Location = New-Object Drawing.Point 15,545

$button_searchfile = New-Object Windows.Forms.Button
$button_searchfile.Location = New-Object Drawing.Point 255,43
$button_searchfile.Size = New-Object Drawing.Point 35,23
$button_searchfile.text = " > "

# Set up event handler to extarct text from TextBox and display it on the Label.
$button.add_click({
if ($gridview.Rows.Count -ge 1) {
$fileid = $gridview.get_Item(0,$gridview.CurrentRow.Index).Value
$filename = $gridview.get_Item(3,$gridview.CurrentRow.Index).Value
$lockedby = $gridview.get_Item(1,$gridview.CurrentRow.Index).Value

openfiles /s $nas_server /disconnect /id $fileid

$message_success = "The following file has been unlocked : "+ `
"`nFilename : "+ $filename+ `
"`nLocked by : "+ $lockedby

[windows.forms.messagebox]::show($message_success,'Information','ok',[Windows.Forms.MessageBoxIcon]::Information)
}
else {
[windows.forms.messagebox]::show('Please search and select a filename to unlock
before submitting your request','Warning','ok',[Windows.Forms.MessageBoxIcon]::Warning)
}

})

$button_searchfile.add_click({
$grid = $gridview
write-host $textfield_filename.Text.ToString()
unlock_file($textfield_filename.Text.ToString())
})

$textfield_filename.add_KeyPress({
If ($_.KeyChar -eq 13) {
$grid = $gridview
write-host $textfield_filename.Text.ToString()
unlock_file($textfield_filename.Text.ToString())
}
})

$button_exit.add_click({
$form.Close()
})

# Add the controls to the Form
$form.Controls.Add($tabcontrol)
$tabcontrol.tabpages.add($tabpage_default)
$tabpage_default.controls.add($label_filename)
$tabpage_default.controls.add($textfield_filename)
$tabpage_default.controls.add($button_searchfile)
$tabpage_default.controls.add($button)
$tabpage_default.controls.add($gridview)
$form.controls.add($button_exit)

$form.add_Load($OnLoadForm_UpdateGrid)

function unlock_file([string]$filename_to_unlock){
	if ($filename_to_unlock) {
		'"Server","FileID","Username","OS","ID","RW","Path"' | out-file $temp_file
		openfiles /query /s $nas_server /v /fo csv | select-string $filename_to_unlockĀ  | out-file $temp_file -Append
		gc $temp_file | where {$_ -ne ""} > $temp_file_format

		$array = New-Object System.Collections.ArrayList
		$data=@(Import-Csv $temp_file_format | select FileID,Username,RW,Path)
		$array.AddRange($data)
		$grid.DataSource = $array

		$form.refresh()

		if ((gc $temp_file_format | Measure-Object -Line).Lines -le "1") {
		[windows.forms.messagebox]::show('Cannot find a filename which contains "'+$filename_to_unlock+'"','Warning','OK',[Windows.Forms.MessageBoxIcon]::Warning)
		}
	}
	else {
		[windows.forms.messagebox]::show('Please enter a file name to unlock','Warning','OK',[Windows.Forms.MessageBoxIcon]::Warning)
	}
}

# Display the dialog
$form.ShowDialog()
Network file unlocker

Leave a Reply

Your email address will not be published. Required fields are marked *