set-sqllogfiles.ps1

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<#PSScriptInfo
 
.VERSION 1.0
 
.GUID edba8ac2-e4d8-471a-b4ca-7fae59ee3ac4
 
.AUTHOR Rob Sewell
 
.DESCRIPTION
   Uses SMO to set the number of Log files for a single or group of SQL Servers
       
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS SQL Error log SQL Server
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
#>

function Set-SQLLogFiles
 {
 <#
.Synopsis
   Sets the number of Log files for a single or group of SQL Servers
 
.DESCRIPTION
   Uses SMO to set the number of Log files for a single or group of SQL Servers
       
.PARAMETER Instances
The SQL Server Instance or an array of instances to change
 
.PARAMETER Number
The number of logfiles to set
 
.EXAMPLE
   Set-SQLLogFiles -instances Fade2Black -Number 20
 
   Sets the number of SQL Server log files to 20 on the instance Fade2Black
.EXAMPLE
   $Servers = 'Fade2Black','JusticeForAll','MasterOfPuppets'
   Set-SQLLogFiles -instances $Servers -Number 20
 
   Sets the number of SQL Server log files to 20 on the instances 'Fade2Black','JusticeForAll','MasterOfPuppets'
.NOTES
    Author - Rob Sewell SQLDBAWithABeard.com
#>


 param(
 [object]$instances,
 [ValidateRange(0,99)]
 [int]$Number
 )
 [void][reflection.assembly]::LoadWithPartialName( 'Microsoft.SqlServer.Smo' )
    foreach($Server in $Instances)
    {
        try
        {
            $srv = New-Object Microsoft.SqlServer.Management.Smo.Server $server
            $srv.Settings.NumberOfLogFiles = $Number
            $srv.Alter()
        }
        catch
        {
            Write-Warning "Eailed to set Log Fiels on $Server - Run `$error[0]|fl -force to find the error"
            continue
        }
    }
 }