Scripts/Get-WOLComputer.ps1


Function Get-WOLComputer {

    <#
 
     .SYNOPSIS
     Get the computer and MAC address from the database
 
     .DESCRIPTION
     Get the computer and MAC address from the database
 
     .PARAMETER ComputerName
     The computer name to query
 
     .EXAMPLE
     Get-WOLComputer -ComputerName LabPC2064
 
     .EXAMPLE
     Get-WOLComputer -ComputerName %
 
     This will display all the records in the database
 
     .NOTES
     N/A
 
     .LINK
     N/A
 
    #>


    [CmdletBinding ()]

    Param (

        [Parameter (Mandatory = $True,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter computer name'
                   )
        ]

        [String[]]$ComputerName

    )

    BEGIN {

        $Path = $MyInvocation.MyCommand.Module.ModuleBase + '\System.Data.SQLite.dll'

        Add-Type -Path $Path

        $DBConnect = New-Object -TypeName System.Data.SQLite.SQLiteConnection
        $DBConnect.ConnectionString = "Data Source = $env:ALLUSERSPROFILE\PSWakeOnLAN\WOLDatabase.db3"
        $DBConnect.Open()

    }

    PROCESS {

        ForEach ($Computer In $ComputerName) {

            $SQL = $DBConnect.CreateCommand()
            $SQL.CommandText = "SELECT * FROM Computers WHERE Computer LIKE ""%$Computer%"""

            $Adapter = New-Object -TypeName System.Data.SQLite.SQLiteDataAdapter $SQL

            $Data = New-Object System.Data.DataSet
            $Records = $Adapter.Fill($Data)

            Write-Verbose -Message "Records found = $Records"
            ForEach ($Item In $Data.Tables.Rows) {

                [PsCustomObject]@{

                    'ComputerName' = $Item.Computer
                    'MACAddress' = $Item.MAC

                }

            }

        }

    }

    END {

        $SQL.Dispose()
        $DBConnect.Close()

    }

}