View/Test-ServerAllFunctions.Task.ps1

Function Global:Test-ServerAllFunctions{
    $TestResult = @()
    $TestResult += (Test-WsusServer)
    $TestResult += (Test-IisWsusPoolPath)
    $TestResult += (Test-SqlServerInstancePath)
    If ($False -notin $TestResult){
        $MainWindow.Dispatcher.Invoke({
            Write-Host "すべてのテストに成功しました"
            $MainWindow.FindName("AllConnectionSuccessfully").Visibility = [System.Windows.Visibility]::Visible
        })
    }
}

Function Global:Test-WsusServer{
    Try{
        Get-WsusServer -Name $CurrentConfig.Wsus.Server -PortNumber $CurrentConfig.Wsus.Port | Out-Null
        Write-Verbose "$($CurrentConfig.Wsus.Server):$($CurrentConfig.Wsus.Port) へ接続できました"
        $MainWindow.Dispatcher.Invoke({
            $MainWindow.FindName("WsusServerConnectionFailure").Visibility = [System.Windows.Visibility]::Collapsed
        })
        Return $True
    }
    Catch{
        $MainWindow.Dispatcher.Invoke({
            Write-Warning "$($CurrentConfig.Wsus.Server):$($CurrentConfig.Wsus.Port) へ接続できませんでした: $($_.Exception.Message)"
            $MainWindow.FindName("WsusServerTextBox").Focus()
            $MainWindow.FindName("WsusServerConnectionFailure").Visibility = [System.Windows.Visibility]::Visible
        })
        Return $False
    }
}


Function Global:Test-IisWsusPoolPath{
    Try{
        Import-Module WebAdministration -ErrorAction Stop
    }
    Catch{
        $MainWindow.Dispatcher.Invoke({
            Write-Warning "このスクリプトの動作に必要な WebAdministration が見つかりませんでした: $($_.Exception.Message)"
            $MainWindow.FindName("IisConnectionFailure").Visibility = [System.Windows.Visibility]::Visible
            $MainWindow.FindName("WsusPoolApplicationContainer").Visibility = [System.Windows.Visibility]::Collapsed
        })
        Return $False
    }
    
    If (-not (Test-Path $CurrentConfig.Wsus.IisWsusPoolPath)){
        $MainWindow.Dispatcher.Invoke({
            Write-Warning "CurrentConfig.Wsus.IisWsusPoolPath ($($CurrentConfig.Wsus.IisWsusPoolPath)) が見つかりませんでした"
            $MainWindow.FindName("IisConnectionFailure").Visibility = [System.Windows.Visibility]::Visible
            $MainWindow.FindName("WsusPoolApplicationContainer").Visibility = [System.Windows.Visibility]::Collapsed
        })
        Return $False
    }
    Else{
        $MainWindow.Dispatcher.Invoke({
            $MainWindow.FindName("WsusPoolApplicationContainer").Visibility = [System.Windows.Visibility]::Visible
        })
        Return $True
    }
}

Function Global:Get-WsusSqlServerName{
    If (Test-Path "HKLM:\SOFTWARE\Microsoft\Update Services\Server\Setup"){
        $ServerInstancePath = (Get-Item -Path "HKLM:\SOFTWARE\Microsoft\Update Services\Server\Setup").GetValue("SqlServerName")
        If ($ServerInstancePath -eq "MICROSOFT##WID"){
            $ServerInstancePath = "np:\\.\pipe\Microsoft##WID\tsql\query"
        }
        Return $ServerInstancePath
    }
    Return $DefaultConfig.MaintenanceSql.ServerInstancePath
}
Function Global:Get-SqlCmdPath{
    $Path = (Get-ChildItem "C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\" -Recurse -Filter "sqlcmd.exe" -File | Sort-Object FullName -Descending | Select-Object -First 1).FullName
    If ($Path -eq $Null){
        $Path = $DefaultConfig.MaintenanceSql.SqlCmdPath
    }
    Return $Path
}

Function Global:Test-SqlServerInstancePath{
    $SuccessfullyConnectedToSql = $False
    If ($Config.MaintenanceSql.SqlCmdExeMode -eq "psmodule"){
        Try{
            (Invoke-Sqlcmd -ServerInstance (Get-WsusSqlServerName) -Query "SELECT @@VERSION").Column1
            $SuccessfullyConnectedToSql = $True
        }
        Catch{}
    }
    Else{
        Try{
            If ((Start-Process (Get-SqlCmdPath) -ArgumentList "-S $(Get-WsusSqlServerName) -q ""exit(SELECT @@VERSION)""" -Wait -NoNewWindow -PassThru).ExitCode -eq -102){
                $SuccessfullyConnectedToSql = $True
            }
        }
        Catch{
            $MainWindow.Dispatcher.Invoke({
                Write-Warning "Get-SqlCmdPath ($(Get-SqlCmdPath)) が見つかりませんでした。SQLCMD ($(Get-SqlCmdPath)) が実行できるか確認してください: $($_.Exception.Message)"
                $MainWindow.FindName("SqlConnectionFailure").Visibility = [System.Windows.Visibility]::Visible
                $MainWindow.FindName("SqlContainer").Visibility = [System.Windows.Visibility]::Collapsed
            })
            Return $False
        }
    }
 
    If(-not $SuccessfullyConnectedToSql){
        $MainWindow.Dispatcher.Invoke({
            Write-Warning "データベース ($(Get-WsusSqlServerName)) が見つかりませんでした"
            $MainWindow.FindName("SqlConnectionFailure").Visibility = [System.Windows.Visibility]::Visible
            $MainWindow.FindName("SqlContainer").Visibility = [System.Windows.Visibility]::Collapsed
        })
        Return $False
    }
    $MainWindow.Dispatcher.Invoke({
        $MainWindow.FindName("SqlContainer").Visibility = [System.Windows.Visibility]::Visible
    })
    Write-Verbose "データベース ($(Get-WsusSqlServerName)) へ接続できました"
    Return $True
}