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 - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -