简体   繁体   中英

PowerShell script using FileSystemWatcher will not launch Python script

I believe this issue to be related to the FileSystemWatcher running as a system account even though the .ps1 script is being launched by simply right-clicking and selecting "Run with Powershell".

I've tried several methods, integrating the command into the main .ps1 script and by launching another .ps1 script during the ACTION event.

Both perform the back-end functions or moving \\ naming \\ deleting and retrieving file content but when it comes to executing the .py script it doesn't launch in the current session.

Does anyone know of a way to get that window to launch in the foreground? The Python code runs a plug-in Selenium and needs to open Chrome to do its thing.

As always thank you all!

Here's the code:

$folder = 'C:\scripts\tmp' # root path to monitor
$filter = '*.*'  # wildcard filter

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
Out-File -FilePath C:\Scripts\URLwatcher\logURL.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"

#Create a timestamp var
$filetimestamp = (Get-Date -Format yyyy-mm-dd-hhmmss)

#Create a timestamp file name
$timestampedURLfile = "C:\Scripts\URLwatcher\processing\" + $filetimestamp + ".txt"

#Move URL file and rename it
move-item C:\scripts\tmp\data.txt $timestampedURLfile

#Extract & store the URL from the file
$addURL = Get-Content $filename

#Scrape the url
python C:\Scripts\DailyStats.py -l $addURL  #### <--This script will not run ####

#Delete the URL file
del $filename
}

Ok so I figured it out. In order to get it working I had to use the Start-Process to launch the other .ps1 script.

As for getting the .py script to launch I changed the directory to where the script was located and executed it from there as shown below.

start-process powershell C:\Scripts\URLwatcher\ManualScrape.ps1
----------------------------------------------------------------

#Debugger \\ Set-PSDebug -Trace 1

#Create a timestamp var
$movetimestamp = (Get-Date -Format yyyy-mm-dd-hhmmss)

#Create a timestamp file name
$timestampedURLfile = "C:\Scripts\URLwatcher\processing\" + $movetimestamp + ".txt"

#Move URL file and rename it
move-item C:\Scripts\tmp\data.txt $timestampedURLfile

#Extract & store the URL from the file
$addURL = Get-Content $timestampedURLfile

#Run the scraper
cd C:\Scripts
python.exe DailyStats.py -l $addURL

#Delete the URL file
del $timestampedURLfile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM