【问题标题】:Method invocation failed ... does not contain a method named 'op_Addition'方法调用失败...不包含名为“op_Addition”的方法
【发布时间】: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


    【解决方案1】:

    我在发布后立即发现,当您使用 get-item 时,您不会得到一个字符串,而是一个对象。

    抛出错误是因为我试图将一个对象与另一个字符串连接起来。我需要使用以下内容,

    $ParentDirectory = (get-item $PSScriptRoot).parent
    $LogPath = $ParentDirectory.FullName + '\logs\InstallLog_' + $CurrentDate + '.txt'
    

    【讨论】:

    • 在我的例子中,我通过在前面添加 [string] 来投射输出。
    猜你喜欢
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多