• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

Call /execute powershell script from inside another script

I have a Main Powershell Script that looks at a text file with headers of computername, service, script. The script looks at the machines under computer name then checks the service listed for each computer and either stops or starts the service based on the request. That part is working However I also have an elseif($computer.script -eq "script") function that I would like to use to pull in an external script and execute it.. So My txt file is like
 Im trying to understand how to pull in that external script and then execute it, in a format like follows.
 elseif($computer.operation -eq "script")
    # run external script.

Where I am running into issues is with how to pull in an external script and running it correctly. I thought about using something like an:
Invoke-Expression    <-- But I am not sure how to do that if each line that has a server name has a different script (in this case I may want to use a script for resetting ColdFusion Services on one server but a different external script for something else on a different server)
1 Solution
Giovanni HewardCommented:
One method:

$str = [IO.File]::ReadAllText(".\External_script.ps1")
Invoke-Expression $str

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now