如何从ps1脚本在powershell控制台执行程序

2024-10-02 10:19:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉python和powershell。为了尽可能地自动化我的测试,我尝试在对特定的.py文件进行更改时触发nosetests可执行文件在控制台上运行。在

到目前为止,我有下面的代码在文件中文件监视器.ps1. 当我在控制台上运行这个并对文件进行更改时词典.py控制台中会回响“Yippee”。好的开始。但是,我尝试了各种命令来调用操作块中的nosetests脚本。我的研究表明,类似这样的方法应该有效->;Invoke Command-ScriptBlock{&;$program}
但是,控制台上什么也没有发生。我认为这可能是因为nosetests需要从这个例子中的项目文件夹ieex48运行。但我不确定。在

感谢任何指导

在文件监视器.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"
}

Tags: 文件代码py命令可执行文件programnosetests词典
1条回答
网友
1楼 · 发布于 2024-10-02 10:19:11

首先,为了安全起见,在powershell中不运行脚本

Set-ExecutionPolicy bypass

第二个 对于运行程序,您可以使用

^{pr2}$

或者类似的

.\winrar.exe

第三 如果要在run.exebatch file中运行powershell脚本 您可以使用此语法

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可以吗 创建: 或者在脚本中,您可以结束脚本调用$changed

相关问题 更多 >

    热门问题