简体   繁体   中英

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

I am new to python and 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.

So far I have the code below in file filemonitor.ps1. When I run this at the console and make a change to file lexicon.py "Yippee" is echoed in the console. Good start. However, I have tried various commands to invoke the nosetests script in the action block. My research suggests that something like this should work -> 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. But I am not sure.

Appreciate any guidance.

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

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

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

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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