Skip to content

Commit 7c0298a

Browse files
committed
More Strictmode Fixes
Mostly due to SMO not instantiating objects with counts of 1, AKA the Count Property is missing unless there are 2 or more objects, but general fixes found by StrictMode
1 parent e4b2f5f commit 7c0298a

18 files changed

+355
-206
lines changed

Scripts/01_Server_Appliance.ps1

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ Set-StrictMode -Version latest;
4646

4747
# Import-Module "sqlps" -DisableNameChecking -erroraction SilentlyContinue
4848
Import-Module ".\LoadSQLSMO"
49-
Import-Module ".\Get-ProductKey.psm1"
5049
LoadSQLSMO
5150

5251

@@ -68,8 +67,6 @@ Write-Output "Server $SQLInstance"
6867
$WinServer = ($SQLInstance -split {$_ -eq "," -or $_ -eq "\"})[0]
6968

7069

71-
# Test Get Product-Key
72-
$ProdKey = Get-ProductKey -Computername $winserver
7370

7471
# Server connection check
7572
[string]$serverauth = "win"
@@ -121,10 +118,6 @@ if(!(test-path -path $fullfolderPath))
121118
}
122119

123120

124-
# Load SMO Assemblies
125-
Import-Module ".\LoadSQLSmo.psm1"
126-
LoadSQLSMO
127-
128121
# Set Local Vars
129122
[string]$server = $SQLInstance
130123

@@ -228,34 +221,31 @@ $mystring | out-file $fullFileName -Encoding ascii -Append
228221
$mystring = "OS Platform: " +$srv.Platform
229222
$mystring | out-file $fullFileName -Encoding ascii -Append
230223

231-
$mystring = "OS Product Key: " +$ProdKey.ProductKey
232-
$mystring | out-file $fullFileName -Encoding ascii -Append
233-
234-
235224

236225
# Turn off default Error Handler for WMI
237226
$old_ErrorActionPreference = $ErrorActionPreference
238227
$ErrorActionPreference = 'SilentlyContinue'
239228

240-
$mystring = Get-WmiObjectclass Win32_OperatingSystem -ComputerName $server | select Name, BuildNumber, BuildType, CurrentTimeZone, InstallDate, SystemDrive, SystemDevice, SystemDirectory
229+
$mystring2 = Get-WmiObjectclass Win32_OperatingSystem -ComputerName $server | select Name, BuildNumber, BuildType, CurrentTimeZone, InstallDate, SystemDrive, SystemDevice, SystemDirectory
241230

242231
# Reset default PS error handler
243232
$ErrorActionPreference = $old_ErrorActionPreference
244233

245-
if ($mystring -ne $null)
234+
try
246235
{
247-
Write-output ("OS Host Name: {0} " -f $mystring.Name)| out-file $fullFileName -Encoding ascii -Append
248-
Write-output ("OS BuildNumber: {0} " -f $mystring.BuildNumber)| out-file $fullFileName -Encoding ascii -Append
249-
Write-output ("OS Buildtype: {0} " -f $mystring.BuildType)| out-file $fullFileName -Encoding ascii -Append
250-
Write-output ("OS CurrentTimeZone: {0}" -f $mystring.CurrentTimeZone)| out-file $fullFileName -Encoding ascii -Append
251-
Write-output ("OS InstallDate: {0} " -f $mystring.InstallDate)| out-file $fullFileName -Encoding ascii -Append
252-
Write-output ("OS SystemDrive: {0} " -f $mystring.SystemDrive)| out-file $fullFileName -Encoding ascii -Append
253-
Write-output ("OS SystemDevice: {0} " -f $mystring.SystemDevice)| out-file $fullFileName -Encoding ascii -Append
254-
Write-output ("OS SystemDirectory: {0} " -f $mystring.SystemDirectory)| out-file $fullFileName -Encoding ascii -Append
236+
Write-output ("OS Host Name: {0} " -f $mystring2.Name)| out-file $fullFileName -Encoding ascii -Append
237+
Write-output ("OS BuildNumber: {0} " -f $mystring2.BuildNumber)| out-file $fullFileName -Encoding ascii -Append
238+
Write-output ("OS Buildtype: {0} " -f $mystring2.BuildType)| out-file $fullFileName -Encoding ascii -Append
239+
Write-output ("OS CurrentTimeZone: {0}" -f $mystring2.CurrentTimeZone)| out-file $fullFileName -Encoding ascii -Append
240+
Write-output ("OS InstallDate: {0} " -f $mystring2.InstallDate)| out-file $fullFileName -Encoding ascii -Append
241+
Write-output ("OS SystemDrive: {0} " -f $mystring2.SystemDrive)| out-file $fullFileName -Encoding ascii -Append
242+
Write-output ("OS SystemDevice: {0} " -f $mystring2.SystemDevice)| out-file $fullFileName -Encoding ascii -Append
243+
Write-output ("OS SystemDirectory: {0} " -f $mystring2.SystemDirectory)| out-file $fullFileName -Encoding ascii -Append
255244
}
256-
else
245+
catch
257246
{
258-
Write-output "WMI Call to Win32_OperatingSystem class failed "| out-file $fullFileName -Encoding ascii -Append
247+
Write-output "Error getting OS specs via WMI - WMI/firewall issue?"| out-file $fullFileName -Encoding ascii -Append
248+
Write-output "Error getting OS specs via WMI - WMI/firewall issue?"
259249
}
260250

261251
" " | out-file $fullFileName -Encoding ascii -Append
@@ -265,68 +255,67 @@ else
265255
$old_ErrorActionPreference = $ErrorActionPreference
266256
$ErrorActionPreference = 'SilentlyContinue'
267257

268-
$mystring = Get-WmiObject -class Win32_Computersystem -ComputerName $server | select manufacturer
258+
$mystring3 = Get-WmiObject -class Win32_Computersystem -ComputerName $server | select manufacturer
269259

270260
# Reset default PS error handler
271261
$ErrorActionPreference = $old_ErrorActionPreference
272262

273-
if ($mystring -ne $null)
263+
try
274264
{
275-
Write-output ("HW Manufacturer: {0} " -f $mystring.Manufacturer)| out-file $fullFileName -Encoding ascii -Append
265+
Write-output ("HW Manufacturer: {0} " -f $mystring3.Manufacturer)| out-file $fullFileName -Encoding ascii -Append
276266
}
277-
else
267+
catch
278268
{
279-
Write-output "WMI Call to Win32_Computersystem class failed "| out-file $fullFileName -Encoding ascii -Append
269+
Write-output "Error getting Hardware specs via WMI - WMI/firewall issue? "| out-file $fullFileName -Encoding ascii -Append
270+
Write-output "Error getting Hardware specs via WMI - WMI/firewall issue? "
280271
}
281272

273+
" " | out-file $fullFileName -Encoding ascii -Append
274+
282275
# Turn off default Error Handler for WMI
283276
$old_ErrorActionPreference = $ErrorActionPreference
284277
$ErrorActionPreference = 'SilentlyContinue'
285278

286-
$mystring = Get-WmiObjectclass Win32_processor -ComputerName $server | select Name,NumberOfCores,NumberOfLogicalProcessors
279+
$mystring4 = Get-WmiObjectclass Win32_processor -ComputerName $server | select Name,NumberOfCores,NumberOfLogicalProcessors
287280

288281
# Reset default PS error handler
289282
$ErrorActionPreference = $old_ErrorActionPreference
290283

291-
if ($mystring -ne $null)
284+
try
292285
{
293-
Write-output ("HW Processor: {0} " -f $mystring.Name)| out-file $fullFileName -Encoding ascii -Append
294-
Write-Output ("HW CPUs: {0}" -f $mystring.NumberOfLogicalProcessors)| out-file $fullFileName -Encoding ascii -Append
295-
Write-output ("HW Cores: {0}" -f $mystring.NumberOfCores)| out-file $fullFileName -Encoding ascii -Append
286+
Write-output ("HW Processor: {0} " -f $mystring4.Name)| out-file $fullFileName -Encoding ascii -Append
287+
Write-Output ("HW CPUs: {0}" -f $mystring4.NumberOfLogicalProcessors)| out-file $fullFileName -Encoding ascii -Append
288+
Write-output ("HW Cores: {0}" -f $mystring4.NumberOfCores)| out-file $fullFileName -Encoding ascii -Append
296289
}
297-
else
290+
catch
298291
{
299-
Write-output "WMI Call to Win32_processor class failed "| out-file $fullFileName -Encoding ascii -Append
292+
Write-output "Error getting CPU specs via WMI - WMI/Firewall issue? "| out-file $fullFileName -Encoding ascii -Append
293+
Write-output "Error getting CPU specs via WMI - WMI/Firewall issue? "
300294
}
301295

302-
<#
303-
Use WMI: 2008 doesnt have System Mainboard String in the Log
304-
$mystring = $srv.ReadErrorLog(0) | where-object {$_.Text -like "System*"} |select text
305-
"HW Mainboard/Model: " + $mystring.text | out-file $fullFileName -Encoding ascii -Append
306-
#>
307-
296+
" " | out-file $fullFileName -Encoding ascii -Append
308297

309-
$mystring = "`r`nSQL Build reference: http://sqlserverbuilds.blogspot.com/ "
310-
$mystring| out-file $fullFileName -Encoding ascii -Append
298+
$mystring5 = "`r`nSQL Build reference: http://sqlserverbuilds.blogspot.com/ "
299+
$mystring5| out-file $fullFileName -Encoding ascii -Append
311300

312-
$mystring = "`r`nSQL Build reference: http://sqlserverupdates.com/ "
313-
$mystring| out-file $fullFileName -Encoding ascii -Append
301+
$mystring5 = "`r`nSQL Build reference: http://sqlserverupdates.com/ "
302+
$mystring5| out-file $fullFileName -Encoding ascii -Append
314303

315304

316-
$mystring = "`r`nMore Detailed Diagnostic Queries here:`r`nhttp://www.sqlskills.com/blogs/glenn/sql-server-diagnostic-information-queries-for-september-2015"
317-
$mystring| out-file $fullFileName -Encoding ascii -Append
305+
$mystring5 = "`r`nMore Detailed Diagnostic Queries here:`r`nhttp://www.sqlskills.com/blogs/glenn/sql-server-diagnostic-information-queries-for-september-2015"
306+
$mystring5| out-file $fullFileName -Encoding ascii -Append
318307

319308
# Dump out loaded DLLs
320309
$mySQLquery = "select * from sys.dm_os_loaded_modules order by name"
321310

322311
# connect correctly
323312
if ($serverauth -eq "win")
324313
{
325-
$sqlresults = Invoke-SqlCmd -ServerInstance $SQLInstance -Query $mySQLquery -QueryTimeout 10 -erroraction SilentlyContinue
314+
$sqlresults2 = Invoke-SqlCmd -ServerInstance $SQLInstance -Query $mySQLquery -QueryTimeout 10 -erroraction SilentlyContinue
326315
}
327316
else
328317
{
329-
$sqlresults = Invoke-SqlCmd -ServerInstance $SQLInstance -Query $mySQLquery -Username $myuser -Password $mypass -QueryTimeout 10 -erroraction SilentlyContinue
318+
$sqlresults2 = Invoke-SqlCmd -ServerInstance $SQLInstance -Query $mySQLquery -Username $myuser -Password $mypass -QueryTimeout 10 -erroraction SilentlyContinue
330319
}
331320

332321
# Create some CSS for help in column formatting during HTML exports
@@ -368,7 +357,7 @@ if(!(test-path -path "$fullfolderPath\HTMLReport.css"))
368357
}
369358

