Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

[PowerShell] Find out if script is called from another script

$
0
0

I have a PowerShell script that we sometimes call by itself, but it is also called when running another script (via Start-Process).

In the called script, I want to be able to detect when the script is called by another script and when it is run directly.

Is this feasible?

Example:

ScriptA.ps1

ScriptB.ps1

Sometimes Script B is called directly at the PowerShell prompt:  .\ScriptB.ps1.  Other times, we call ScriptA.ps1 and inside ScriptA.ps1 it calls ScriptB.ps1 like so:  Start-Process powershell.exe -ArgumentList ".\ScriptB.ps1 -Parameter Argument" -NoNewWindow -Wait.

I want to put some code in ScriptB.ps1 that makes the script behave slightly differently when it is called directly vs. when it is called by ScriptA.ps1.

I have looked into $MyInvocation and [environment]::commandline, but neither of these display any evidence of being called by ScriptA.ps1.  Perhaps I need to call ScriptB.ps1 in ScriptA.ps1 differently?


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>