使用FileSystemWatcher的PowerShell脚本不会启动Python脚本

时间:2017-09-22 16:02:31

标签: python powershell filesystemwatcher

我认为这个问题与作为系统帐户运行的FileSystemWatcher有关,即使只是右键单击并选择“使用Powershell运行”即可启动.ps1脚本。

我尝试了几种方法,将命令集成到主.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
}

1 个答案:

答案 0 :(得分:0)

好的,我明白了。 为了使它工作,我不得不使用Start-Process来启动另一个.ps1脚本。

至于启动.py脚本,我将目录更改为脚本所在的位置并从那里执行,如下所示。

<div ng-app="myApp" ng-controller="fooController as fc">
  <input class="form-control input-sm valign" type="search" placeholder="Search" ng-model="fc.search" ng-change="fc.highlightActiveSearch()" ng-class="fc.styleSearch" />
</div>