应该起作用:
脚本-A.ps1
if ($MyInvocation.InvocationName -eq $MyInvocation.MyCommand.Source)
{
Out-Host -InputObject ('Script startet with: ' + $MyInvocation.InvocationName)
}
else
{
Out-Host -InputObject ('Script startet with: ' + $MyInvocation.ScriptName)
}
Script-B.ps1
& C:\Temp\Script-A.ps1
输出:
PS C:\> C:\Temp\Script-A.ps1
Script startet with: C:\Temp\Script-A.ps1
PS C:\> C:\Temp\Script-B.ps1
Script startet with: C:\Temp\Script-B.ps1
实际上,我将使用输入参数(开关)来识别情况,因为仅当您在ISE中使用F5执行文件或直接在powershell.exe中运行脚本时,以上方法才有效。
,
在脚本中,$MyInvocation.PSCommandPath
包含:
因此:
if (-not $MyInvocation.PSCommandPath){
# Script was called from the PowerShell prompt or via the PowerShell CLI.
'DIRECT invocation'
}
else {
# Script was called from the script whose path is reflected in
# $MyInvocation.PSCommandPath
'Invocation VIA SCRIPT'
}
注意:
本文链接:https://www.f2er.com/3137325.html