mamadali mamadali - 7 months ago 135
PowerShell Question

System IO DirectoryInfo

I just try to restore SQL backup to my restore server and I wrote script to do it but I get this error on my PowerShell console:

Method invocation failed because [System.IO.DirectoryInfo] does not contain a method named 'op_Addition'.
At line:39 char:1
+ $dbRestoreLog.LogicalFileName = $dbname + "_Log"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound


From the error shown, $dbname is obviously a DirectoryInfo object.

My guess it that you retrieved it through Get-Item or Get-ChildItem.

If you want the LogicalFileName property to be the name of the $dbname folder with the string "_Log" appended, do:

$dbRestoreLog.LogicalFileName = $dbname.Name + "_Log"

Alternatively have PowerShell implicitly convert $dbname to a string by placing it inside double-quotes (DirectoryInfo.ToString() defaults to the Name property anyways):

$dbRestoreLog.LogicalFileName = "$dbname" + "_Log"

You could similarly use the -f string format operator:

$dbRestoreLog.LogicalFileName = '{0}_Log' -f $dbname