Skip to content

Commit 3437940

Browse files
committed
Improved error handling in several modules
Fixed bugs reported since version 1.0.0.0 Added 11 new & updated 3 existing database engine assessment checks
1 parent 7ec246d commit 3437940

10 files changed

Lines changed: 1490 additions & 1353 deletions

File tree

Get-SqlServerInventoryToClixml.ps1

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -185,67 +185,52 @@
185185
.\Convert-SqlServerInventoryClixmlToExcel.ps1
186186
187187
#>
188-
[cmdletBinding(SupportsShouldProcess=$True, DefaultParametersetName='computername_WindowsAuthentication')]
188+
[cmdletBinding(SupportsShouldProcess=$True, DefaultParametersetName='computername')]
189189
param(
190-
[Parameter(Mandatory=$true, ParameterSetName='dns_SQLAuthentication', HelpMessage='DNS Server(s)')]
191-
[Parameter(Mandatory=$true, ParameterSetName='dns_WindowsAuthentication', HelpMessage='DNS Server(s)')]
190+
[Parameter(Mandatory=$true, ParameterSetName='dns', HelpMessage='DNS Server(s)')]
192191
[alias('dns')]
193192
[ValidatePattern('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^auto$|^automatic$')]
194193
[string[]]
195194
$DnsServer = 'automatic'
196195
,
197-
[Parameter(Mandatory=$false, ParameterSetName='dns_SQLAuthentication', HelpMessage='DNS Domain Name')]
198-
[Parameter(Mandatory=$false, ParameterSetName='dns_WindowsAuthentication', HelpMessage='DNS Domain Name')]
196+
[Parameter(Mandatory=$false, ParameterSetName='dns', HelpMessage='DNS Domain Name')]
199197
[alias('domain')]
200198
[string]
201199
$DnsDomain = 'automatic'
202200
,
203-
[Parameter(Mandatory=$true, ParameterSetName='subnet_SQLAuthentication', HelpMessage='Subnet (in CIDR notation)')]
204-
[Parameter(Mandatory=$true, ParameterSetName='subnet_WindowsAuthentication', HelpMessage='Subnet (in CIDR notation)')]
201+
[Parameter(Mandatory=$true, ParameterSetName='subnet', HelpMessage='Subnet (in CIDR notation)')]
205202
[ValidatePattern('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[\\/]\d{1,2}$|^auto$|^automatic$')]
206203
[string[]]
207204
$Subnet = 'automatic'
208205
,
209-
[Parameter(Mandatory=$true, ParameterSetName='computername_SQLAuthentication', HelpMessage='Computer Name(s)')]
210-
[Parameter(Mandatory=$true, ParameterSetName='computername_WindowsAuthentication', HelpMessage='Computer Name(s)')]
206+
[Parameter(Mandatory=$true, ParameterSetName='computername', HelpMessage='Computer Name(s)')]
211207
[alias('computer')]
212208
[string[]]
213209
$ComputerName
214210
,
215-
[Parameter(Mandatory=$false, ParameterSetName='dns_SQLAuthentication')]
216-
[Parameter(Mandatory=$false, ParameterSetName='dns_WindowsAuthentication')]
217-
[Parameter(Mandatory=$false, ParameterSetName='subnet_SQLAuthentication')]
218-
[Parameter(Mandatory=$false, ParameterSetName='subnet_WindowsAuthentication')]
211+
[Parameter(Mandatory=$false, ParameterSetName='dns')]
212+
[Parameter(Mandatory=$false, ParameterSetName='subnet')]
219213
[ValidatePattern('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[\\/]\d{1,2}$')]
220214
[string[]]
221215
$ExcludeSubnet
222216
,
223-
[Parameter(Mandatory=$false, ParameterSetName='dns_SQLAuthentication')]
224-
[Parameter(Mandatory=$false, ParameterSetName='dns_WindowsAuthentication')]
217+
[Parameter(Mandatory=$false, ParameterSetName='dns')]
225218
[ValidatePattern('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[\\/]\d{1,2}$')]
226219
[string[]]
227220
$LimitSubnet
228221
,
229-
[Parameter(Mandatory=$false, ParameterSetName='dns_SQLAuthentication')]
230-
[Parameter(Mandatory=$false, ParameterSetName='dns_WindowsAuthentication')]
231-
[Parameter(Mandatory=$false, ParameterSetName='subnet_SQLAuthentication')]
232-
[Parameter(Mandatory=$false, ParameterSetName='subnet_WindowsAuthentication')]
222+
[Parameter(Mandatory=$false, ParameterSetName='dns')]
223+
[Parameter(Mandatory=$false, ParameterSetName='subnet')]
233224
[string[]]
234225
$ExcludeComputerName
235226
,
236-
[Parameter(Mandatory=$true, ParameterSetName='dns_SQLAuthentication') ]
237-
[Parameter(Mandatory=$true, ParameterSetName='subnet_SQLAuthentication') ]
238-
[Parameter(Mandatory=$true, ParameterSetName='computername_SQLAuthentication') ]
239-
[ValidateNotNull()]
227+
[Parameter(Mandatory=$false)]
240228
[System.String]
241-
$Username
229+
$Username = $null
242230
,
243-
[Parameter(Mandatory=$true, ParameterSetName='dns_SQLAuthentication') ]
244-
[Parameter(Mandatory=$true, ParameterSetName='subnet_SQLAuthentication') ]
245-
[Parameter(Mandatory=$true, ParameterSetName='computername_SQLAuthentication') ]
246-
[ValidateNotNull()]
231+
[Parameter(Mandatory=$false)]
247232
[System.String]
248-
$Password
233+
$Password = $null
249234
,
250235
[Parameter(Mandatory=$false)]
251236
[ValidateRange(1,100)]
@@ -387,43 +372,24 @@ $ParameterHash = @{
387372
IncludeDatabaseObjectPermissions = $IncludeDatabaseObjectPermissions
388373
IncludeDatabaseObjectInformation = $IncludeDatabaseObjectInformation
389374
IncludeDatabaseSystemObjects = $IncludeDatabaseSystemObjects
375+
Username = $Username
376+
Password = $Password
390377
}
391378

392379
switch ($PsCmdlet.ParameterSetName) {
393-
'dns_SQLAuthentication' {
394-
$ParameterHash.Add('Username',$Username)
395-
$ParameterHash.Add('Password',$Password)
396-
$ParameterHash.Add('DnsServer',$DnsServer)
397-
$ParameterHash.Add('DnsDomain',$DnsDomain)
398-
if ($ExcludeSubnet) { $ParameterHash.Add('ExcludeSubnet',$ExcludeSubnet) }
399-
if ($LimitSubnet) { $ParameterHash.Add('IncludeSubnet',$LimitSubnet) }
400-
if ($ExcludeComputerName) { $ParameterHash.Add('ExcludeComputerName',$ExcludeComputerName) }
401-
}
402-
'dns_WindowsAuthentication' {
380+
'dns' {
403381
$ParameterHash.Add('DnsServer',$DnsServer)
404382
$ParameterHash.Add('DnsDomain',$DnsDomain)
405383
if ($ExcludeSubnet) { $ParameterHash.Add('ExcludeSubnet',$ExcludeSubnet) }
406384
if ($LimitSubnet) { $ParameterHash.Add('IncludeSubnet',$LimitSubnet) }
407385
if ($ExcludeComputerName) { $ParameterHash.Add('ExcludeComputerName',$ExcludeComputerName) }
408386
}
409-
'subnet_SQLAuthentication' {
410-
$ParameterHash.Add('Username',$Username)
411-
$ParameterHash.Add('Password',$Password)
387+
'subnet' {
412388
$ParameterHash.Add('Subnet',$Subnet)
413389
if ($ExcludeSubnet) { $ParameterHash.Add('ExcludeSubnet',$ExcludeSubnet) }
414390
if ($ExcludeComputerName) { $ParameterHash.Add('ExcludeComputerName',$ExcludeComputerName) }
415391
}
416-
'subnet_WindowsAuthentication' {
417-
$ParameterHash.Add('Subnet',$Subnet)
418-
if ($ExcludeSubnet) { $ParameterHash.Add('ExcludeSubnet',$ExcludeSubnet) }
419-
if ($ExcludeComputerName) { $ParameterHash.Add('ExcludeComputerName',$ExcludeComputerName) }
420-
}
421-
'computername_SQLAuthentication' {
422-
$ParameterHash.Add('Username',$Username)
423-
$ParameterHash.Add('Password',$Password)
424-
$ParameterHash.Add('ComputerName',$ComputerName)
425-
}
426-
'computername_WindowsAuthentication' {
392+
'computername' {
427393
$ParameterHash.Add('ComputerName',$ComputerName)
428394
}
429395
}

Modules/LogHelper/LogHelper.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function Write-Log {
182182
# If we're not writing to the log then we need some way to alert on warnings and errors
183183
# so write to the warning and error streams
184184
if ($MessageLevel -ieq 'warning') {
185-
Write-Warning $Message
185+
Write-Warning $Message -WarningAction Continue
186186
}
187187
elseif ($MessageLevel -ieq 'error') {
188188
Write-Error $Message -ErrorAction Continue

Modules/NetworkScan/NetworkScan.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
ModuleToProcess = 'NetworkScan'
55

66
# Version number of this module.
7-
ModuleVersion = '1.0.0.0'
7+
ModuleVersion = '1.0.1.0'
88

99
# ID used to uniquely identify this module
1010
GUID = '{d4dba74f-b4b6-46e8-b6aa-1ba66775f3ea}'

Modules/NetworkScan/NetworkScan.psm1

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ function Get-ActiveDirectoryDnsConfiguration {
5757

5858
foreach ($Computer in $ComputerName) {
5959

60-
61-
Write-Verbose "Processing $Computer"
62-
6360
$ComputerDomain = Get-ComputerDomain -ComputerName $Computer
6461

6562
Get-WmiObject -Namespace root\CIMV2 -Class Win32_NetworkAdapterConfiguration `
@@ -153,7 +150,7 @@ function Get-DnsARecord {
153150
$DnsServerIPAddress | ForEach-Object {
154151
try {
155152
$DnsServer = $_
156-
Get-WMIObject -Namespace root\MicrosoftDNS -Class MicrosoftDNS_AType -Computer $_ -Filter "ContainerName= '$Domain'" |
153+
Get-WMIObject -Namespace root\MicrosoftDNS -Class MicrosoftDNS_AType -Computer $_ -Filter "ContainerName= '$Domain'" -ErrorAction Stop |
157154
Where-Object {$_.DomainName -ine $_.OwnerName } |
158155
Select-Object OwnerName, IPAddress |
159156
ForEach-Object {
@@ -1319,7 +1316,7 @@ function Find-SqlServerService {
13191316
if ($_.AdvancedProperties['CLUSTERED'].Value -eq $true) {
13201317
$IsClusteredInstance = $true
13211318

1322-
Get-WmiObject -Namespace root\CIMV2 -Class Win32_ComputerSystem -Property Domain -ComputerName $IpAddress | ForEach-Object {
1319+
Get-WmiObject -Namespace root\CIMV2 -Class Win32_ComputerSystem -Property Domain -ComputerName $IpAddress -ErrorAction Stop | ForEach-Object {
13231320
$DomainName = $_.Domain
13241321
}
13251322

@@ -1401,8 +1398,13 @@ function Find-SqlServerService {
14011398

14021399
# Get the Service Start Date (if it's got a Process ID greater than than 0)
14031400
if ($_.ProcessId -gt 0) {
1404-
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Process -Filter "ProcessId = '$($_.ProcessId)'" -Property CreationDate -ComputerName $IpAddress | ForEach-Object {
1405-
$ServiceStartDate = $_.ConvertToDateTime($_.CreationDate)
1401+
try {
1402+
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Process -Filter "ProcessId = '$($_.ProcessId)'" -Property CreationDate -ComputerName $IpAddress -ErrorAction Stop | ForEach-Object {
1403+
$ServiceStartDate = $_.ConvertToDateTime($_.CreationDate)
1404+
}
1405+
}
1406+
catch {
1407+
$ServiceStartDate = $null
14061408
}
14071409
} else {
14081410
$ServiceStartDate = $null
@@ -1464,7 +1466,7 @@ function Find-SqlServerService {
14641466

14651467
# Use the WMI Registry provider to access the registry on $ComputerName
14661468
# For info on using this WMI class see http://msdn.microsoft.com/en-us/library/windows/desktop/aa393664(v=vs.85).aspx
1467-
$StdRegProv = Get-WmiObject -Namespace root\DEFAULT -Query "select * FROM meta_class WHERE __Class = 'StdRegProv'" -ComputerName $ComputerName
1469+
$StdRegProv = Get-WmiObject -Namespace root\DEFAULT -Query "select * FROM meta_class WHERE __Class = 'StdRegProv'" -ComputerName $ComputerName -ErrorAction Stop
14681470

14691471
# Iterate through installed instances of the Database Engine (which includes the SQL Agent)
14701472
$($StdRegProv.GetMultiStringValue($HKEY_LOCAL_MACHINE,'SOFTWARE\Microsoft\Microsoft SQL Server','InstalledInstances')).sValue | ForEach-Object {
@@ -1496,7 +1498,10 @@ function Find-SqlServerService {
14961498
}
14971499

14981500
# Get Service Information
1499-
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Service -Filter "DisplayName = '$DisplayName'" -Property PathName,StartMode,ProcessId,State,StartName,Description -ComputerName $IpAddress | ForEach-Object {
1501+
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Service `
1502+
-Filter "DisplayName = '$DisplayName'" `
1503+
-Property PathName,StartMode,ProcessId,State,StartName,Description -ComputerName $IpAddress -ErrorAction Stop |
1504+
ForEach-Object {
15001505
$PathName = $_.PathName
15011506
$StartMode = $_.StartMode
15021507
$ProcessId = $_.ProcessId
@@ -1507,8 +1512,13 @@ function Find-SqlServerService {
15071512

15081513
# Get the Service Start Date (if it's got a Process ID greater than than 0)
15091514
if ($ProcessId -gt 0) {
1510-
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Process -Filter "ProcessId = '$ProcessId'" -Property CreationDate -ComputerName $IpAddress | ForEach-Object {
1511-
$ServiceStartDate = $_.ConvertToDateTime($_.CreationDate)
1515+
try {
1516+
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Process -Filter "ProcessId = '$ProcessId'" -Property CreationDate -ComputerName $IpAddress -ErrorAction Stop | ForEach-Object {
1517+
$ServiceStartDate = $_.ConvertToDateTime($_.CreationDate)
1518+
}
1519+
}
1520+
catch {
1521+
$ServiceStartDate = $null
15121522
}
15131523
} else {
15141524
$ServiceStartDate = $null
@@ -1596,7 +1606,10 @@ function Find-SqlServerService {
15961606
}
15971607

15981608
# Get Service Information
1599-
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Service -Filter "DisplayName = '$DisplayName'" -Property PathName,StartMode,ProcessId,State,StartName,Description -ComputerName $IpAddress | ForEach-Object {
1609+
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Service `
1610+
-Filter "DisplayName = '$DisplayName'" `
1611+
-Property PathName,StartMode,ProcessId,State,StartName,Description -ComputerName $IpAddress -ErrorAction Stop |
1612+
ForEach-Object {
16001613
$PathName = $_.PathName
16011614
$StartMode = $_.StartMode
16021615
$ProcessId = $_.ProcessId
@@ -1607,8 +1620,13 @@ function Find-SqlServerService {
16071620

16081621
# Get the Service Start Date (if it's got a Process ID greater than than 0)
16091622
if ($ProcessId -gt 0) {
1610-
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Process -Filter "ProcessId = '$ProcessId'" -Property CreationDate -ComputerName $IpAddress | ForEach-Object {
1611-
$ServiceStartDate = $_.ConvertToDateTime($_.CreationDate)
1623+
try {
1624+
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Process -Filter "ProcessId = '$ProcessId'" -Property CreationDate -ComputerName $IpAddress -ErrorAction Stop | ForEach-Object {
1625+
$ServiceStartDate = $_.ConvertToDateTime($_.CreationDate)
1626+
}
1627+
}
1628+
catch {
1629+
$ServiceStartDate = $null
16121630
}
16131631
} else {
16141632
$ServiceStartDate = $null
@@ -1657,7 +1675,7 @@ function Find-SqlServerService {
16571675
# You can't have more than one instance of each in 2000
16581676
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Service `
16591677
-Filter "(DisplayName = 'MSSQLServerOLAPService') or (DisplayName = 'Microsoft Search') or (DisplayName = 'ReportServer')" `
1660-
-Property DisplayName,PathName,StartMode,ProcessId,State,StartName,Description -ComputerName $IpAddress |
1678+
-Property DisplayName,PathName,StartMode,ProcessId,State,StartName,Description -ComputerName $IpAddress -ErrorAction Stop |
16611679
ForEach-Object {
16621680

16631681
$DisplayName = $_.DisplayName
@@ -1670,8 +1688,13 @@ function Find-SqlServerService {
16701688

16711689
# Get the Service Start Date (if it's got a Process ID greater than than 0)
16721690
if ($ProcessId -gt 0) {
1673-
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Process -Filter "ProcessId = '$ProcessId'" -Property CreationDate -ComputerName $IpAddress | ForEach-Object {
1674-
$ServiceStartDate = $_.ConvertToDateTime($_.CreationDate)
1691+
try {
1692+
Get-WmiObject -Namespace root\CIMV2 -Class Win32_Process -Filter "ProcessId = '$ProcessId'" -Property CreationDate -ComputerName $IpAddress -ErrorAction Stop | ForEach-Object {
1693+
$ServiceStartDate = $_.ConvertToDateTime($_.CreationDate)
1694+
}
1695+
}
1696+
catch {
1697+
$ServiceStartDate = $null
16751698
}
16761699
} else {
16771700
$ServiceStartDate = $null
Binary file not shown.

0 commit comments

Comments
 (0)