python - Determine if Powershell script started in main scope? -


i beginner powershell user. writing script files (.ps1)

i determine how script invoked:

was "main" script or dot sourced file?

in python, use like:

if __name__ == "__main__": 

is there similar in powershell?

update

after reading answers, using following @ end of .ps1 file:

if ($myinvocation.invocationname -ne '.') {   # "main" stuff here } 

any answers include how fail welcome.

it appears duplicate question, didn't use right search terms: determine if powershell script has been dot-sourced

if you're wanting know how invoked, have @ $myinvocation automatic variable.

if want test if you're in global scope:

try {if (get-variable args -scope 1){$true}} catch {$false} 

should return $true if you're running in child scope. if you're in global scope, there no parent scope , throw error , return $false.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -