Reputation: 1043
I have a bit of a headscratcher where I have a powershell script references another. At the top of this script I have the reference,
. $PSScriptRoot\bin\functions.ps1
This throws the error,
Method invocation failed because [System.IO.DirectoryInfo] does not contain a method named 'op_Addition'. At ....\bin\functions.ps1:5 char:1 + $LogPath = $ParentDirectory + '\InstallLog_' + $CurrentDate + '.txt' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
I cannot figure out why this is happening. Here is the code in the first 5 lines of the second file. The issue is with $Logs assignment.
$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'
If I hardcode the path in the $Logs variables, its fine. If I use the variable, $CurrentDirectory instead of $ParentDirectory, its fine.
If fails with $ParentDirectory or use the statement in line for $Logs. I don't think what I'm doing is complicated nor anything that someone else has not done. Is there some nuance I do not know about?
Thanks,
Upvotes: 2
Views: 12003
Reputation: 1043
I discovered right after I posted that when you use get-item you do not get a string back, you get an object.
The error is being thrown because I was trying to concatenate an object with another string. I needed to use the following,
$ParentDirectory = (get-item $PSScriptRoot).parent
$LogPath = $ParentDirectory.FullName + '\logs\InstallLog_' + $CurrentDate + '.txt'
Upvotes: 2