我已經創建了一段代碼,可以在腳本中正常工作,但在轉換爲函數時失敗。使用函數設置腳本文件夾位置失敗
該代碼將腳本所在的位置設置爲當前位置。
Function Set-ScriptFolder
{
<#
.SYNOPSIS
Sets the location to the folder where the script folder resides.
.DESCRIPTION
Sets the location to the folder where the script folder resides.
.EXAMPLE
Set-ScriptFolder
.EXAMPLE
Set-ScriptFolder
#>
$location = Split-Path $MyInvocation.MyCommand.Path
set-location $location
}
輸出是:
Split-Path : Cannot bind argument to parameter 'Path' because it is null.
set-location : Cannot process argument because the value of argument "path" is null. Change the value of argument
"path" to a non-null value.
不知道如何轉換:
$location = Split-Path $MyInvocation.MyCommand.Path
set-location $location
的功能?
感謝
函數中的'$ MyInvocation'是該函數的調用信息嗎?那會是我想的問題。這意味着你可以從我認爲的腳本中將參數傳遞給你的函數......但是爲什麼呢? –