locked
[PowerShell] Find out if script is called from another script RRS feed

  • Question

  • 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?

    Thursday, December 18, 2014 3:30 PM

Answers

  • './scriptB.ps1' | set-content ./scriptA.ps1
    
    @'
    if ((Get-PSCallStack | select -expandproperty Command) -eq 'scriptA.ps1' )
      {Write-Host 'Script was called by scriptA.ps1'}
    '@ | set-content  ./scriptB.ps1 
    
    ./scriptA.ps1
    
    
    Script was called by scriptA.ps1


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinor Thursday, December 18, 2014 3:51 PM
    • Marked as answer by Bill_Stewart Wednesday, February 18, 2015 9:45 PM
    Thursday, December 18, 2014 3:49 PM

All replies

  • './scriptB.ps1' | set-content ./scriptA.ps1
    
    @'
    if ((Get-PSCallStack | select -expandproperty Command) -eq 'scriptA.ps1' )
      {Write-Host 'Script was called by scriptA.ps1'}
    '@ | set-content  ./scriptB.ps1 
    
    ./scriptA.ps1
    
    
    Script was called by scriptA.ps1


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinor Thursday, December 18, 2014 3:51 PM
    • Marked as answer by Bill_Stewart Wednesday, February 18, 2015 9:45 PM
    Thursday, December 18, 2014 3:49 PM
  • I'm surprised at this, but I don't understand what is going on in the example.  I'm not familiar with Set-Content nor the @' '@ syntax.

    Friday, December 19, 2014 9:51 PM
  • The @' '@ syntax is called a here-string.  It's a way of creating a multi-line string.

    See 

    Get-Help about_Quoting_Rules

    Then Set-Content writes it out to a file.  

    Essentially it's just creating test scripts for demo.  

    The Get-PSCallStack is the part that you need to port into your own script.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, December 19, 2014 10:03 PM
  • The @' '@ syntax is called a here-string.  It's a way of creating a multi-line string.

    See 

    Get-Help about_Quoting_Rules

    Then Set-Content writes it out to a file.  

    Essentially it's just creating test scripts for demo.  

    The Get-PSCallStack is the part that you need to port into your own script.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    Thanks!  I'll check it out.
    Wednesday, December 24, 2014 6:56 PM