在多个 Powershell 脚本中运行设置的函数名称

发布于 2024-11-11 17:59:55 字数 595 浏览 3 评论 0原文

我正在为我当前的开发团队构建一个测试框架。我想让他们做的一件事是创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笙痞 2024-11-18 17:59:55

这将递归通过给定的文件夹并执行它找到的所有 setup.ps1 脚本。

Get-ChildItem D:\test -Recurse | where { $_.name -eq "setup.ps1" }| foreach {
   "Executing $($_.Fullname)"
    Invoke-Expression "$($_.Fullname) -setup -Verbose"
}

但它不接受参数......

如果您只想深入一个文件夹,这将完成这项工作:

Get-ChildItem D:\test | where{$_.psiscontainer}|foreach {
    Get-ChildItem $_.fullname | where { $_.name -eq "setup.ps1" }| foreach {
       "Executing $($_.Fullname)"
       Invoke-Expression "$($_.Fullname) -setup -Verbose"
    }
}

参数不起作用让我有点恼火 - 我想知道使用 Invoke-Command 是否可以工作那。我现在没有时间尝试,除非其他人弄清楚,我稍后会看看。

的脚本

[cmdletbinding()]
Param()

function setup() {

    Write-Verbose "In setup 1"
    Write-Output "Done setup 1"

}

setup

这是我用于 setup.ps1 HTH

this will recurse through the given folder and execute all the setup.ps1 scripts it finds.

Get-ChildItem D:\test -Recurse | where { $_.name -eq "setup.ps1" }| foreach {
   "Executing $($_.Fullname)"
    Invoke-Expression "$($_.Fullname) -setup -Verbose"
}

It doesn't accept parameters though....

If you just wanted to go one folder deep this will do the job:

Get-ChildItem D:\test | where{$_.psiscontainer}|foreach {
    Get-ChildItem $_.fullname | where { $_.name -eq "setup.ps1" }| foreach {
       "Executing $($_.Fullname)"
       Invoke-Expression "$($_.Fullname) -setup -Verbose"
    }
}

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

[cmdletbinding()]
Param()

function setup() {

    Write-Verbose "In setup 1"
    Write-Output "Done setup 1"

}

setup

HTH

沒落の蓅哖 2024-11-18 17:59:55

感谢 Matt 的想法,我能够遇到 Invoke-Expression。理想情况下,我希望将 Invoke-Command-filepath 参数一起使用,该参数默认在本地运行。但是,存在一个错误,即使在本地运行时也需要使用 -ComputerName 参数。如果您使用该参数,则即使在本地计算机上运行,​​也需要打开远程处理。

这是我在脚本中使用的代码:

# Run the setup functions from all of the Powershell test scripts
foreach ($testPSScript in Get-ChildItem "$testScriptDir\*.ps1") {
    Invoke-Expression "$testPSScript -Setup"
}

# Do some other stuff

# Run the tests in the Powershell test scripts
foreach ($testPSScript in Get-ChildItem "$testScriptDir\*.ps1") {
    Invoke-Expression "$testPSScript"
}

然后我的测试脚本如下所示:

param([switch]$Setup = $false)

if ($Setup) {write-host "Setting things up"; return}

Write-Host "Running the tests"

Thanks to Matt's ideas I was able to come across Invoke-Expression. Ideally I would have liked to use Invoke-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:

# Run the setup functions from all of the Powershell test scripts
foreach ($testPSScript in Get-ChildItem "$testScriptDir\*.ps1") {
    Invoke-Expression "$testPSScript -Setup"
}

# Do some other stuff

# Run the tests in the Powershell test scripts
foreach ($testPSScript in Get-ChildItem "$testScriptDir\*.ps1") {
    Invoke-Expression "$testPSScript"
}

My test script then looked like this:

param([switch]$Setup = $false)

if ($Setup) {write-host "Setting things up"; return}

Write-Host "Running the tests"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文