在多个 Powershell 脚本中运行设置的函数名称
我正在为我当前的开发团队构建一个测试框架。我想让他们做的一件事是创建一个 Powershell 脚本来运行他们的测试。该系统是一个数据库部署系统,因此为了测试它,他们可能需要运行一些设置代码,然后部署将开始,最后他们将运行一些检查代码。
由于部署需要一段时间,我希望框架能够为所有测试处理一次。因此,基本流程是:
Run test #1 set-up
Run test #2 set-up
Run test #3 set-up
Run the deploy process
Run the code to confirm that test #1 passed
Run the code to confirm that test #2 passed
Run the code to confirm that test #3 passed
我认为框架总是在特定目录中的所有 Powershell 脚本中调用名为“setup”(或类似内容)的函数。如果不存在“设置”功能也没关系,并且不会出错。然后我将运行部署,然后运行 Powershell 脚本中的其他功能。
给定一个目录列表,我如何循环遍历每个 Powershell 脚本并运行这些函数?
感谢您的指导!
I am building a testing framework for my current development team. One thing that I'd like to let them do is create a Powershell script to run their tests. The system is a database deploy system, so to test it they will need to potentially run some set-up code, then the deploy will be kicked off, then they will run some check code at the end.
Since the deploy takes awhile I'd like to have the framework handle that once for all of the tests. So, the basic flow is:
Run test #1 set-up
Run test #2 set-up
Run test #3 set-up
Run the deploy process
Run the code to confirm that test #1 passed
Run the code to confirm that test #2 passed
Run the code to confirm that test #3 passed
I figured that I would have the framework always call a function called "setup" (or something similar) in all of the Powershell scripts in a particular directory. If no "setup" function exists that's ok and it shouldn't error out. Then I would run the deploy and then run the other functions in the Powershell scripts.
Given a directory listing, how could I cycle through each Powershell script and run these functions?
Thanks for any guidance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将递归通过给定的文件夹并执行它找到的所有 setup.ps1 脚本。
但它不接受参数......
如果您只想深入一个文件夹,这将完成这项工作:
参数不起作用让我有点恼火 - 我想知道使用 Invoke-Command 是否可以工作那。我现在没有时间尝试,除非其他人弄清楚,我稍后会看看。
的脚本
这是我用于 setup.ps1 HTH
this will recurse through the given folder and execute all the setup.ps1 scripts it finds.
It doesn't accept parameters though....
If you just wanted to go one folder deep this will do the job:
It's irritated me a little that the parameters didn't work - I wonder if using the Invoke-Command could work for that. I haven't got time to try now, unless anyone else figures it out I'll have a look later.
Here's the script i used for setup.ps1
HTH
感谢 Matt 的想法,我能够遇到
Invoke-Expression
。理想情况下,我希望将Invoke-Command
与-filepath
参数一起使用,该参数默认在本地运行。但是,存在一个错误,即使在本地运行时也需要使用-ComputerName
参数。如果您使用该参数,则即使在本地计算机上运行,也需要打开远程处理。这是我在脚本中使用的代码:
然后我的测试脚本如下所示:
Thanks to Matt's ideas I was able to come across
Invoke-Expression
. Ideally I would have liked to useInvoke-Command
with the-filepath
parameter, which supposedly defaults to running locally. However, there is a bug that requires using the-ComputerName
parameter even when running locally. If you use that parameter then it requires remoting to be turned on, even when running on the local computer.Here's the code that I used in my script:
My test script then looked like this: