1

I have a measuring device on a PC in our network. The PC is not joined to the domain, however has a shared folder for which I have a username and password.

I am attempting to build a Powershell script to monitor this folder from a remote server, for new CSV files, the script will then copy the file to a specified folder location. I am struggling to pass through the parameters to the FileSystemWatcher cmdlet.

Any ideas?

$folder = '\\remoteip\folder\subfolder'# <--'<full path to the folder to watch>'
$filter = '*.csv'
$destination = '\\mynetworkstorage\folder\' # <--' Where is the file going?

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubdirectories = $true              # <-- set this according to your requirements
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
} 
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $path = $Event.SourceEventArgs.FullPath
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' was $changeType at $timeStamp"
    Copy-Item $path -Destination $destination -Force -Verbose
}

EDIT - The script will be run from a server joined to our domain, so there is a need to pass through credentials to the folder in order that I can access it. I have these credentials.

AdzzzUK
  • 288
  • 3
  • 12

1 Answers1

1

I refactored the code a little (mainly so I could more easily understand it):

$folder = '\\localhost\c$\tmp'
$filter = '*.*'
$destination = '\\localhost\c$\tmp_destination\' 

$fsw = New-Object IO.FileSystemWatcher
$fsw.Path = $folder
$fsw.Filter = $filter
$fsw.IncludeSubdirectories = $true
$fsw.EnableRaisingEvents = $true  
$fsw.NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'

$action = { 
    $path = $Event.SourceEventArgs.FullPath
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' was $changeType at $timeStamp"
    Copy-Item $path -Destination $destination -Force -Verbose    
}  

$created = Register-ObjectEvent $fsw Created -Action $action

while ($true) {sleep 1}

I ran this code locally and managed to get files created in $folder automatically copied to $destination.

FileSystemWatcher run in the context of the current user. In the case of accessing remote systems that require login, impersonation is required.

If the remote system only got local users, no domain user that the source system can log on as, then impersonation does not seem to be possible.

See this and this link for more details on impersonation.

kim
  • 3,385
  • 2
  • 15
  • 21
  • Thanks Kim. No problem with the refactoring, certainly looks cleaner. However, my key requirement is connecting to this remote folder using different credentials. The folder is on a remote PC which is not joined to the domain (but for which we have remote access to a shared folder using different credentials). – AdzzzUK Mar 23 '18 at 11:45
  • Now I understood the problem. I didn't find any easy way to do that. But have a look at https://stackoverflow.com/questions/7118961/filesystemwatcher-on-a-network-machine-setting-credential and https://gallery.technet.microsoft.com/scriptcenter/Impersonate-a-User-9bfeff82 as a reference implementation. In short, it seems FileSystemWatcher does not relate to any user, it runs in the context of the current user. So if it needs to run under the context of some other user, then that other user need to be impersonated. – kim Mar 23 '18 at 11:58
  • However, the above links assume you can log on as that user. If the user only exist on a remote system, as a local user, then this approach doesn't seem to work. – kim Mar 23 '18 at 12:01