run multiple scripts inside main logic

  • Currently I have this script executing based off another process, but only if I actually have a file to process. Now I would like to add

    additional scripts to be executed. What is the best way to go about that, and if something fails don't process anymore that follow.

    Thanks.

    $path = '\\srv1\dblocks\' 
    $file = Get-ChildItem -Path $Path -Filter wait*.* |?{-not $_.PsIsContainer} | Sort CreationTime | Select -Last 1

    Write-Output "The most recently created file is $($file.Name)"
    If ($file.Length -gt 1024)

    {

    do something I'm sending Email

    Call other scripts before executing Main script and catch errors

    }
    Else
    {
    $file.Length
    Write-Output "No Locks found in file $($file.Name)"
    }
  • Do you see any downside of using something like this embedding in original script?

     

    $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
    }
  • Bruin's solution works with the exception that it doesn't capture error messages from the scripts.  But what you COULD do is a modified version of Bruin's script, but it does depend on how you return errors.  Your scripts would NEED to have some return value that you could validate as a "failure".

    How I would modify Bruin's script would be like:

    $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)
    {
    $result = & $script
    if ($result = "error") {
    Do Error stuff like exit script
    }
    }

    That way you capture the result of the script (such as errors).

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • Thanks,

    Is it a problem that for each script it pops open a Window to execute?

     

  • That is on you to decide if it is a problem or not.  If it is, then you are going to need to come up with another solution.  If it isn't, then go for it.

    Personally, I like having the window pop up so I can see that it is doing something.

    it really depends on your approach and requirements though.  Like if the script 1.ps1 doesn't have any output, and you are not allowed to modify it, then the approach I suggested won't work as it requires some output from the script to indicate an error.

    Alternately, you may handle errors entirely inside the called scripts and may not need to have it handled in the calling script.

    That being said, I generally don't develop scripts that are complex enough to need me to call secondary scripts.  My powershell scripts are usually short and to the point - "get AD user groups.ps1" for example would get me the groups for a specified user.  Most of my powershell scripts are 10 lines or less.  Once it starts getting more complicated than that, I start questioning if powershell is the right tool for what I am trying to do.  I do have a few longer ones, but the longer ones are usually only longer due to me putting a lot of "if" statements in them.  I have not had a need to call secondary scripts within powershell with anything I have developed.

    With the example I gave, on my system, I get no window popping up when I run it.

    Now, if you are trying to run this as a windows scheduled task without having a user logged in, it MAY give you errors as it can't create the window as there is no GUI running.

    In your case, if you are not sure if having a window pop up will be a problem or not, I would suggest you try it on a test system and see if it works the way you need.  Scripts, like all coding, should be run on a test system where you can do things to validate your error scenarios and ensure that things are working as expected.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • Thanks again!!

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply