-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.ps1
149 lines (129 loc) · 5.4 KB
/
delete.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#####################################################
# HelloID-Conn-Prov-Target-Raet-DPIA100-Delete
#
# Version: 1.1.0
#####################################################
# Initialize default values
$c = $configuration | ConvertFrom-Json
$p = $person | ConvertFrom-Json
$aRef = $AccountReference | ConvertFrom-Json
$success = $false # Set to false at start, at the end, only when no error occurs it is set to true
$auditLogs = [System.Collections.Generic.List[PSCustomObject]]::new()
# Set debug logging
switch ($($c.isDebug)) {
$true { $VerbosePreference = 'Continue' }
$false { $VerbosePreference = 'SilentlyContinue' }
}
$InformationPreference = "Continue"
$WarningPreference = "Continue"
# used to define for export file
$useSingleFilePerPerson = [System.Convert]::ToBoolean($c.useSingleFilePerPerson)
$filePath = $c.filePath
$filenamePrefix = $c.filenamePrefix
$updateUser = $c.updateUser
$processCode = $c.processCode
$indication = $c.indication
#region Change mapping here
$currentDate = Get-Date
if ($useSingleFilePerPerson -eq $true) {
$fileName = "$($filenamePrefix)" + "$($p.ExternalId)" + ".txt"
}
else {
$fileName = "$($filenamePrefix)" + "$($currentDate.toString("yyyyMMdd"))" + ".txt"
}
$account = [PSCustomObject]@{
'objectId' = $p.ExternalId
'filePath' = "$($filePath)\$($fileName)"
'properties' = [PSCustomObject]@{
# Business Email address
'P01035' = ''
# Login name
'E02544' = ''
# Identity (for Youforce SSO)
'E02850' = ''
}
}
#endregion Change mapping here
# Troubleshooting
# $account = [PSCustomObject]@{
# 'objectId' = '12345678'
# 'filePath' = "$($filePath)\$($fileName)"
# 'properties' = [PSCustomObject]@{
# # Business Email address
# 'P01035' = ''
# # Login name
# 'E02544' = ''
# # Identity (for Youforce SSO)
# 'E02850' = ''
# }
# }
# $dryRun = $false
try {
# Build fixed length fields
$processCode = "$processCode $(" " * 3)".Substring(0, 3)
$objectId = "$($account.objectId) $(" " * 50)".Substring(0, 50)
$indication = "$indication $(" " * 1)".Substring(0, 1) # V for Variable S for Stam
$exportDate = "$($currentDate.toString("ddMMyyyy")) $(" " * 11)".Substring(0, 11)
$startDate = "$($currentDate.toString("ddMMyyyy")) $(" " * 11)".Substring(0, 11)
$creationUser = "$updateUser $(" " * 16)".Substring(0, 16)
$productionType = "NOR$($currentDate.toString("MMyyyy")) $(" " * 9)".Substring(0, 9)
$spaces = "$(" " * 30)".Substring(0, 30)
# Export data to file
try {
foreach ($property in $account.properties.PSObject.Properties) {
if (-Not($dryRun -eq $True)) {
$rubrieksCode = "$($property.Name) $(" " * 6)".Substring(0, 6)
$value = "$($property.Value) $(" " * 50)".Substring(0, 50)
$fileContent = "$processcode" + "$rubriekscode" + "$objectId" + "$indication" + "$exportDate" + "$creationUser" + "$value" + "$startDate" + "$spaces" + "$productionType"
Write-Verbose "Exporting rubriek '$($rubrieksCode)' to DPIA100 file '$($account.filePath)'. File content: $($fileContent|ConvertTo-Json)"
$fileContent | Out-File -FilePath $account.filePath -Encoding ascii -Force -Confirm:$false -Append
$auditLogs.Add([PSCustomObject]@{
Message = "Successfully exported rubriek '$($rubrieksCode)' to DPIA100 file '$($account.filePath)'"
IsError = $false
})
}
else {
Write-Warning "DryRun: Would export rubriek '$($rubrieksCode)' to DPIA100 file '$($account.filePath)'. File content: $($fileContent|ConvertTo-Json)"
}
}
}
catch {
# Clean up error variables
$verboseErrorMessage = $null
$auditErrorMessage = $null
$ex = $PSItem
# If error message empty, fall back on $ex.Exception.Message
if ([String]::IsNullOrEmpty($verboseErrorMessage)) {
$verboseErrorMessage = $ex.Exception.Message
}
if ([String]::IsNullOrEmpty($auditErrorMessage)) {
$auditErrorMessage = $ex.Exception.Message
}
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($verboseErrorMessage)"
$auditLogs.Add([PSCustomObject]@{
Message = "Error exporting rubriek '$($rubrieksCode)' to DPIA100 file '$($account.filePath)'. File content: $($fileContent|ConvertTo-Json). Error Message: $auditErrorMessage"
IsError = $True
})
}
}
finally {
# Check if auditLogs contains errors, if no errors are found, set success to true
if (-NOT($auditLogs.IsError -contains $true)) {
$success = $true
}
# Dynamically build ExportDate based on defined properties
$exportData = [PSCustomObject]::new()
foreach ($property in $account.properties.PSObject.Properties) {
$exportData | Add-Member -MemberType NoteProperty -Name "$($property.Name)" -Value "$($property.Value)" -Force
}
# Send results
$result = [PSCustomObject]@{
Success = $success
AccountReference = $aRef
AuditLogs = $auditLogs
Account = $account
# Optionally return data for use in other systems
ExportData = $exportData
}
Write-Output ($result | ConvertTo-Json -Depth 10)
}