2017-11-28 14 views
0

Powershell을 아는 사람에게는 간단한 질문 일 것입니다. 나는 이미 thesethese과 같은 비슷한 질문에 대한 몇 가지 답변의 제안을 구현하려고 시도했다. 폴더를 모니터링하고 변경 사항이 발생할 경우 로그 파일을 작성하는 파일 공유기가 있습니다. 이것은 잘 작동합니다.Powershellscript의 FileSystemWatcher를 사용하여 Python 파일을 실행하십시오.

내가 할 수 없었던 것은 추가 파이썬 스크립트를 실행하는 것입니다. 어떤 조언을 해주셔서 감사합니다.

$folder = 'F:\myPath\myFolder' # Enter the root path you want to monitor. 
$filter = '*.*' # You can enter a wildcard filter here. 

# In the following line, you can change 'IncludeSubdirectories to $true if required.       
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 

# Registered events: 
Register-ObjectEvent $fsw Renamed -SourceIdentifier FileRenamed -Action { 
$OldName = $Event.SourceEventArgs.OldName 
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The file '$name' was $changeType at $timeStamp" -fore white 
Out-File -FilePath F:\myPath\myLogFile.txt -Append -InputObject "The file $name was $changeType at $timeStamp" 
python F:\myPath\myPythonScript.py} 

Powershell ISE로 어떻게하면됩니까?

답변

0

Powershell 콘솔을 사용하면 ISE보다 훨씬 운이 좋습니다. 파이썬을 PATH에 추가했다면, 다음과 같이 간단합니다. python F:\myPath\myPythonScript.py

파이썬 스크립트가 값을 반환하면 Powershell로 다시 전달하기 위해 화면에 출력해야하며 다음과 같이 저장됩니다. 반환되는 순서로 배열.

+0

맞습니다 : 콘솔에'python F : \ myPath \ myPythonScript.py '명령 만 입력하면 파이썬 스크립트가 실행됩니다. 그러나 PowerShell 콘솔에서 전체 스크립트 (파일 시스템 워처 포함)를 실행할 때 감시 폴더에서 파일을 변경하면 Python 스크립트가 실행되지 않습니다. 그렇다면 감시 된 폴더에서 파일이 변경된 경우 Python 스크립트를 실행하려면 어떻게해야합니까? – Chris

+0

EventSubscriber는'-Action' 다음의 중괄호로 코드를 실행합니다. 거기에 코드가 올바르게 실행됩니까? 예를 들어 로그 파일에 기록합니까? –

+0

예, 코드가 올바르게 실행되고 로그 파일을 작성합니다. 그러나 콘솔에서 python 스크립트를 실행하지 않습니다. 내가 사용하고있는 질문과 코드를 편집했습니다. – Chris