简体   繁体   English

如何通过PS1脚本在Powershell控制台上执行程序

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

I am new to python and powershell. 我是python和powershell的新手。 To automate my testing as much as possible I am trying to trigger the nosetests executable to run at the console when I make a change to a specific .py file. 为了使测试尽可能自动化,当我对特定的.py文件进行更改时,我试图触发鼻子测试可执行文件在控制台上运行。

So far I have the code below in file filemonitor.ps1. 到目前为止,我在文件filemonitor.ps1中具有以下代码。 When I run this at the console and make a change to file lexicon.py "Yippee" is echoed in the console. 当我在控制台上运行此命令并更改文件lexicon.py时,在控制台中回显“ Yippee”。 Good start. 好的开始。 However, I have tried various commands to invoke the nosetests script in the action block. 但是,我尝试了各种命令来调用action块中的鼻子测试脚本。 My research suggests that something like this should work -> Invoke-Command -ScriptBlock { & $program } 我的研究表明,类似这样的方法应该起作用-> Invoke-Command -ScriptBlock {&$ program}
However, nothing happens at the console. 但是,控制台没有任何反应。 I think it could be something to do with the fact that nosetests needs to run from the project folder ie ex48 in this example. 我认为这可能与鼻子测试需要从项目文件夹(即本示例中的ex48)运行有关。 But I am not sure. 但我不确定。

Appreciate any guidance. 感谢任何指导。

filemonitor.ps1 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"
}

first of all in powershell for security no script run before you use 首先,为了安全起见,在Powershell中使用该脚本之前没有运行脚本

Set-ExecutionPolicy bypass

second for run programm u can use 第二个运行程序,您可以使用

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

or like 或喜欢

.\winrar.exe

third if you want run powershell script in run.exe or batch file you can use this syntax 第三,如果要在run.exebatch file运行powershell脚本, run.exe可以使用此语法

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

ok and your script System.IO.FileSystemWatcher just see your path and if you Created changed or deleted notify you see that 好的,您的脚本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)"}

ok with another powershell if you make txt file with add-content or make directory with mkdir that powershell you run that notify something like that Created: or in script you can end of script call $changed 好的,如果您运行带有add-content txt文件或使用mkdir生成运行powershell的目录,并通知类似Created add-content则可以使用另一个powershell,或者在脚本中可以结束脚本调用$changed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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