簡體   English   中英

如何通過PS1腳本在Powershell控制台上執行程序

[英]How to execute a program at the powershell console from a ps1 script

我是python和powershell的新手。 為了使測試盡可能自動化,當我對特定的.py文件進行更改時,我試圖觸發鼻子測試可執行文件在控制台上運行。

到目前為止,我在文件filemonitor.ps1中具有以下代碼。 當我在控制台上運行此命令並更改文件lexicon.py時,在控制台中回顯“ Yippee”。 好的開始。 但是,我嘗試了各種命令來調用action塊中的鼻子測試腳本。 我的研究表明,類似這樣的方法應該起作用-> Invoke-Command -ScriptBlock {&$ program}
但是,控制台沒有任何反應。 我認為這可能與鼻子測試需要從項目文件夾(即本示例中的ex48)運行有關。 但我不確定。

感謝任何指導。

filemonitor.ps1

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\envs\projects\ex48\ex48"
$watcher.Filter = "lexicon.py"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
$program = "C:\envs\acme\Scripts\nosetests.exe"

$changed = Register-ObjectEvent $watcher "Changed" -Action {
    Write-Host "Yippee"
}

首先,為了安全起見,在Powershell中使用該腳本之前沒有運行腳本

Set-ExecutionPolicy bypass

第二個運行程序,您可以使用

Start-Process C:\Windows\System32\PING.EXE

或喜歡

.\winrar.exe

第三,如果要在run.exebatch file運行powershell腳本, run.exe可以使用此語法

powershell -noexit "& ""c:\test-webssl.ps1"""

好的,您的腳本System.IO.FileSystemWatcher只會看到您的路徑,如果您創建或更改或刪除了它,則通知您

$watcher = New-Object System.IO.FileSystemWatcher
 $watcher.Path = "S:\soheil"
 $watcher.IncludeSubdirectories = $false
 $watcher.EnableRaisingEvents = $true
 $changed = Register-ObjectEvent $watcher "Created" -Action { Write-Host 

"Created: $($event.Args.Fullpath)"}

好的,如果您運行帶有add-content txt文件或使用mkdir生成運行powershell的目錄,並通知類似Created add-content則可以使用另一個powershell,或者在腳本中可以結束腳本調用$changed

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM