8

i'm trying to run a script which will run another powershell script. i need the first script to continue only after the other one is over. something like:

start Copy.ps1 wait till Copy.ps1 is done
continue with the script

tried using the Invoke-Expression but it doesn't have wait parameter.

Julia
  • 135
  • 1
  • 1
  • 7

2 Answers2

2

I think you easily can do it by calling the file like this:

<#
  here is some awesome code
#>

# Call P$ Script here
& C:\Path\To\CopyScript.ps1

<#
  awesome code continues here
#>

it will call the whole script in Copy.ps1 and continues after copy.ps1 finished.

Other method is you set the whole script in copy.ps1 into a function.

in CopyScript.ps1:

function MyCopyFunction(){

  # all code inside the Copy.ps1 is here
   
}

And in your new File:

# Call P$ Script here
& C:\Path\To\CopyScript.ps1

# then call the function
MyCopyFunction

Greetz Eldo.Ob

Eldo.Ob
  • 774
  • 4
  • 16
  • Unless you specifically want to run the script directly in the caller's scope - so as to load function, alias, variable _definitions_ from there - you should use `&`, the [call operator](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#call-operator-), instead of `.`, the [dot-sourcing operator](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#dot-sourcing-operator-). – mklement0 Aug 08 '22 at 14:38
  • In cases where the dot-sourcing operator _is_ called for, follow it by a _space_ to avoid the awkward need for wrapping the script path in `(...)`: `. C:\Path\To\Copy.ps1` – mklement0 Aug 08 '22 at 14:39
  • Thanks for updating, but now the `(...)` is superfluous and it's still not clear why you're dot-sourcing (`. `; running in the _same_ scope - rather than calling (`& `; running in a _child_ scope). Also note that to-end-of-line comments in PowerShell use `#`, not `//`. – mklement0 Oct 11 '22 at 23:20