This topic contains 4 replies, has 2 voices, and was last updated by
September 20, 2013 at 9:50 am #10291
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: (:) [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.
September 20, 2013 at 11:13 am #10293MemberTopics: 9Replies: 2322Points: 0Rank: Member
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:\
. 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.Rows) # To this: (Note, the subexpression operator $() isn't necessary here, but it doesn't hurt anything either) $InstanceName = $($Dataset.Tables.Rows) # 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\")
September 23, 2013 at 6:21 am #10311
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
September 23, 2013 at 9:30 am #10315MemberTopics: 9Replies: 2322Points: 0Rank: Member
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" with "Rows" 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.
September 23, 2013 at 10:00 am #10317
AWESOME....it worked this time.
thank you very much David...appreciate your help..
The topic ‘Create new directory and append the log file using powershell’ is closed to new replies.