Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Query to variable - not wanting table style result Expand / Collapse
Author
Message
Posted Tuesday, December 04, 2012 8:00 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, May 14, 2013 5:04 AM
Points: 137, Visits: 115
Hi

I am trying to run a script that queries the last full backup file for a specific database. Invoke line formated for ease of reading.

# Script is ran on the SQL server (not remotely)
# Instance is called sql-instance
# database is called sql-data

import-module SQLPS -disableNameChecking
$instanceName = "$($env:computername)\sql-instance"
$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
$lastBackupFile = Invoke-Sqlcmd -Query "
SELECT TOP 1 msdb.dbo.backupmediafamily.physical_device_name
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
WHERE msdb..backupset.type = 'D' and msdb.dbo.backupset.database_name = 'sql-data'
ORDER BY msdb.dbo.backupset.backup_finish_date desc"


The result I get with this is in a table format:
physical_device_name
--------------------
C:\Program Files\Microsoft SQL Server\MSSQL11.SIMS2012\MSQL\Backup\full_sql-data_20121101.bak


Is there any way to just get the result and not the heading stuff?
I need just the path and filename for using elsewhere in the script.

I tried searching around but only found articles that give the table format and I don't know enouth to trim this down with confidence.

Thanks in advance
Post #1392510
Posted Tuesday, December 04, 2012 10:21 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Yesterday @ 1:53 AM
Points: 1,474, Visits: 2,342
If it's one result, you can get it like this:
$lastBackupFile.physical_device_name

This can be used in other methods, e.g.:

($lastBackupFile.physical_device_name).SubString(0, 30)

If it's multiple rows you can do something similar but include the row number:
$lastBackupFile[0].physical_device_name
Post #1392609
Posted Wednesday, December 05, 2012 2:15 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, May 14, 2013 5:04 AM
Points: 137, Visits: 115
Many thanks for that.

It must seem like common sense to you but I didn't realize that queries would actually return results with members that can be seperated. This opens up a world of possibility and extra knowledge for me.

Query resolved.
Post #1392850
Posted Wednesday, December 05, 2012 3:11 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Yesterday @ 1:53 AM
Points: 1,474, Visits: 2,342
No problem.
Still getting to grips with Powershell and very little of it is common sense so far

The hardest bit has been linking all the individual steps together - once you start seeing how you can pass info from one step to the next it really does become an amazing tool!

Cheers
Gaz
Post #1392875
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse