Functions/Add-WITLog.ps1



<#
.Synopsis
   Loggar ändringar med tid och datum
.DESCRIPTION
   $rsclog must be open to logg table
 
####### Sqlscript create database #################################
   SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
CREATE TABLE [dbo].[Log_Changes](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [TimeStamp] [datetime] NOT NULL,
    [Changed_ID] [nvarchar](50) NULL,
    [Changed_table] [nvarchar](127) NULL,
    [Changed_IDfieldname] [nvarchar](50) NULL,
    [Change_action] [nvarchar](10) NULL,
 CONSTRAINT [PK_Log_Changes] PRIMARY KEY CLUSTERED
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
#######################################################
 
.EXAMPLE
   $a=add-wittedlog -F_id 'Värde primary key' -F_Table 'Tabellen' -F_Fiels 'Primary key fieldname' -F_Action 'Create' -recordset $rslogg
#>

function Add-WitLog
{
    [CmdletBinding()]
    [Alias()]
    
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $F_id,
         [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=1)]
        $F_Table,
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=2)]
        $F_Field,
           [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=2)]
           [ValidateSet('Create','Update','Delete','New','Changed', ignorecase=$true)]
        $F_Action,
        $Recordset

    )

    Begin
    {
    if ($Recordset -ne $null) {
       $rsclogg=$recordset
       }
    }
    Process
    {
    if ($F_Action -eq 'New')
       {
       $act='Create'
       }
    elseif ($F_Action -eq 'Changed')
       {
       $act='Update'
       }
    else
       {
       $act=$F_Action
       }
    $rsclogg.addnew()
    $rsclogg.Fields.Item("Changed_ID").Value="$F_id"
    $rsclogg.Fields.Item("Changed_Table").Value="$F_table"
    $rsclogg.Fields.Item("Changed_IDfieldname").Value="$F_Field"
    $rsclogg.Fields.Item("Change_Action").Value="$F_Action"
    $rsclogg.Fields.Item("TimeStamp").Value =$(Get-Date -Format g)
    
    #
    }
    End
    {
    }
}