Search code examples
powershellpowershell-4.0powershell-5.0

Do Until with Get-SmbOpenFile


I'm trying to check if any files are opened on a server with powershell. I've got the below script which kind of works

If I start it when there are no files opened it checks first, waits 10 seconds and then prints the message "No files are opened". If I open any files and start the script it says "Files are opened, please wait..." but when I close all files and disconnect all sessions it still says the same

Clear-Host
$CheckOpenfiles = Get-SmbOpenFile
Do
    {
     "$(get-date) Files are opened, please wait..."
     Start-Sleep 10
     } Until (!$CheckOpenfiles) 
     "No files are opened"

Solution

  • As LotPings notes in the comments, you assign your value to $CheckOpenfiles before you start looping. This means that it is not reevaluated in your Until conditional.

    Do {
        "$(get-date) Files are opened, please wait..."
        Start-Sleep 10
    } Until (!(Get-SmbOpenFile)) 
    "No files are opened"