gpt4 book ai didi

powershell - $MyInspiration.MyCommand.Name 提供奇怪的结果

转载 作者:行者123 更新时间:2023-12-02 23:13:55 26 4
gpt4 key购买 nike

为什么在函数内执行时,$MyInitation.MyCommand.Name 会给出函数的名称?根据this它的目的是为您提供脚本文件名。这是我正在使用的代码:

function startScript
{
$ScriptName = $MyInvocation.MyCommand.Name
$ScriptName
}

$ScriptName = $MyInvocation.MyCommand.Name
$ScriptName

startScript

<#
Output:

testing.ps1
startScript
#>

最佳答案

根据PowerShell documentation :

$MyInvocation
Contains an information about the current command, such as the name,
parameters, parameter values, and information about how the command was
started, called, or "invoked," such as the name of the script that called
the current command.

$MyInvocation is populated only for scripts, function, and script blocks.
You can use the information in the System.Management.Automation.InvocationInfo
object that $MyInvocation returns in the current script, such as the path
and file name of the script ($MyInvocation.MyCommand.Path) or the name of a
function ($MyInvocation.MyCommand.Name) to identify the current command.
This is particularly useful for finding the name of the current script.

如果您对脚本路径感兴趣,另请参阅以下段落:

   Beginning in Windows PowerShell 3.0, $MyInvocation has the following new
properties.

-- PSScriptRoot: Contains the full path to the script that invoked the
current command. The value of this property is populated only when
the caller is a script.

关于powershell - $MyInspiration.MyCommand.Name 提供奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25603958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com