Skip to content

Commit

Permalink
Version 1.0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kendalvandyke authored Sep 17, 2018
1 parent a289723 commit 37eebf7
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Modules/LogHelper/LogHelper.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ModuleToProcess = 'LogHelper'

# Version number of this module.
ModuleVersion = '1.0.0.0'
ModuleVersion = '1.0.1.0'

# ID used to uniquely identify this module
GUID = '{9b13d1af-effb-4827-b880-17e106dac3a1}'
Expand Down
2 changes: 1 addition & 1 deletion Modules/NetworkScan/NetworkScan.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ModuleToProcess = 'NetworkScan'

# Version number of this module.
ModuleVersion = '1.0.1.0'
ModuleVersion = '1.0.2.0'

# ID used to uniquely identify this module
GUID = '{d4dba74f-b4b6-46e8-b6aa-1ba66775f3ea}'
Expand Down
142 changes: 109 additions & 33 deletions Modules/NetworkScan/NetworkScan.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ function Find-IPv4Device {
.PARAMETER ParentProgressId
If the caller is using Write-Progress then all progress information will be written using ParentProgressId as the ParentID
.PARAMETER TimeoutSeconds
Number of seconds to wait for WMI connectivity test to return before timing out.
If not provided then 30 seconds is used as the default.
.EXAMPLE
Find-IPv4Device -DNSServer automatic -DNSDomain automatic -PrivateOnly
Expand Down Expand Up @@ -415,6 +420,12 @@ function Find-IPv4Device {
[ValidateNotNull()]
[Int32]
$ParentProgressId = -1
,
[Parameter(Mandatory=$false)]
[ValidateRange(1,32767)]
[alias('Timeout')]
[Int16]
$TimeoutSeconds = 30
)
process {

Expand Down Expand Up @@ -796,7 +807,7 @@ function Find-IPv4Device {
Write-NetworkScanLog -Message "PING response from $($Device[$HashKey].IPAddress): $($Device[$HashKey].IsPingAlive)" -MessageLevel Verbose
}
}
}
}
}

# Found that in some cases an ACCESS_VIOLATION error occurs if we don't delay a little bit during each iteration
Expand Down Expand Up @@ -920,6 +931,7 @@ function Find-IPv4Device {
PowerShell = $PowerShell
Runspace = $PowerShell.BeginInvoke()
HashKey = $_.Key
StartDate = [DateTime]::Now
}
)) | Out-Null

