Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 320 fix? #623

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions functions/Get-DatabaseDiskFreeSpace.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
function Get-DatabaseDiskFreeSpace {
<#
.SYNOPSIS
Returns if there is enough diskspace for the instance to grow for one cycle.

.DESCRIPTION
Returns if there is enough diskspace for the instance to grow for one cycle.
The checks is disk based, so the check will be done for each disk where the databases stores files.

.NOTES
Tags: Database, FileSize, FileGrowth
Author: Reitse Eskens (@2meterDBA)

.PARAMETER SqlInstance
The Instance for which you want to perform your check

.PARAMETER SqlCredential
Credentials to connect to the SQL Server instance if the calling user doesn't have permission

.EXAMPLE
Get-DatabaseDiskFreeSpace -SqlInstance sql2016
#>

[CmdletBinding()]

param (
[parameter(ValueFromPipeline)]
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential
)


begin {
<#
Step one. Get de free diskspace from the disks where Sql Server is parking it's files.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get the free

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the comments in the code :-) Thank you :-)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo fixed

The line will get the data from the chosen instance. Next the result is filtered down to just the drive letter for the summation later on.
The -unique is used to limit the results. We get a result line for each database file, but all we need is a drive and the free space.

#>

$DiskFreeSpace = Get-DbaDbFile -SqlInstance REITSE-PC\VANTAGE | SELECT @{label='DriveLetter';Expression={$_.PhysicalName.substring(0,3)}}, VolumeFreeSpace |Sort-Object -Property Driveletter -Unique

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please can this have a try catch

<#
Step two. Determine per drive letter how much growth can be expected.
Same concept as the first step, but now we're looking at the file growth.
#>

$FileGrowth = Get-DbaDbFile -SqlInstance REITSE-PC\VANTAGE | SELECT @{label='DriveLetter';Expression={$_.PhysicalName.substring(0,3)}}, NextGrowthEventSize

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please can this have a try catch

<#
Step three, summation of the disk growth
#>


$calc = $FileGrowth | Group-Object -Property Driveletter | ForEach-Object -Process {
$Sum = $_.group | measure -Sum -Property NextGrowthEventSize
[pscustomobject]@{DriveLetter=$_.Name ; value = $Sum.Sum}
}

<#
Step three and a bit, because the summation results in something else than GB, the result is rebuilt to gigabytes
shaneis marked this conversation as resolved.
Show resolved Hide resolved
#>


$CalcInGB = $calc | select DriveLetter, @{name="GrowthInGB" ; Expression={[math]::Round($_.value/1GB, 2)}}

<#
Now for the interesting part. Time to compare the results!
For each line in the disk free space results, the expected growth will be checked.
If the drives are the same, the comparison will take place and the result will be shown.
#>


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super, can we create a pscustomobject here so that we have something like

ComputerName | SQLInstance | DriveLetter | FreeSpace | Growth | GrowthAchievable
Beard BeardInstance | Q | 100000 | 500000 | true

$DiskFreeSpace | ForEach-Object -Process {
if($_.DriveLetter -cin $CalcInGB.DriveLetter)
{
$localDisk = $_.DriveLetter
$localFileSize = $_.VolumeFreeSpace
$CalcInGB | ForEach-Object -Process {
If($_.DriveLetter -eq $localDisk)
{
if($_.GrowthInGB -ge $localFileSize )
{
Write-Host $localDisk 'Don't panic, don't Panic. Time to grow the this disk mr Mainwairing'
}
else
{
Write-Host $localDisk 'Fall in chaps, if you please... yes yes yes you look very smart'
}

}
}
}
else
{
Write-Host $_.DriveLetter 'Pike! You stupid Boy!'
}
}
}
}