Hi Robin,
The error you’ve posted doesn’t appear to have anything to do with the attached script, which has no calls to Out-File.
Focusing just on what you’ve attached, I can see a few apparent errors:
- You’ve set up the $logFolder_Path variable as an array, but I suspect you actually wanted it to be a string.
- $InstanceName is currently referring to an object of type DataRow, but based on how you’re using it, you probably want it to refer to the string data in the row’s single column.
- In the Add-LogFile line, you’re sending a path of $logFolder_Path:\<filename>. Based on what you’ve done with $logFolder_Path earlier in the code, it looks like you shouldn’t have the colon character in there.
Try making these changes:
# Change this line: $InstanceName = $($Dataset.Tables[0].Rows[0]) # To this: (Note, the subexpression operator $() isn't necessary here, but it doesn't hurt anything either) $InstanceName = $($Dataset.Tables[0].Rows[0][0]) # Change these lines: $logFolder_Path = @() $logFolder_Path = $logFolder_Path +$InstanceName # To this: (Note: the [string] cast is to make sure you get the right type, regardless of what the $InstanceName variable holds. That # doesn't guarantee that the value of the string will be correct, but it's a start. [string]$logFolder_Path = $InstanceName # Change this line: $logFile = Add-LogFile -Path $logFolder_Path:\dbrestorelog_$Timestamp.txt # To this: $logFile = Add-LogFile -Path $logFolder_Path\dbrestorelog_$Timestamp.txt
That’s probably the minimum changes you need to make to have a chance at getting this working. I have a few other comments, though:
- You don’t need to create the folders ahead of time, if you don’t want to. The PSLogging module will attempt to create them for you, if they don’t already exist. That’s up to you; maybe you want to do error handling on the folder creation, or something like that.
- If you do decide to create the folders yourself, you don’t have to create each folder in the path separately. A single call to New-Item will create an entire folder tree, if it doesn’t already exist. For example, you can try this: New-Item -Path $home\Documents\Testfolder\Subfolder\Yay -ItemType Directory
- Your log path is currently relative to the current working directory of the script. It’s up to you, but typically I’d use a rooted path (something like “C:\Logs\$InstanceName\DatabaseRestore\”)