Create new directory and append the log file using powershell

Hi guys,

I’m trying to find the log firectory location by connecting to SQL database and store the log file in that location.We have standard log directory with naming convention. After i get the director from sql database, i need to create two more subdirectories and append the log file into that location ( for log file i’m using add-log file and pslogging function ).

I’m able to get the log directory information from sql database but script is failing to create the new directories and append the path near add-file command.

Here’s the error :

Cannot convert ‘System.Object’ to the type ‘System.String’ required by parameter ‘FilePath’. Specified method is not supported.
At C:\Program Files\CommVault\Simpana\Base\final2.ps1:306 char:25

  • $template_xml | Out-File <<<< $Output -Encoding ascii
    • CategoryInfo : InvalidArgument: (:slight_smile: [Out-File], ParentContainsErrorRecordException
    • FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.OutFileCommand
      Please find the attached powershell script and please let me know if i miss anything.

Thanks in advance.

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:

  1. You've set up the $logFolder_Path variable as an array, but I suspect you actually wanted it to be a string.
  2. $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.
  3. 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\")

Thank you very much David for your detail response…I really appreciate it

I have some questions regarding your post above…
Here $logfolder_path is the path : C:\test\log
$instancename = DBATest ( sql server instance name )
I’m trying to create a folder with above instance name, once the folder is created, path should look like C:\test\log\DBATest
After the instance name is created, we need to create a folder DatabaseRestore…
This whole operation is a one time thing ( i’m still keeping this code because when a new sql instance comes into picture it automatically creates these folders ), so i’m using test-path to check if the folder is already present.
I made your changes and tested the code , it still wont create those folders but it says folder already exists

Folder: DBATest Already Exists
Folder: DBATest\DatabaseRestore Already Exists

And regarding the out-file…i’m creating an xml file and saving the xml file at $logfolder_path ( same location )…

$filename = ‘\template_restore_’ + $Timestamp + ‘.xml’ ------- ( $timestamp is a function created in the script…when log file or xml file is generated it will be stored with timestamp )
[string]$Output = $logFolder_Path + $filename
$template_xml = “<?xml version=”“1.0"” encoding =““UTF-8"” standalone=”“no”" ?>
$template_xml | Out-File $Output -Encoding ascii

Please let me know your thoughts… and let me know if you need more information

I just looked over your code again, and realized that I missed some parts of it earlier. I had assumed that the line “$logFolder_Path = @()” was the first time you were doing anything with the $logFolder_Path variable, when it was not.

This time I’ll just attach my recommended updates. I’ve replaced “Rows[0]” with “Rows[0][0]” in all places that occurs, and any time you were appending to the path variable, I’ve updated those lines to use the Join-Path cmdlet.

Give it a shot and let me know if you have any problems.

AWESOME…it worked this time.

thank you very much David…appreciate your help…