Scripts/Remove-WOLComputerFromDatabase.ps1


Function Remove-WOLComputerFromDatabase {

    <#
 
     .SYNOPSIS
     Remove the computer from the database
 
     .DESCRIPTION
     Remove the computer from the database
 
     .PARAMETER ComputerName
     The name of the computer to remove
 
     .PARAMETER Force
     This switch (optional) forces the deletion of records
     without prompting for confirmation
 
     .EXAMPLE
     Remove-WOLComputerFromDatabase -ComputerName LabPC2064
 
     .EXAMPLE
     Remove-WOLComputerFromDatabase -ComputerName LabPC2064 -Force
 
     .NOTES
     Wildcards are not allowed
 
     .LINK
     N/A
 
    #>


    [CmdletBinding (SupportsShouldProcess = $True,
                    ConfirmImpact = 'High'
                   )

    ]

    Param (

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

        [String[]]$ComputerName,

        [Switch]$Force

    )

    BEGIN {}

    PROCESS {

        ForEach ($Computer In $ComputerName) {

            $Computer = $Computer.toUpper()

            If ($Force -and $WhatIfPreference -eq $False -or $PSCmdlet.ShouldProcess('Wake On LAN database', "Removing: $Computer")) {

                $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()

                $SQL = $DBConnect.CreateCommand()
                $SQL.CommandText = "DELETE FROM Computers WHERE Computer = ""$Computer"""
                $Records = $SQL.ExecuteNonQuery()

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

                Write-Verbose -Message "Records found = $Records"
                If ($Records -gt 0) {

                    [PsCustomObject]@{

                         'ComputerName' = $Computer
                         'Status' = 'Removed'

                    }

                }

            }

            Else {

                If ($WhatIfPreference -eq $False) {

                    [PsCustomObject]@{

                         'ComputerName' = $Computer
                         'Status' = 'Excluded'

                    }

                }

            }

        }

    }

    END {}

}