370359
$RunTime = Get-date
371-
$sqlresults | select file_version, product_version, debug, patched, prerelease, private_build, special_build, language, company, description, name `
360+
$sqlresults2 | select file_version, product_version, debug, patched, prerelease, private_build, special_build, language, company, description, name `
372361
| ConvertTo-Html -PostContent "<h3>Ran on : $RunTime</h3>" -PreContent "<h1>$SqlInstance</H1><H2>Loaded DLLs</h2>" -CSSUri "HtmlReport.css" | Set-Content "$fullfolderPath\02_Loaded_Dlls.html"
373362

374363

@@ -378,20 +367,16 @@ $mySQLquery2= "dbcc tracestatus()"
378367
# connect correctly
379368
if ($serverauth -eq "win")
380369
{
381-
$sqlresults2 = Invoke-SqlCmd -ServerInstance $SQLInstance -Query $mySQLquery2 -QueryTimeout 10 -erroraction SilentlyContinue
370+
$sqlresults3 = Invoke-SqlCmd -ServerInstance $SQLInstance -Query $mySQLquery2 -QueryTimeout 10 -erroraction SilentlyContinue
382371
}
383372
else
384373
{
385-
$sqlresults2 = Invoke-SqlCmd -ServerInstance $SQLInstance -Query $mySQLquery2 -Username $myuser -Password $mypass -QueryTimeout 10 -erroraction SilentlyContinue
374+
$sqlresults3 = Invoke-SqlCmd -ServerInstance $SQLInstance -Query $mySQLquery2 -Username $myuser -Password $mypass -QueryTimeout 10 -erroraction SilentlyContinue
386375
}
387376

