-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSSC-SQLToolBox.ps1
More file actions
61 lines (54 loc) · 2.2 KB
/
SSC-SQLToolBox.ps1
File metadata and controls
61 lines (54 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function Get-FreeSpace{
param([string] $HostName = ($env:COMPUTERNAME))
Get-WmiObject win32_volume -computername $hostname | `
Where-Object {$_.drivetype -eq 3} | `
Sort-Object name | `
Format-Table name,@{l="Size(GB)";e={($_.capacity/1gb).ToString("F2")}},`
@{l="Free Space(GB)";e={($_.freespace/1gb).ToString("F2")}},`
@{l="% Free";e={(($_.Freespace/$_.Capacity)*100).ToString("F2")}}
}
function Get-SQLBackups{
param([Parameter(Mandatory=$true)][string]$Directory
,[ValidateSet('Full','Differential','Log')]$Type
,[int] $OlderThanHours=0
,[int] $EarlierThanHours=0)
if(Test-Path $Directory){
$extension = switch($Type){
Full{'.bak'}
Differential{'.dff'}
Log{'.trn'}
}
if($OlderThanHours -gt 0){
$files = Get-ChildItem $Directory -Filter "*$extension" -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddHours(-$OlderThanHours)}
}
elseif($EarlierThanHours -gt 0){
$files = Get-ChildItem $Directory -Filter "*$extension" -Recurse | Where-Object {$_.LastWriteTime -gt (Get-Date).AddHours(-$EarlierThanHours)}
}
else{
$files = Get-ChildItem $Directory -Filter "*$extension" -Recurse
}
return $files
}
else{
Write-Warning "'$Directory' is not a valid path."
}
}
function Test-SQLConnection{
param([parameter(mandatory=$true)][string[]] $Instances)
$return = @()
foreach($InstanceName in $Instances){
$row = New-Object –TypeName PSObject –Prop @{'InstanceName'=$InstanceName;'StartupTime'=$null}
try{
$check=Invoke-Sqlcmd -ServerInstance $InstanceName -Database TempDB -Query "SELECT @@SERVERNAME as Name,Create_Date FROM sys.databases WHERE name = 'TempDB'" -ErrorAction Stop -ConnectionTimeout 3
$row.InstanceName = $check.Name
$row.StartupTime = $check.Create_Date
}
catch{
#do nothing on the catch
}
finally{
$return += $row
}
}
return $return
}