0

I have plenty of ps1 scipts which I execute one by one manually, they have $ErrorActionPreference = "Stop" in their syntax so if something goes wrong they shall stop.

What I want help with is to create a ps1 script that can run all of these scripts sequentially. So if one script fails the "master script" must stop and not continue with next script.

1 Answer 1

0

If you have 4 files like:

1WriteHello.ps1

Write-Host "Hello " -NoNewLine

2WriteWorld.ps1

Write-Host "World!"

3WriteError.ps1

Write-Error "Something went wrong."

4WriteANumber.ps1

Write-Host "123"

You can run it with command:

$ErrorActionPreference = 'Stop'; Get-Item 'C:\Path\*.ps1' |% { & $_.FullName }

In your example:

$Scripts = 'D:\temp\download_script.ps1', 'D:\temp\upload_script.ps1', 'D:\temp\reset_scripts.ps1'
$ErrorActionPreference = 'Stop'
$Scripts |% { & $_ }
Sign up to request clarification or add additional context in comments.

2 Comments

You may want to sort the files you get, as not every file system gives you files ordered alphabetically (especially on non-Windows systems).
Also worth noting that % is an alias of ForEach-Object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.