How to continuously run the custom activity until a file is deleted from file server

John 190 Reputation points
2023-08-24T05:33:56.2566667+00:00

I have a requirement where I need to continuously check for a file and restart other pipelines only if that file is deleted.

I have a working powershell script which is working fine in windows powershell ISE.

Script:-

Set-ExecutionPolicy RemoteSigned -Scope Process -Force

$FTPHost = 'file.server.test'
$FTPUser = 'user'
$FTPPass = 'password'
 
$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($FTPUser,$FTPPass) 

While ((Test-Path '\\file.server.test\file\data\test.txt' -ErrorAction SilentlyContinue))
{
  # endless loop, when the file will be there, it will continue
}

I have kept it at blob and running it by powershell.exe script.ps1

But it is not waiting for the file to be deleted, it just completes without throwing any error.

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,624 questions
Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2023-08-24T14:21:52.9333333+00:00

    it just completes without throwing any error.

    That's because "-ErrorAction SilentlyContinue" tells Powershell to ignore all errors on that cmdlet and keep running.

    which is working fine in windows powershell ISE.

    ISE is running in the context of your desktop session and probably on your local network. If you are running the script in Azure or some other non-interactive environment, you may have firewalls blocking network access. Use Test-NetConnection to verify network connectivity. I would also expect that you need to authenticate to the remote system. You created a WebClient object, with credentials, but you don't use it. What is the point of doing that?

    Use Get-ChildItem to get a directory list of the folder and verify that your script can see the remote file system. If your environment doesn't capture the output of the script use Start-Transcript and have Powershell log activity to a file.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-08-24T18:59:07.6366667+00:00

    The ISE isn't the best place to test a script. It's okay for composing and debugging, but you have to keep in mind that the ISE "remembers" variable and their values from previous executions of the script. It also uses your credentials when accessing things such as file shares. It's a good idea to start a new PowerShell session to run the script each time when you want to verify that it's able to run by itself in a clean session.

    You probably don't have permission to access either the file share or its subdirectory. Without permission, the Test-Path will fail and with it the While loop (which will receive a $null value), causing the script to end.

    Try using this sort of code:

    Try{
        # just see if you have access to the directory
        if (-NOT (Test-Path \\file.server.test\file\data -PathType Container -ErrorAction STOP)){
            Throw "No error was detected, but you do not have access to the directory or the directory does not exist"
        }
    }
    Catch{
        # don't use Write-Host here because you may be running in an environment (e.g. Scheduled Task) that has no user profile
        "Unable to access file share or a sub directory" | Out-File <some log file name here> -Append
        $_ | Out-File <some log file name here>  -Append
        Exit    # or try to recover
    }
    Try{
        if (Test-Path '\\file.server.test\file\data\test.txt' -ErrorAction STOP){     # verify existence of the file
            While ((Test-Path '\\file.server.test\file\data\test.txt' -ErrorAction SilentlyContinue))
            {
                # endless loop, when the file will be there, it will continue
                Start-Sleep -Milliseconds 500   # avoid large CPU consumption and network traffic -- adjust time as needed
            }
            "The file has been removed from the directory after being detected at least once. " | Out-File <some log file name here> -Append
        }
        else {
            "No error was detected, but the file was not found in the directory" | Out-File <some log file name here> -Append
        }
    }
    Catch{
        "Unable to access the file" | Out-File <some log file name here> -Append
        $_ | Out-File <some log file name here>  -Append
        Exit    # or try to recover
    }
    

    EDIT: added additional checking and stopped True/False emitted in success stream.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.