Skip to content

Commit acdd682

Browse files
author
Shawn Melton
authored
Merge pull request #2 from wsmelton/development
Refresh changes 08-16-2016
2 parents 71850cd + 5b04b68 commit acdd682

File tree

3 files changed

+180
-165
lines changed

3 files changed

+180
-165
lines changed

functions/Copy-SqlJob.ps1

+2
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,14 @@ Shows what would happen if the command were executed using force.
184184
Write-Output "Disabling $jobname on $destination"
185185
$destserver.JobServer.Jobs.Refresh()
186186
$destserver.JobServer.Jobs[$job.name].IsEnabled = $False
187+
$destserver.JobServer.Jobs[$job.name].Alter()
187188
}
188189

189190
if ($DisableOnSource)
190191
{
191192
Write-Output "Disabling $jobname on $source"
192193
$job.IsEnabled = $false
194+
$job.Alter()
193195
}
194196
}
195197
}
+119-79
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
function Get-DbaDatabaseFreespace{
1+
function Get-DbaDatabaseFreespace
2+
{
23
<#
34
.SYNOPSIS
45
Returns database file space information for database files on a SQL instance.
@@ -36,7 +37,13 @@ PSCredential object to connect under. If not specified, currend Windows login wi
3637
3738
.PARAMETER IncludeSystemDBs
3839
Switch parameter that when used will display system database information
40+
41+
.PARAMETER Databases
42+
Specify one or more databases to process.
3943
44+
.PARAMETER Exclude
45+
Specify one or more databases to exclude.
46+
4047
.LINK
4148
https://dbatools.io/Get-DbaDatabaseFreespace
4249
@@ -56,84 +63,117 @@ the output object by any files that have a percent used of greater than 80%.
5663
5764
Returns all user database files and free space information for the localhost and
5865
localhost\namedinstance SQL Server instances. Processes data via the pipeline.
66+
67+
.EXAMPLE
68+
Get-DbaDatabaseFreespace -SqlServer localhost -Databases db1, db2
69+
70+
Returns database files and free space information for the db1 and db2 on localhost.
5971
#>
60-
[CmdletBinding(SupportsShouldProcess = $true)]
61-
param([parameter(ValueFromPipeline,Mandatory = $true)]
72+
[CmdletBinding()]
73+
param ([parameter(ValueFromPipeline, Mandatory = $true)]
6274
[Alias("ServerInstance", "SqlInstance")]
63-
[object[]]$SqlServer
64-
,[System.Management.Automation.PSCredential]$SqlCredential
65-
,[switch] $IncludeSystemDBs)
66-
BEGIN{
67-
$outputraw = @()
68-
$sql = @'
69-
SELECT
70-
@@SERVERNAME as SqlServer
71-
,DB_NAME() as DBName
72-
,f.name AS [FileName]
73-
,fg.name AS [Filegroup]
74-
,f.physical_name AS [PhysicalName]
75-
,CAST(CAST(FILEPROPERTY(f.name, 'SpaceUsed') AS int)/128.0 AS DECIMAL(15,2)) as [UsedSpaceMB]
76-
,CAST(f.size/128.0 - CAST(FILEPROPERTY(f.name, 'SpaceUsed') AS int)/128.0 AS DECIMAL(15,2)) AS [FreeSpaceMB]
77-
,CAST((f.size/128.0) AS DECIMAL(15,2)) AS [FileSizeMB]
78-
,CAST((FILEPROPERTY(f.name, 'SpaceUsed')/(f.size/1.0)) * 100 as DECIMAL(15,2)) as [PercentUsed]
79-
FROM sys.database_files AS f WITH (NOLOCK)
80-
LEFT OUTER JOIN sys.filegroups AS fg WITH (NOLOCK)
81-
ON f.data_space_id = fg.data_space_id
82-
'@
83-
}
84-
85-
PROCESS{
86-
foreach($s in $SqlServer){
87-
#For each SQL Server in collection, connect and get SMO object
88-
Write-Verbose "Connecting to $SqlServer"
89-
$server = Connect-SqlServer $SqlServer -SqlCredential $SqlCredential
90-
#If IncludeSystemDBs is true, include systemdbs
91-
#only look at online databases (Status equal normal)
92-
try{
93-
if($IncludeSystemDBs){
94-
$dbs = $server.Databases | Where-Object {$_.status -eq 'Normal'}
95-
} else {
96-
$dbs = $server.Databases | Where-Object {$_.status -eq 'Normal' -and $_.IsSystemObject -eq 0}
97-
}
98-
}
99-
catch{
100-
Write-Exception $_
101-
throw "Unable to gather dbs for $($s.name)"
102-
continue
103-
}
104-
105-
foreach($db in $dbs){
106-
try{
107-
Write-Verbose "Querying $($s) - $($db.name)."
108-
#Execute query against individual database and add to output
109-
$outputraw += ($db.ExecuteWithResults($sql)).Tables[0]
110-
}
111-
catch {
112-
Write-Exception $_
113-
throw "Unable to query $($s) - $($db.name)"
114-
continue
115-
}
116-
}
117-
}
118-
}
119-
END{
120-
#Sanitize output into array of custom objects, not DataRow objects
121-
Write-Verbose 'Sanitizing outupt, converting DataRow to custom PSObject.'
122-
$output = @()
123-
foreach($row in $outputraw){
124-
$outrow = [ordered]@{'SqlServer'=$row.SqlServer;`
125-
'DatabaseName'=$row.DBName;`
126-
'FileName'=$row.FileName;`
127-
'FileGroup'=$row.FileGroup;`
128-
'PhysicalName'=$row.PhysicalName;`
129-
'UsedSpaceMB'=$row.UsedSpaceMB;`
130-
'FreeSpaceMB'=$row.FreeSpaceMB;`
131-
'FileSizeMB'=$row.FileSizeMB;`
132-
'PercentUsed'=$row.PercentUSed}
133-
$output += New-Object psobject -Property $outrow
134-
}
135-
136-
137-
return $output
138-
}
75+
[object[]]$SqlServer,
76+
[System.Management.Automation.PSCredential]$SqlCredential,
77+
[switch]$IncludeSystemDBs)
78+
79+
DynamicParam { if ($SqlServer) { return Get-ParamSqlDatabases -SqlServer $SqlServer[0] -SqlCredential $SourceSqlCredential } }
80+
81+
BEGIN
82+
{
83+
$outputraw = @()
84+
$sql = "SELECT
85+
@@SERVERNAME as SqlServer
86+
,DB_NAME() as DBName
87+
,f.name AS [FileName]
88+
,fg.name AS [Filegroup]
89+
,f.physical_name AS [PhysicalName]
90+
,CAST(CAST(FILEPROPERTY(f.name, 'SpaceUsed') AS int)/128.0 AS DECIMAL(15,2)) as [UsedSpaceMB]
91+
,CAST(f.size/128.0 - CAST(FILEPROPERTY(f.name, 'SpaceUsed') AS int)/128.0 AS DECIMAL(15,2)) AS [FreeSpaceMB]
92+
,CAST((f.size/128.0) AS DECIMAL(15,2)) AS [FileSizeMB]
93+
,CAST((FILEPROPERTY(f.name, 'SpaceUsed')/(f.size/1.0)) * 100 as DECIMAL(15,2)) as [PercentUsed]
94+
FROM sys.database_files AS f WITH (NOLOCK)
95+
LEFT OUTER JOIN sys.filegroups AS fg WITH (NOLOCK)
96+
ON f.data_space_id = fg.data_space_id"
97+
98+
$databases = $psboundparameters.Databases
99+
$exclude = $psboundparameters.Exclude
100+
}
101+
102+
PROCESS
103+
{
104+
foreach ($s in $SqlServer)
105+
{
106+
#For each SQL Server in collection, connect and get SMO object
107+
Write-Verbose "Connecting to $s"
108+
$server = Connect-SqlServer $s -SqlCredential $SqlCredential
109+
#If IncludeSystemDBs is true, include systemdbs
110+
#only look at online databases (Status equal normal)
111+
try
112+
{
113+
if ($databases.length -gt 0)
114+
{
115+
$dbs = $server.Databases | Where-Object { $databases -contains $_.Name }
116+
}
117+
elseif ($IncludeSystemDBs)
118+
{
119+
$dbs = $server.Databases | Where-Object { $_.status -eq 'Normal' }
120+
}
121+
else
122+
{
123+
$dbs = $server.Databases | Where-Object { $_.status -eq 'Normal' -and $_.IsSystemObject -eq 0 }
124+
}
125+
126+
if ($exclude.length -gt 0)
127+
{
128+
$dbs = $dbs | Where-Object { $exclude -notcontains $_.Name }
129+
}
130+
}
131+
catch
132+
{
133+
Write-Exception $_
134+
throw "Unable to gather dbs for $($s.name)"
135+
continue
136+
}
137+
138+
foreach ($db in $dbs)
139+
{
140+
try
141+
{
142+
Write-Verbose "Querying $($s) - $($db.name)."
143+
#Execute query against individual database and add to output
144+
$outputraw += ($db.ExecuteWithResults($sql)).Tables[0]
145+
}
146+
catch
147+
{
148+
Write-Exception $_
149+
throw "Unable to query $($s) - $($db.name)"
150+
continue
151+
}
152+
}
153+
}
154+
}
155+
END
156+
{
157+
#Sanitize output into array of custom objects, not DataRow objects
158+
Write-Verbose 'Sanitizing outupt, converting DataRow to custom PSObject.'
159+
$output = @()
160+
foreach ($row in $outputraw)
161+
{
162+
$outrow = [ordered]@{
163+
'SqlServer' = $row.SqlServer;`
164+
'DatabaseName' = $row.DBName;`
165+
'FileName' = $row.FileName;`
166+
'FileGroup' = $row.FileGroup;`
167+
'PhysicalName' = $row.PhysicalName;`
168+
'UsedSpaceMB' = $row.UsedSpaceMB;`
169+
'FreeSpaceMB' = $row.FreeSpaceMB;`
170+
'FileSizeMB' = $row.FileSizeMB;`
171+
'PercentUsed' = $row.PercentUSed
172+
}
173+
$output += New-Object psobject -Property $outrow
174+
}
175+
176+
177+
return $output
178+
}
139179
}

0 commit comments

Comments
 (0)