【发布时间】:2018-02-27 22:47:31
【问题描述】:
我有一个令人头疼的地方,我有一个 powershell 脚本引用另一个。在这个脚本的顶部我有参考,
. $PSScriptRoot\bin\functions.ps1
这会引发错误,
方法调用失败,因为 [System.IO.DirectoryInfo] 不包含名为“op_Addition”的方法。 在 ....\bin\functions.ps1:5 char:1 + $LogPath = $ParentDirectory + '\InstallLog_' + $CurrentDate + '.txt' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
我无法弄清楚为什么会这样。这是第二个文件前 5 行中的代码。问题在于 $Logs 分配。
$invocation = (Get-Variable MyInvocation).Value
$CurrentDate = Get-Date -format "yyyy_MM_dd_hhmmss"
$CurrentDirectory = Split-Path $invocation.MyCommand.Path
$ParentDirectory = (get-item $CurrentDirectory).parent
$LogPath = $ParentDirectory + '\InstallLog_' + $CurrentDate + '.txt'
如果我在 $Logs 变量中硬编码路径,那就没问题了。 如果我使用变量 $CurrentDirectory 而不是 $ParentDirectory,则很好。
如果 $ParentDirectory 失败或使用 $Logs 行中的语句。我不认为我正在做的事情很复杂,也没有其他人没有做过的事情。有什么我不知道的细微差别吗?
谢谢,
【问题讨论】:
标签: powershell