powershell - Get running script name from a function - Stack Overflow

admin2025-04-18  3

I can't seem to get this idea to work with good results.

I am trying to have meaningful automatic filename creating based on the running script name.

   function GetScriptName {
            $callStack = Get-PSCallStack
            $callerScript = $callStack[2].Location  # Get the outermost script location
            
            if ([string]::IsNullOrWhiteSpace($callerScript)) {
                return $null
            }
            
            return $callerScript
        }

If I paste the function above into a script and run that script the function works.

If I import the function into a script and call it with GetScriptName while that script is running often there is nothing returned or I get the file name of the function which is not helpful.

$callStack = Get-PSCallStack
$callerScript = $callStack[1].Location

i have tested with various numbers inside [] and I have tested a function that contains the line below

[System.IO.Path]::GetFileName($MyInvocation.MyCommand.Path)

All results are either blank or the file name of the function inconsistent unless the code above is run in the script itself.

I can't seem to get this idea to work with good results.

I am trying to have meaningful automatic filename creating based on the running script name.

   function GetScriptName {
            $callStack = Get-PSCallStack
            $callerScript = $callStack[2].Location  # Get the outermost script location
            
            if ([string]::IsNullOrWhiteSpace($callerScript)) {
                return $null
            }
            
            return $callerScript
        }

If I paste the function above into a script and run that script the function works.

If I import the function into a script and call it with GetScriptName while that script is running often there is nothing returned or I get the file name of the function which is not helpful.

$callStack = Get-PSCallStack
$callerScript = $callStack[1].Location

i have tested with various numbers inside [] and I have tested a function that contains the line below

[System.IO.Path]::GetFileName($MyInvocation.MyCommand.Path)

All results are either blank or the file name of the function inconsistent unless the code above is run in the script itself.

Share edited Jan 30 at 22:54 dcaz asked Jan 30 at 14:40 dcazdcaz 9099 silver badges28 bronze badges 4
  • 1 How about using $fullName = if ($PSCommandPath) { $PSCommandPath } else { $MyInvocation.PSCommandPath } ? – Theo Commented Jan 30 at 16:00
  • As far as I can tell $MyInvocation.PSCommandPath always returns the name of the function so if I use that line directly in the script it would work but I am trying to get i to be a function to deploy it for a few use cases. – dcaz Commented Jan 30 at 16:06
  • I will test with $PSCommandPath – dcaz Commented Jan 30 at 16:07
  • I can automate a 1 line using $PSCommandPath but it can not be in an external function. Maybe there is no way to use an external function. – dcaz Commented Jan 30 at 16:53
Add a comment  | 

1 Answer 1

Reset to default 0

Unless someone has a better answer. I will just add the following to my scripts that need automatic names. Its a bummer that I can't get it to work as an imported function.

$filename = $home + [IO.Path]::DirectorySeparatorChar + (Get-Date).ToString("yyyy-MM-dd_") + [System.IO.Path]::GetFileName($MyInvocation.MyCommand.Path) -replace '\.[^.]+$','.csv'

转载请注明原文地址:http://anycun.com/QandA/1744913086a89391.html