Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

can you convert objects? System.Object to SMO

$
0
0

Hi all, 

trying to play around with a restore script to pull out random full backup file and then restore it. 

im having issues with the -relocatefile in "restore-sqldatabase", ive got the answers i need via doing a invoke-sqlcmd and putting the results into a variable, but when i add them to the restore statement i get the error:

 

Cannot convert the "System.Object[]" value of type "System.Object[]" to type "Microsoft.SqlServer.Management.Smo.RelocateFile".

At line:34 char:66

+ [Microsoft.SqlServer.Management.Smo.RelocateFile]$fileheader = @(Invoke-Sqlcmd - ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException

    + FullyQualifiedErrorId : RuntimeException

Cannot index into a null array.

At line:35 char:1

+ $RelocateData = $fileheader.logicalName[0]

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.

At line:36 char:1

+ $RelocateLog = $fileheader.logicalName[1]

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : NullArray

 

 

my code is currently:

##Import-Module sqlps -ErrorAction SilentlyContinue

 

## restore server

$server = "dev-sqlserver"

## file location

$loc = "\\serverA\it\SQL_Backups"

$ignore = @("DIFF","LDF");

$files = Get-ChildItem -Path $loc -recurse -file *.bak |

 ? { !$_.PSIsContainer } | % { $relative = $_.FullName.Replace($loc,""); $nomatch = $true; foreach ($folder in $ignore) { if($relative -like "*\$folder\*") { $nomatch = $false } }; if ($nomatch) { $_ } }

##

##$files | ft name, directory -AutoSize

## get random number of files to restore

$restores = $files | get-random -count 1

## restore process 

foreach ($f in $restores){

$path = $f.DirectoryName

$pathSplit = $path.split(“\”)

$splitsinPath = $pathSplit.countfile

$splitDatabaseName = $splitsinPath - 2

$splitServerName = $splitsinPath - 3

$databaseName = [string]$pathSplit[$splitDatabaseName]

$ServerName = [string]$pathSplit[$splitServerName]

$dateofRestore = Get-Date -format dmyyyy_hhmmss

$RestoreDatabaseName = $ServerName + "_" + $databaseName + "_" + $dateofRestore

$filepath = $f.FullName

 

[Microsoft.SqlServer.Management.Smo.RelocateFile]$fileheader = @(Invoke-Sqlcmd -ServerInstance "DEV-BERRY" -Query "RESTORE FILELISTONLY FROM DISK='$filepath'") 

$RelocateData = $fileheader.logicalName[0]

$RelocateLog = $fileheader.logicalName[1]

## Write host the details of what you are restoring

write-host "Resotring Database : "  $databaseName -ForegroundColor Green

write-host "From File : "  $f.FullName -ForegroundColor Green

write-host "Restored to Database : "  $RestoreDatabaseName -ForegroundColor Green

## now we have all the vars.. lets run the restore

Restore-SqlDatabase -ServerInstance $server -Database $RestoreDatabaseName -BackupFile $f.FullName -ReplaceDatabase -RelocateFile @($RelocateData,$RelocateLog)

}

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles