Functions/update-WITSqlrecord.ps1
<#
.Synopsis Updaterar eller lägger till en post skapad av objektet .DESCRIPTION $Object Med de fält som ska kolla om de ändrats $Recordset Det filtrerade recordsetet med posten som ska ändras eller skapas $IDguidfieldname Nyckel guid fält namn Om null finns ett id räknar fält obs $Skipfields Sträng med Fältnamn komma separerade $newpost Switch Returnerar Status 'New eller Changed eller Same' och IDguid .EXAMPLE Example of how to use this cmdlet .EXAMPLE Another example of how to use this cmdlet #> class WITsqlrecordstate { [string]$State [string]$Guid } function Update-WITsqlrecord { [CmdletBinding()] [Alias()] Param ( # Param1 help description [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)] $Object, [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)] $Recordset, $IDguidfieldname, $Skipfields, [Switch]$newpost ) Begin { } Process { if ($Recordset.eof -eq $true -or $newpost) { $F_Changed = $true $F_New = $true $Recordset.addnew() if ($IDguidfieldname -ne $null) { $Recordset.Fields.Item($IDguidfieldname).Value = New-WITguid $f_guid = $Recordset.Fields.Item($IDguidfieldname).Value } } else { $F_Changed = $false $F_New = $false } $fields = $Object | Get-Member | where membertype -like '*Property' | select -ExpandProperty name foreach ($field_name in $fields) { if ("$Skipfields," -notlike "*$field_name,*") { $objectvalue = [string]$Object.$field_name if ($objectvalue -eq $null) { $objectvalue = '' } if ($Recordset.Fields.Item($field_name).Value -ne $objectvalue) { if ($F_Changed -eq $false) { $Recordset.editmode() $F_Changed = $true if ($IDguidfieldname -ne $null) { $f_guid = $Recordset.Fields.Item($IDguidfieldname).Value } } $Recordset.Fields.Item($field_name).Value = $objectvalue } else { } } } #for fields } #Process End { if ($F_New -eq $true) { $state = 'New' } elseif ($F_Changed -eq $true) { $state = 'Changed' } else { if ($IDguidfieldname -ne $null) { $f_guid = $Recordset.Fields.Item($IDguidfieldname).Value } else {$f_guid = $null} $state = 'Same' } $recordstate = [WITsqlrecordstate]::new() $recordstate.Guid = $f_guid $recordstate.State = $state return $recordstate } } |