Expand Down Expand Up @@ -947,9 +959,11 @@ function Find-IPv4Device {
# Double check that we've got a DNS Hostname. If not, get it from DNS
# If we do have a DNS Hostname that doesn't begin with the WMI Machine Name and $ResolveAliases is true, get the machine name from DNS
if (
( !$Device[$HashKey].DnsRecordName) `
-or `
( ( $ResolveAliases -eq $true) -and ($Device[$HashKey].DnsRecordName.StartsWith($HostName, 'CurrentCultureIgnoreCase') -ne $true))
!$Device[$HashKey].DnsRecordName -or
(
$ResolveAliases -eq $true -and
$Device[$HashKey].DnsRecordName.StartsWith($HostName, 'CurrentCultureIgnoreCase') -ne $true
)
) {
try {
[System.Net.Dns]::GetHostByName($HostName) | ForEach-Object {
Expand Down Expand Up @@ -977,6 +991,25 @@ function Find-IPv4Device {
}
}
}
elseif ($([DateTime]::Now).Subtract($_.StartDate).TotalSeconds -gt $TimeoutSeconds) {

$HashKey = $_.HashKey
$_.PowerShell.Stop()
$_.PowerShell.dispose()
$_.Runspace = $null
$_.PowerShell = $null

if ($Device[$HashKey].DnsRecordName) {
Write-NetworkScanLog -Message "Timeout waiting for WMI response from $($Device[$HashKey].DnsRecordName) ($($Device[$HashKey].IPAddress))" -MessageLevel Warning
Write-NetworkScanLog -Message "WMI response from $($Device[$HashKey].DnsRecordName) ($($Device[$HashKey].IPAddress)): $($Device[$HashKey].IsWmiAlive)" -MessageLevel Verbose
} elseif ($Device[$HashKey].WmiMachineName) {
Write-NetworkScanLog -Message "Timeout waiting for WMI response from $($Device[$HashKey].WmiMachineName) ($($Device[$HashKey].IPAddress))" -MessageLevel Warning
Write-NetworkScanLog -Message "WMI response from $($Device[$HashKey].WmiMachineName) ($($Device[$HashKey].IPAddress)): $($Device[$HashKey].IsWmiAlive)" -MessageLevel Verbose
} else {
Write-NetworkScanLog -Message "Timeout waiting for WMI response from $($Device[$HashKey].IPAddress): $($Device[$HashKey].IsWmiAlive)" -MessageLevel Warning
Write-NetworkScanLog -Message "WMI response from $($Device[$HashKey].IPAddress): $($Device[$HashKey].IsWmiAlive)" -MessageLevel Verbose
}
}
}

# Found that in some cases an ACCESS_VIOLATION error occurs if we don't delay a little bit during each iteration
Expand Down Expand Up @@ -1247,7 +1280,7 @@ function Find-SqlServerService {
$RunspacePool.Open()

# Create an empty collection to hold the Runspace jobs
$Runspaces = New-Object System.Collections.ArrayList
$Runspaces = New-Object System.Collections.ArrayList


$ScanCount = 0
Expand Down Expand Up @@ -1277,10 +1310,8 @@ function Find-SqlServerService {
$DomainName = $null
$ServiceTypeName = $null
$ServiceName = $null
$Server = $null
$Port = $null
$IsDynamicPort = $false
$ServiceInstallDate = $null
$ServiceStartDate = $null

$StdRegProv = $null
Expand Down Expand Up @@ -1361,31 +1392,72 @@ function Find-SqlServerService {
# Get the port number for SQL Server Services
#if ($_.Type -eq $ManagedServiceTypeEnum::SqlServer) {
if ($ServiceTypeName -ieq 'SQL Server') {
$ServiceName = $_.Name

$ServiceIpAddress = $null
$Port = $null
$IsDynamicPort = $null
$ServiceName = if ($IsNamedInstance -eq $true) { $InstanceName } else { $_.Name }
$ManagedComputer.ServerInstances | Where-Object { $_.Name -ieq $ServiceName } | ForEach-Object {

$_.ServerProtocols | Where-Object { $_.Name -ieq 'tcp' } | ForEach-Object {
$_.ServerProtocols | Where-Object {
$_.Name -ieq 'tcp' -and
$_.IsEnabled -eq $true
} | ForEach-Object {

# First get the port for all IP Addresses
$_.IPAddresses | Where-Object { $_.Name -ieq 'ipall' } | ForEach-Object {
$Port = $_.IPAddressProperties['TcpPort'].Value
if (-not $Port) {
$Port = $_.IPAddressProperties['TcpDynamicPorts'].Value
$IsDynamicPort = $true
} else {
$IsDynamicPort = $false
# If listening on all IPs then get the "IPAll" port info
# Otherwise get port info for an IP that's enabled and active

if ($_.ProtocolProperties['ListenOnAllIPs'].Value -eq $true) {

$_.IPAddresses | Where-Object { $_.Name -ieq 'ipall' } | ForEach-Object {
$Port = $_.IPAddressProperties['TcpPort'].Value
if (-not $Port) {
$Port = $_.IPAddressProperties['TcpDynamicPorts'].Value
$IsDynamicPort = $true
} else {
$IsDynamicPort = $false
}
}
}

# Then check to see if the port is overridden for the specific IP Address provided
$_.IPAddresses | Where-Object { ($_.IPAddress -ieq $ServiceIpAddress) -and ($_.IPAddressProperties['Active'].Value -eq $true) -and ($_.IPAddressProperties['Enabled'].Value -eq $true) } | ForEach-Object {
$Port = $_.IPAddressProperties['TcpPort'].Value
if (-not $Port) {
$Port = $_.IPAddressProperties['TcpDynamicPorts'].Value
$IsDynamicPort = $true
} else {
$IsDynamicPort = $false
} else {

# Start with 127.0.0.1 first in case that's the only IP that's enabled
$_.IPAddresses | Where-Object {
$_.IPAddressProperties['Active'].Value -eq $true -and
$_.IPAddressProperties['Enabled'].Value -eq $true -and
$_.IPAddress.AddressFamily -ieq 'InterNetwork' -and
$_.IPAddress -ieq '127.0.0.1'
} | Select-Object -First 1 | ForEach-Object {

$ServiceIpAddress = $_.IPAddress.ToString()
$Port = $_.IPAddressProperties['TcpPort'].Value

if (-not $Port) {
$Port = $_.IPAddressProperties['TcpDynamicPorts'].Value
$IsDynamicPort = $true
} else {
$IsDynamicPort = $false
}
}

# Now try and see if there's a non-loopback IP enabled
$_.IPAddresses | Where-Object {
$_.IPAddressProperties['Active'].Value -eq $true -and
$_.IPAddressProperties['Enabled'].Value -eq $true -and
$_.IPAddress.AddressFamily -ieq 'InterNetwork' -and
$_.IPAddress -ine '127.0.0.1'
} | Select-Object -First 1 | ForEach-Object {

$ServiceIpAddress = $_.IPAddress.ToString()
$Port = $_.IPAddressProperties['TcpPort'].Value

if (-not $Port) {
$Port = $_.IPAddressProperties['TcpDynamicPorts'].Value
$IsDynamicPort = $true
} else {
$IsDynamicPort = $false
}
}
}
}

Expand All @@ -1410,12 +1482,6 @@ function Find-SqlServerService {
$ServiceStartDate = $null
}

# # Get the Service Install Date
# Get-WmiObject -Namespace root\CIMV2 -Class Win32_Service -Filter "DisplayName = '$($_.DisplayName)'" -Property CreationDate -ComputerName $IpAddress | ForEach-Object {
# $ServiceStartDate = $_.CreationDate
# }


Write-Output (
New-Object -TypeName psobject -Property @{
ComputerName = $ComputerName
Expand Down Expand Up @@ -1486,7 +1552,17 @@ function Find-SqlServerService {
# Determine if instance is clustered
$IsClusteredInstance = $null
$ClusterName = [String]::Empty
$ServiceIpAddress = $IpAddress

# If $ComputerName is the local host then use the loopback IP, otherwise use $IpAddress
if (
$ComputerName -ieq $env:COMPUTERNAME -or
$ComputerName.StartsWith([String]::Concat($env:COMPUTERNAME, '.'), [System.StringComparison]::InvariantCultureIgnoreCase)
) {
$ServiceIpAddress = '127.0.0.1'
} else {
$ServiceIpAddress = $IpAddress
}


# Get the TCP port number for SQL Server Services
$Port = ($StdRegProv.GetStringValue($HKEY_LOCAL_MACHINE,"$RegistryKeyRootPath\MSSQLServer\SuperSocketNetLib\Tcp",'TcpDynamicPorts')).sValue
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3754,6 +3754,10 @@ function Get-SqlConnection {
$Instance = '(local)'
,
[Parameter(Mandatory=$false)]
[System.Net.IPAddress]
$IpAddress = $null
,
[Parameter(Mandatory=$false)]
[Int]
$Port = $null
,
Expand Down Expand Up @@ -3788,7 +3792,11 @@ function Get-SqlConnection {
$SQLConnection = New-Object -TypeName System.Data.SqlClient.SqlConnection
$SQLConnectionBuilder = New-Object -TypeName system.Data.SqlClient.SqlConnectionStringBuilder

$SQLConnectionBuilder.psBase.DataSource = if ($Port) { "$Instance,$Port" } else { $Instance }
$SQLConnectionBuilder.psBase.DataSource = if ($IpAddress) {
if ($Port) { "$($IpAddress.ToString()),$Port" } else { $IpAddress.ToString() }
} else {
if ($Port) { "$Instance,$Port" } else { $Instance }
}
$SQLConnectionBuilder.psBase.InitialCatalog = $Database

if ($PSCmdlet.ParameterSetName -eq 'SQLAuthentication') {
Expand Down Expand Up @@ -11126,6 +11134,11 @@ function Get-SqlServerDatabaseEngineInformation {
.PARAMETER InstanceName
The database engine instance to connect to.

.PARAMETER IpAddress
The IP Address to use when connecting to the database engine.

If not provided, the value provided for InstanceName will be used to connect instead.

.PARAMETER Port
The port to use when connecting to the database engine.

Expand Down Expand Up @@ -11267,6 +11280,10 @@ function Get-SqlServerDatabaseEngineInformation {
$InstanceName = '(local)'
,
[Parameter(Mandatory=$false)]
[System.Net.IPAddress]
$IpAddress = $null
,
[Parameter(Mandatory=$false)]
[int]
$Port = $null
,
Expand Down Expand Up @@ -11341,9 +11358,9 @@ function Get-SqlServerDatabaseEngineInformation {


if ($PSCmdlet.ParameterSetName -eq 'SQLAuthentication') {
$Connection = Get-SqlConnection -Instance $InstanceName -Port $Port -Username $Username -Password $Password
$Connection = Get-SqlConnection -Instance $InstanceName -IpAddress $IpAddress -Port $Port -Username $Username -Password $Password
} else {
$Connection = Get-SqlConnection -Instance $InstanceName -Port $Port
$Connection = Get-SqlConnection -Instance $InstanceName -IpAddress $IpAddress -Port $Port
}


Expand Down Expand Up @@ -11726,4 +11743,12 @@ function Get-SqlServerDatabaseEngineInformation {
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended') | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SQLWMIManagement') | Out-Null
}
}

# Now check which SMO assemblies are loaded and set $SmoMajorVersion to the lowest version
[System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.FullName -ilike 'Microsoft.SqlServer.SMO, Version=*' } | ForEach-Object {
if ($_.GetName().Version.Major -lt $SmoMajorVersion) {
$SmoMajorVersion = $_.GetName().Version.Major
Write-SqlServerDatabaseEngineInformationLog -Message "Multiple versions of Microsoft.SqlServer.SMO are loaded; reverting to the lowest version to avoid problems" -MessageLevel Warning
}
}
Binary file modified Modules/SqlServerInventory/SqlServerInventory.psd1
Binary file not shown.
Loading

0 comments on commit 37eebf7

Please sign in to comment.