-1

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!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Tom H
  • 46,766
  • 14
  • 87
  • 128

2 Answers2

1

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

Matt
  • 1,931
  • 12
  • 20
  • Thanks. I knew there was a syntax to call by a variable name that included parentheses, but I couldn't find it anywhere. I'll try some of my own tests as well and post back here with results. Also, my goal was to run a Setup function inside all of the scripts, but then I thought of using a "-setup" switch parameter instead... of course that requires being able to pass parameters :) – Tom H Jun 02 '11 at 13:59
  • I tested your first script and it just returned the value of the string variable (the script's full path). Invoke-Command had some problems (there seems to be a bug that requires using "-ComputerName" param, but then it's a remote call even if on the local computer. Invoke-Expression worked though. I'm going to post a solution. I gave you the up-vote. If you want me to change the selected answer then you can change yours and I'll switch it. Thanks for the help! – Tom H Jun 02 '11 at 15:12
  • @Tom-H. Thanks for the up vote - it's a little late for me at the moment. I've just realised Invoke-command uses WinRM when called with the filepath parameter locally - that was new to me! Glad to have helped. – Matt Jun 02 '11 at 22:05
  • @Tom-H I've added the Invoke-Expression like you have below. I'm not 100% happy with it though as it doesn't deal with spaces in the path.... – Matt Jun 03 '11 at 12:41
0

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"
Tom H
  • 46,766
  • 14
  • 87
  • 128