因此,我可以使用以下命令找到脚本的目录
function Get-ScriptDirectory { Split-Path -parent $PSCommandPath }
但是我无法将目录更改为保存脚本的文件夹。例如
cd d:\MyScripts\
所以像
cd function Get-ScriptDirectory { Split-Path -parent $PSCommandPath }
不起作用。
很抱歉,如果这是一个常见而又愚蠢的问题,但是我是Powershell的新手,我在搜索并尝试了很多之后才在此处发布
您可以按照以下步骤进行操作。您可以在脚本中包含代码示例。然后,如果您正在其他位置运行脚本(通过脚本的完全限定路径名);$MyInvocation
将提取调用位置。
来自文档
$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.
代码样例
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
cd $dir
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句