Set-OpenFiles.ps1
function Set-OpenFiles { <# .SYNOPSIS Close files on a remote computer by ID number. .DESCRIPTION Use Get-OpenFiles to pipe IDs to Set-OpenFiles and close them. .PARAMETER ComputerName Remote computer hostname that you want to close files. .PARAMETER IDs List of IDs that can only come from the pipeline. .EXAMPLE Close files on a remote computer named 'server1'. PS C:\> Get-OpenFiles server1 "%.pdf" | Set-OpenFiles server1 .NOTES Author: Yannick Gagne .LINK http://msdn.microsoft.com/en-us/library/ #> [cmdletbinding()] Param ( [parameter(ValueFromPipeline=$False)] [string]$ComputerName, # 1st param is remote computer you want to close open files from [parameter(ValueFromPipeline=$True)] [array[]]$IDs # IDs of files to close (COMES FROM PIPELINE) ) Begin { } Process { # loop through all IDs that we got from the pipeline $IDs | ForEach-Object { # file ID $fileId = $_.Row[2] # file opened by $fileUser = $_.Row[1] # file full path $filePath = $_.Row[0] # write information to console Write-Host "Closing file $($filePath) with ID #$($fileId), opened by $($fileUser)." # close file by ID using openfiles.exe $cmdreturn = openfiles.exe /disconnect /S $ComputerName /ID $fileId # write return value from openfiles.exe to console Write-Host $cmdreturn } } End { } } |