我有点头脑发热,我有一个powershell脚本引用另一个脚本。在这个脚本的顶部,我有一个参考,
. $PSScriptRoot\bin\functions.ps1这会导致错误,
方法调用失败,因为System.IO.DirectoryInfo不包含名为“op_a”的方法。At ....\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行语句。我不认为我正在做的事情是复杂的,也不是其他人没有做过的事情。有什么我不知道的细微差别吗?
谢谢,
发布于 2018-02-27 22:52:16
我刚发完帖子就发现,当您使用get-item时,如果没有返回字符串,就会得到一个对象。
引发错误是因为我试图将一个对象与另一个字符串连接起来。我需要用以下方法,
$ParentDirectory = (get-item $PSScriptRoot).parent
$LogPath = $ParentDirectory.FullName + '\logs\InstallLog_' + $CurrentDate + '.txt'https://stackoverflow.com/questions/49019209
复制相似问题