SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Finding Free Space per Data File with PowerShell


Finding Free Space per Data File with PowerShell

Author
Message
Ofer Gal
Ofer Gal
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 13
This gives me the log space used.
I need the data space used or unused
WI-DBA
WI-DBA
SSC Veteran
SSC Veteran (258 reputation)SSC Veteran (258 reputation)SSC Veteran (258 reputation)SSC Veteran (258 reputation)SSC Veteran (258 reputation)SSC Veteran (258 reputation)SSC Veteran (258 reputation)SSC Veteran (258 reputation)

Group: General Forum Members
Points: 258 Visits: 605
This should do it -


Set-ExecutionPolicy RemoteSigned
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")

Function Get-DatabaseFilesBySpaceAvailable ([Microsoft.SqlServer.Management.Smo.Server] $SmoSqlServer
, [decimal] $sizeThreshold=0.80)
{
$sqlServer.Databases | Where-Object{$_.Status -eq "Normal"} `
| Select-Object FileGroups -ExpandProperty FileGroups `
| Select-Object Files -ExpandProperty Files `
| Where-Object {$_.MaxSize -gt -1} `
| Where-Object {$_.UsedSpace -gt ($_.MaxSize * $sizeThreshold)} `
| Select UsedSpace,Size,MaxSize,FileName
}

$sqlServer = New-Object("Microsoft.SqlServer.Management.Smo.Server") "Server\Instance"

Get-DatabaseFilesBySpaceAvailable -SmoSqlServer $sqlServer| FT -autosize

Cheers
http://twitter.com/widba
http://widba.blogspot.com/
Aaron Nelson
Aaron Nelson
Old Hand
Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)

Group: General Forum Members
Points: 389 Visits: 173
WI-DBA (5/11/2011)
@Nadrek - Its a preference really, I don't like having lots of maintenance jobs on my servers, and when I come up with something new to check, the single deployment makes the whole process much quicker to deploy.


The PowerShell for just a single database would be:

Get-SqlDatabase "WIN7W510\R2" "AdventureWorks" | Get-SqlDataFile | Format-Table Server, dbname, FileGroup, FileName, FreeSpace, timestamp


Just make sure to run this first if you haven't already:

Import-Module SQLServer



and obviously change out AdventureWorks to be the name of your database that you want to look at and "WIN7W510\R2" to be the name of the server & instance that database is on.

@SQLvariantI have a PowerShell script for you.
SQLHeap
SQLHeap
Mr or Mrs. 500
Mr or Mrs. 500 (535 reputation)Mr or Mrs. 500 (535 reputation)Mr or Mrs. 500 (535 reputation)Mr or Mrs. 500 (535 reputation)Mr or Mrs. 500 (535 reputation)Mr or Mrs. 500 (535 reputation)Mr or Mrs. 500 (535 reputation)Mr or Mrs. 500 (535 reputation)

Group: General Forum Members
Points: 535 Visits: 741
Ofer Gal (5/11/2011)
This gives me the log space used.
I need the data space used or unused


Oops. Here is a query (sql2k compatible) that I use regularly for that.

SET NOCOUNT ON;

Declare
@TargetDatabase sysname,
@Level varchar(10),
@UpdateUsage bit,
@Unit char(2)

Select @TargetDatabase = NULL, -- NULL: all dbs
@Level = 'File', -- or "Database"
@UpdateUsage = 0, -- default no update
@Unit = 'GB' -- Megabytes, Kilobytes or Gigabytes

CREATE TABLE #Tbl_CombinedInfo (
DatabaseName sysname NULL,
[type] VARCHAR(10) NULL,
FileGroup VARCHAR(50) NULL,
LogicalName VARCHAR(150) NULL,
T dec(10, 2) NULL,
U dec(10, 2) NULL,
[U(%)] dec(5, 2) NULL,
F dec(10, 2) NULL,
[F(%)] dec(5, 2) NULL,
PhysicalName sysname NULL );

CREATE TABLE #Tbl_DbFileStats (
Id int identity,
DatabaseName sysname NULL,
FileId int NULL,
FileGroupID int NULL,
TotalExtents bigint NULL,
UsedExtents bigint NULL,
Name sysname NULL,
[FileName] varchar(255) NULL );

CREATE TABLE #Tbl_ValidDbs (
Id int identity,
Dbname sysname NULL );

CREATE TABLE #Tbl_Logs (
DatabaseName sysname NULL,
LogSize dec (10, 2) NULL,
LogSpaceUsedPercent dec (5, 2) NULL,
Status int NULL );

DECLARE @Ver varchar(10),
@DatabaseName sysname,
@Ident_last int,
@String varchar(2000),
@BaseString varchar(2000);

SELECT @DatabaseName = '',
@Ident_last = 0,
@String = '',
@Ver = CASE WHEN @@VERSION LIKE '%9.0%' THEN 'SQL 2005'
WHEN @@VERSION LIKE '%8.0%' THEN 'SQL 2000'
WHEN @@VERSION LIKE '%10.%' THEN 'SQL 2008'
END;

SELECT @BaseString = ' SELECT DB_NAME(), ' +
CASE WHEN @Ver = 'SQL 2000' THEN 'CASE WHEN a.status & 0x40 = 0x40 THEN ''Log'' ELSE ''Data'' END'
ELSE 'CASE type WHEN 0 THEN ''Data'' WHEN 1 THEN ''Log'' WHEN 4 THEN ''Full-text'' ELSE ''reserved'' END'
END
+ ', groupname, name, ' +
CASE WHEN @Ver = 'SQL 2000' THEN 'filename'
ELSE 'physical_name'
END
+ ', size*8.0/1024.0 FROM ' +
CASE WHEN @Ver = 'SQL 2000' THEN 'sysfiles a Left Join sysfilegroups b on a.groupid = b.groupid'
ELSE 'sys.database_files a Left Join sysfilegroups b on a.data_space_id = b.groupid'
END
+ ' WHERE ' +
CASE WHEN @Ver = 'SQL 2000' THEN ' HAS_DBACCESS(DB_NAME()) = 1'
ELSE 'state_desc = ''ONLINE'''
END
+ '';

SELECT @String = 'INSERT INTO #Tbl_ValidDbs SELECT name FROM ' +
CASE WHEN @Ver = 'SQL 2000' THEN 'master.dbo.sysdatabases'
WHEN @Ver IN ('SQL 2005', 'SQL 2008') THEN 'master.sys.databases'
END
+ ' WHERE HAS_DBACCESS(name) = 1 ORDER BY name ASC';
EXEC (@String);

INSERT INTO #Tbl_Logs EXEC ('DBCC SQLPERF (LOGSPACE) WITH NO_INFOMSGS');

BEGIN
WHILE 1 = 1
BEGIN
SELECT TOP 1 @DatabaseName = Dbname
FROM #Tbl_ValidDbs
WHERE Dbname > @DatabaseName
ORDER BY Dbname;

IF @@ROWCOUNT = 0
BREAK;

SELECT @Ident_last = ISNULL(MAX(Id), 0)
FROM #Tbl_DbFileStats;

SELECT @String = 'INSERT INTO #Tbl_CombinedInfo (DatabaseName, type, FileGroup, LogicalName, PhysicalName, T) ' + @BaseString;

EXEC ('USE [' + @DatabaseName + '] ' + @String);

INSERT INTO #Tbl_DbFileStats (FileId, FileGroupID, TotalExtents, UsedExtents, Name, FileName)
EXEC ('USE [' + @DatabaseName + '] DBCC SHOWFILESTATS WITH NO_INFOMSGS');

UPDATE #Tbl_DbFileStats
SET DatabaseName = @DatabaseName
WHERE Id BETWEEN @Ident_last + 1
AND @@IDENTITY;
END
END

UPDATE #Tbl_CombinedInfo
SET U = s.UsedExtents*8*8/1024.0
FROM #Tbl_CombinedInfo t
JOIN #Tbl_DbFileStats s ON t.LogicalName = s.Name
AND s.DatabaseName = t.DatabaseName;

UPDATE #Tbl_CombinedInfo
SET [U(%)] = LogSpaceUsedPercent,
U = T * LogSpaceUsedPercent/100.0
FROM #Tbl_CombinedInfo t
JOIN #Tbl_Logs l ON l.DatabaseName = t.DatabaseName
WHERE t.type = 'Log';

UPDATE #Tbl_CombinedInfo SET F = T - U, [U(%)] = U*100.0/T;

UPDATE #Tbl_CombinedInfo SET [F(%)] = F*100.0/T;

IF UPPER(ISNULL(@Level, 'DATABASE')) = 'FILE'
BEGIN
IF @Unit = 'KB'
UPDATE #Tbl_CombinedInfo
SET T = T * 1024, U = U * 1024, F = F * 1024;

IF @Unit = 'GB'
UPDATE #Tbl_CombinedInfo
SET T = T / 1024, U = U / 1024, F = F / 1024;

SELECT Case When CAST(SERVERPROPERTY('InstanceName') as varchar(50)) is NULL
Then CAST(SERVERPROPERTY('MachineName') as varchar(50))
Else CAST(SERVERPROPERTY('InstanceName') as varchar(50))
End as 'InstanceName',
DatabaseName AS 'Database',
type AS 'Type',
FileGroup,
LogicalName,
T AS 'Total',
U AS 'Used',
[U(%)] AS 'Used (%)',
F AS 'Free',
[F(%)] AS 'Free (%)',
PhysicalName
FROM #Tbl_CombinedInfo
WHERE DatabaseName LIKE ISNULL(@TargetDatabase, '%')
ORDER BY DatabaseName, type;

END

DROP TABLE #Tbl_CombinedInfo
DROP TABLE #Tbl_DbFileStats
DROP TABLE #Tbl_ValidDbs
DROP TABLE #Tbl_Logs



There is an exception to every rule, except this one...
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32168 Visits: 18551
Thanks for another great resource in the series.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Ofer Gal
Ofer Gal
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 13
Thanks! this script is great!
stiej1977
stiej1977
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 282
Hi

where do i get the out and write-datatable functions from?

i did find the code for out-datatable but when your script runs, the engine errors saying that the Process parameter is missing.

thanks
Aaron Nelson
Aaron Nelson
Old Hand
Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)

Group: General Forum Members
Points: 389 Visits: 173
Steven Jones-245254 (5/12/2011)
Hi

where do i get the out and write-datatable functions from?

i did find the code for out-datatable but when your script runs, the engine errors saying that the Process parameter is missing.

thanks


Here's the link to the Write-DataTable function: http://gallery.technet.microsoft.com/ScriptCenter/en-us/2fdeaf8d-b164-411c-9483-99413d6053ae

@SQLvariantI have a PowerShell script for you.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search