Run Multiple Powershell Scripts Sequentially - on a Folder - Combine Scripts into a Master Script

后端 未结 6 2029
花落未央
花落未央 2021-02-05 08:25

I have 6+ scripts and growing to run on a folder, what is the best way to have them run one after the other.

I have seen and tried this thread - it did not work unfortun

相关标签:
6条回答
  • 2021-02-05 09:00

    To run multiple scripts sequentially you can use the -Wait parameter on Start-Process like so:

    $scriptsList = 
    @(
        'C:\Users\WP\Desktop\Scripts\1.ps1'
        'C:\Users\WP\Desktop\Scripts\2.ps1'
        'C:\Users\WP\Desktop\Scripts\3.ps1'
    )
    
    foreach($script in $scriptsList)
    {
        Start-Process -FilePath "$PSHOME\powershell.exe" -ArgumentList "-command '& $script'" -Wait
    }
    

    PowerShell will wait for the current script to finish before running the next script

    0 讨论(0)
  • 2021-02-05 09:01

    Are you hard coding the paths to the files in the master file?

    In this case something like this should work

    Invoke-Expression "C:\Users\WP\Desktop\Scripts\1.ps1"
    Invoke-Expression "C:\Users\WP\Desktop\Scripts\2.ps1"
    Invoke-Expression "C:\Users\WP\Desktop\Scripts\3.ps1"
    
    0 讨论(0)
  • 2021-02-05 09:03

    To get the path that your script is in you can do this:

    $MyInvocation.MyCommand.Definition
    

    That will show something like 'C:\Users\WP\Desktop\Scripts\Master.ps1'. From that we can do a Split-Path to get just the folder, and run Get-ChildItem on the folder to get a list of files. We'll probably want to exclude the master script, so that we don't end up in a recursive loop, so that would look something like:

    $ScriptPath = Split-Path $MyInvocation.MyCommand.Definition
    Get-ChildItem "$ScriptPath\*.ps1" | Where{$_.FullName -ne $MyInvocation.MyCommand.Definition}
    

    Then we just run those through a ForEach-Object loop, and invoke the script with the call operator & as such:

    $ScriptPath = Split-Path $MyInvocation.MyCommand.Definition
    Get-ChildItem "$ScriptPath\*.ps1" | Where{$_.FullName -ne $MyInvocation.MyCommand.Definition} | ForEach-Object { & $_.FullName }
    

    Edit: Hm, that wasn't filtering right. Here's a re-write that does filter out the master script correctly.

    $Master = Split-Path $MyInvocation.MyCommand.Definition -Leaf
    $ScriptPath = Split-Path $MyInvocation.MyCommand.Definition
    Get-ChildItem "$ScriptPath\*.ps1" | Where{$_.Name -ne $Master} | ForEach-Object { & $_.FullName }
    
    0 讨论(0)
  • 2021-02-05 09:05

    If you don't want to hard code the path, you can make Master.ps1 like this:

    &"$PSScriptroot\1.ps1"
    &"$PSScriptroot\2.ps1"
    &"$PSScriptroot\3.ps1"
    

    And it will look for those scripts in the same directory where it is.

    0 讨论(0)
  • 2021-02-05 09:20

    Simply create a text file, using any code editor or text editor, and use the following example batch script:

    start /min powershell.exe C:\your folder\script1.ps1
    start /min powershell.exe C:\your folder\script2.ps1
    

    Save it as a script.bat and open it. This will make two powershell scripts run at the same time.

    0 讨论(0)
  • 2021-02-05 09:20

    The reason why the powershell screen pops up is cuz you dont have the argument -NoExit set. Please use the following as an example to see if your code is running properly:

    Start-Process "$pshome\powershell.exe" -ArgumentList "-NoExit", "-Command '& script'" -wait
    

    Sorry for the late response but I also ran into the issue and thats how I was able to resolve it to make sure it was work.

    Also I changes the file names as follows:

    $Scripts =
    @(
     ".\C:\Scripts\First.ps1"
     ".\C:\Scripts\Second.ps1"
     ".\C:\Scripts\Third.ps1"
     );
    
    0 讨论(0)
提交回复
热议问题