388-
if ($sqlresults2 -eq $null)
389-
{
390-
391-
}
392-
else
377+
if ($sqlresults3 -ne $null)
393378
{
394-
$sqlresults2 | select TraceFlag, Status, Global, Session | ConvertTo-Html -PostContent "<h3>Ran on : $RunTime</h3>" -PreContent "<h1>$SqlInstance</H1><H2>Trace Flags</h2>" -CSSUri "HtmlReport.css" | Set-Content "$fullfolderPath\03_Trace_Flags.html"
379+
$sqlresults3 | select TraceFlag, Status, Global, Session | ConvertTo-Html -PostContent "<h3>Ran on : $RunTime</h3>" -PreContent "<h1>$SqlInstance</H1><H2>Trace Flags</h2>" -CSSUri "HtmlReport.css" | Set-Content "$fullfolderPath\03_Trace_Flags.html"
395380
}
396381

397382

Scripts/01_Server_Logins.ps1

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ function CopyObjectsToFiles($objects, $outDir) {
172172
$schemaPrefix = $o.Schema + "."
173173
}
174174
}
175-
catch
176-
{
177-
}
175+
catch {}
178176

179177
$fixedOName = $o.name.replace('\','_')
180178
$scripter.Options.FileName = $outDir + $schemaPrefix + $fixedOName + ".sql"
@@ -242,7 +240,7 @@ $logins = $srv.Logins
242240

243241
foreach ($Login in $Logins)
244242
{
245-
243+
246244
# Skip non-Domain logins that look like Domain Logins (contain "\")
247245
if ($Login.Name -like "NT SERVICE\*") {continue}
248246
if ($Login.Name -like "NT AUTHORITY\*") {continue}
@@ -300,7 +298,7 @@ foreach ($Login in $Logins)
300298
}
301299

302300

303-
# Windows Users
301+
# Windows Domain Users
304302
if ($Login.LoginType -eq "WindowsUser")
305303
{
306304
Write-Output ("Scripting out: {0}" -f $Login.Name)

Scripts/01_Server_Resource_Governor.ps1

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,9 @@ function CopyObjectsToFiles($objects, $outDir) {
105105

106106
$schemaPrefix = ""
107107

108-
try
109-
{
110108
if ($o.Schema -ne $null -and $o.Schema -ne "") {
111109
$schemaPrefix = $o.Schema + "."
112110
}
113-
}
114-
catch
115-
{
116-
}
117111

118112
$fixedOName = $o.name.replace('\','_')
119113
$scripter.Options.FileName = $outDir + $schemaPrefix + $fixedOName + ".sql"
@@ -194,39 +188,47 @@ $scripter.Options.ToFileOnly = $true
194188
$scripter.Options.WithDependencies = $false
195189
$scripter.Options.XmlIndexes = $true
196190

197-
#Export Resource Governor Pools and Workgroups
191+
# Output Folder
198192
Set-Location $BaseFolder
199193
$output_path = "$BaseFolder\$SQLInstance\01 - Server Resource Governor\"
200194
if(!(test-path -path $output_path))
201195
{
202196
mkdir $output_path | Out-Null
203197
}
204198

205-
#pools
206-
$pools = $srv.ResourceGovernor.ResourcePools | where-object -FilterScript {$_.Name -notin "internal","default"}
207-
if ($pools -ne $null)
199+
try
208200
{
209-
CopyObjectsToFiles $pools $output_path
210-
}
201+
# Pools
202+
$pools = $srv.ResourceGovernor.ResourcePools | where-object -FilterScript {$_.Name -notin "internal","default"}
203+
if ($pools.Count -gt 0)
204+
{
205+
CopyObjectsToFiles $pools $output_path
206+
}
211207

212-
#Workgroups
213-
foreach ($pool in $pools)
214-
{
215-
216-
#Put Workgroups in parent pool's folder
217-
$pool_path = "$BaseFolder\$SQLInstance\01 - Server Resource Governor\"+$pool.Name+"\"
218-
if(!(test-path -path $pool_path))
208+
# Workgroups
209+
foreach ($pool in $pools)
210+
{
211+
212+
#Put Workgroups in parent pool's folder
213+
$pool_path = "$BaseFolder\$SQLInstance\01 - Server Resource Governor\"+$pool.Name+"\"
214+
if(!(test-path -path $pool_path))
215+
{
216+
mkdir $pool_path | Out-Null
217+
}
218+
219+
#Workgroup
220+
$workloadgroups = $pool.WorkloadGroups
221+
foreach ($workloadgroup in $workloadgroups)
219222
{
220-
mkdir $pool_path | Out-Null
223+
CopyObjectsToFiles $workloadgroup $pool_path
221224
}
222-
223-
#Workgroup
224-
$workloadgroups = $pool.WorkloadGroups
225-
foreach ($workloadgroup in $workloadgroups)
226-
{
227-
CopyObjectsToFiles $workloadgroup $pool_path
228-
}
229-
230-
}
225+
226+
}
227+
}
228+
catch
229+
{
230+
$fullfolderpath = "$BaseFolder\$SQLInstance\"
231+
echo null > "$fullfolderpath\01 - Server Resource Governor - not setup or enabled.txt"
232+
}
231233

232234
set-location $BaseFolder

Scripts/01_Server_Settings.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,13 @@ else
178178
$sqlresults = Invoke-SqlCmd -ServerInstance $SQLInstance -Query $mySQLquery -Username $myuser -Password $mypass -QueryTimeout 10 -erroraction SilentlyContinue
179179
}
180180

181-
# Export it
181+
# Export BPE
182+
if ($?)
183+
{
182184
if ($sqlresults.state -eq 5)
183185
{
186+
Write-Output "Buffer-Pool Extensions:"
187+
184188
$strExport = "
185189
ALTER SERVER CONFIGURATION SET BUFFER POOL EXTENSION
186190
ON
@@ -191,6 +195,7 @@ if ($sqlresults.state -eq 5)
191195

192196
$strExport | out-file "$output_path\Buffer_Pool_Extension.sql" -Encoding ascii
193197
}
198+
}
194199

195200
set-location $BaseFolder
196201

0 commit comments

Comments
 (0)