1

I created a PowerShell script to monitor a folder for new files; it deletes files containing "cmr" and logs the names of the files that contains "cdr".

This all worked yesterday and today I decided to reboot and see if the event will stay but I can't even get it to work at all, I am not sure what happened.

$folder = "C:\Users\home\Documents\calldata"
$filter = '*.*'
Set-Location $folder
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}

Register-ObjectEvent $fsw Created -SourceIdentifier NewCallData -Action{
    $name = $Event.SourceEventArgs.Name
    if($name -match "cmr"){
        Write-Host $folder\$name
        Remove-Item $folder\$name
    }
    if($name -match "cdr"){
        Out-File -FilePath C:\MCallPowershell\outlog.txt -Append -InputObject "$name"
    }
}

1 Answer 1

2

You might need to un-register your event to run a new instance:

Unregister-Event NewCallData

then run it again

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.