2017-09-22 98 views
0

我相信這個問題與作爲系統帳戶運行的FileSystemWatcher相關,即使通過右鍵單擊並選擇「Run with Powershell」啓動.ps1腳本。使用FileSystemWatcher的PowerShell腳本不會啓動Python腳本

我嘗試了幾種方法,將命令集成到主.ps1腳本中,並在ACTION事件期間啓動另一個.ps1腳本。

無論是執行後端功能還是移動\命名\刪除和檢索文件內容,但執行.py腳本時,它都不會在當前會話中啓動。

有沒有人知道有一種方法可以讓該窗口在前臺啓動? Python代碼運行插件Selenium,需要打開Chrome來完成它的工作。

一如既往的謝謝大家!

下面的代碼:

$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 
} 

回答

0

好了,所以我想它了。 爲了讓它工作,我不得不使用啓動過程啓動其他.ps1腳本。

至於讓.py腳本啓動,我將目錄更改爲腳本所在的位置,並從那裏執行它,如下所示。

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