-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExport-NotesGroupsToXML.ps1
237 lines (213 loc) · 10.4 KB
/
Export-NotesGroupsToXML.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
param(
[parameter(Mandatory = $true, Position = 0)][ValidateNotNullOrEmpty()][string]$DominoServer,
[parameter(Mandatory = $true, Position = 1)][ValidateNotNullOrEmpty()][string]$Database,
[parameter(Mandatory = $true, Position = 2)][ValidateNotNullOrEmpty()][string]$XmlFilePath,
[parameter(Mandatory = $true, Position = 3)][ValidateNotNullOrEmpty()][string]$LogPath,
[parameter(Mandatory = $false)][string]$NotesPW = "",
[parameter(Mandatory = $false)][switch]$LogToScreen
)
function New-LogMsg {
param(
[parameter(Mandatory = $true, Position = 0)][string]$msg,
[parameter(Mandatory = $true, Position = 1)][ValidateSet('Info', 'Error', 'Warning')][string]$level
)
if (-not (Test-Path $LogPath)) {
New-Item $LogPath -ItemType Directory
}
$logFilePath = Join-Path -Path $LogPath -ChildPath $_LogFileName
$msgToLog = "[$(Get-Date -Format "yyyy-MM-dd HH-mm-ss")]`t[$level]`t[$msg]"
Out-File -FilePath $logFilePath -Encoding utf8 -InputObject $msgToLog -Append -Force
If ($LogToScreen) {
Switch ($level) {
'Info' {Write-Host $msgToLog}
'Error' {Write-Host $msgToLog -ForegroundColor red}
'Warning' {Write-Host $msgToLog -ForegroundColor yellow}
}
}
}
function Get-GroupType {
param(
[int]$groupTypeId
)
$groupType = $null
switch ($groupTypeId) {
0 { $groupType = "MultiPurpose" }
1 { $groupType = "MailOnly" }
2 { $groupType = "AccessControlListOnly" }
3 { $groupType = "DenyListOnly" }
4 { $groupType = "ServersOnly" }
}
return $groupType
}
#init xml
function Add-XmlAttribute {
param (
[String]$AttributeName,
[String]$AttributeValue,
[System.Xml.XmlNode]$ParentNode,
[System.Xml.XmlDocument]$Document
)
$attribute = $Document.CreateAttribute($AttributeName)
$attribute.Value = $AttributeValue
$ParentNode.Attributes.Append($attribute) | out-null
}
function Add-XmlElementValue {
param(
[string]$ElementName,
[string]$ElementValue,
[System.Xml.XmlNode]$ParentNode,
[System.Xml.XmlDocument]$Document
)
$xmlValueElement = $Document.CreateNode("element", $ElementName, $null)
Add-XmlAttribute -AttributeName "Value" -AttributeValue $ElementValue -ParentNode $xmlValueElement -Document $Document
$ParentNode.AppendChild($xmlValueElement) | out-null
}
function Add-XmlArray {
param (
[Array]$Values,
[String]$ElementName,
[System.Xml.XmlNode]$ParentNode,
[System.Xml.XmlDocument]$Document
)
$parentElement = $xmlDocument.CreateNode("element", "$($ElementName)s", $null)
$ParentNode.AppendChild($parentElement) | out-null
foreach ($value in $Values) {
Add-XmlElementValue -ElementName $ElementName -ElementValue $value -ParentNode $parentElement -Document $Document
}
}
[xml]$xmlDocument = New-Object System.Xml.XmlDocument
$xmlDocument.AppendChild($xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", $null)) | out-null
$initComment = @"
Notes Group export
Author: David Espin, Marc Jonas
Script Date: 04/2020
Generated: $(Get-Date)
"@
$xmlDocument.AppendChild($xmlDocument.CreateComment($initComment)) | out-null
$xmlRootElement = $xmlDocument.createNode("element", "DominoEnvironment", $null)
Add-XmlAttribute -AttributeName "Server" -AttributeValue $DominoServer -ParentNode $xmlRootElement -Document $xmlDocument
Add-XmlAttribute -AttributeName "Database" -AttributeValue $Database -ParentNode $xmlRootElement -Document $xmlDocument
$xmlDocument.AppendChild($xmlRootElement) | out-null
$xmlGroupsElement = $xmlDocument.createNode("element", "Groups", $null)
$xmlRootElement.AppendChild($xmlGroupsElement) | out-null
#end init xml
$_LogFileName = "Export-NotesGroupsToXML_$(Get-Date -Format "yyyy-MM-dd_HH-mm-ss").log"
if ([Environment]::Is64BitProcess -eq $true) {
New-LogMsg "PowerShell is a 64 bit process. 32 bit process needed. Exiting script." Error
exit
} #because you have a 64-bit PowerShell
$notesSession = $null
$notesAdressbook = $null
$groupsView = $null
$usersView = $null
$groupDocument = $null
try {
$notesSession = New-Object -ComObject Lotus.NotesSession #open the Notes Session
$notesSession.Initialize($NotesPW) #if no password is provided, Notes will ask for the password
$notesAdressbook = $notesSession.GetDatabase($DominoServer, $Database, 0)
}
catch {
New-LogMsg "Could not establish Notes session." Error
New-LogMsg "Error Message: $($_.Exception.Message)" Error
exit
}
try {
$groupsView = $notesAdressbook.GetView("(`$VIMGroups)")
}
catch {
New-LogMsg "Could not get groups view." Error
New-LogMsg "Error Message: $($_.Exception.Message)" Error
exit
}
try {
$usersView = $notesAdressbook.GetView("(`$VIMPEOPLE)")
}
catch {
New-LogMsg "Could not get users view." Error
New-LogMsg "Error Message: $($_.Exception.Message)" Error
exit
}
try {
$groupDocument = $groupsView.GetFirstDocument()
}
catch {
New-LogMsg "Could not get first group document." Error
New-LogMsg "Error Message: $($_.Exception.Message)" Error
exit
}
while ($null -ne $groupDocument) {
try {
$groupName = $groupDocument.GetItemValue("ListName")[0]
$groupSMTP = $groupDocument.GetItemValue("InternetAddress")[0]
$groupType = Get-GroupType -GroupTypeId ($groupDocument.GetItemValue("GroupType")[0])
New-LogMsg -level Info -msg "Started processing group $groupName."
$xmlGroupElement = $xmlDocument.CreateNode("element", "Group", $null)
Add-XmlAttribute -AttributeName "Name" -AttributeValue $groupName -ParentNode $xmlGroupElement -Document $xmlDocument
Add-XmlAttribute -AttributeName "SmtpAddress" -AttributeValue $groupSMTP -ParentNode $xmlGroupElement -Document $xmlDocument
Add-XmlAttribute -AttributeName "Type" -AttributeValue $groupType -ParentNode $xmlGroupElement -Document $xmlDocument
$xmlGroupsElement.AppendChild($xmlGroupElement) | out-null
$groupMembers = $groupDocument.GetItemValue("Members")
if ($null -ne $groupMembers) {
$xmlInternalMembersElement = $xmlDocument.CreateNode("element", "InternalMembers", $null)
$xmlGroupMembersElement = $xmlDocument.CreateNode("element", "GroupMembers", $null)
$xmlUnresolvedMembersElement = $xmlDocument.CreateNode("element", "UnresolvedMembers", $null)
$xmlExternalMembersElement = $xmlDocument.CreateNode("element", "ExternalMembers", $null)
$xmlGroupElement.AppendChild($xmlInternalMembersElement) | out-null
$xmlGroupElement.AppendChild($xmlGroupMembersElement) | out-null
$xmlGroupElement.AppendChild($xmlUnresolvedMembersElement) | out-null
$xmlGroupElement.AppendChild($xmlExternalMembersElement) | out-null
foreach ($member in $groupMembers) {
New-LogMsg -level Info -msg "Working on member $member."
$notesName = $notesSession.CreateName($member)
if ($notesName.IsHierarchical) {
# case internal member
$personDocument = $usersView.GetDocumentByKey($notesName.Abbreviated, $true)
if ($null -ne $personDocument) {
$primarySMTPAddress = $personDocument.GetItemValue("InternetAddress")[0]
if([String]::IsNullOrEmpty($primarySMTPAddress)) {
New-LogMsg "Member $member has no primary SMTP address" Warning
}
$notesShortNames = $personDocument.GetItemValue("ShortName")
$notesFullNames = $personDocument.GetItemValue("FullName")
$xmlInternalMemberElement = $xmlDocument.CreateNode("element", "InternalMember", $null)
Add-XmlAttribute -AttributeName "PrimarySMTPAddress" -AttributeValue $primarySMTPAddress -ParentNode $xmlInternalMemberElement -Document $xmlDocument
Add-XmlArray -Values $notesShortNames -ElementName "ShortName" -ParentNode $xmlInternalMemberElement -Document $xmlDocument
Add-XmlArray -Values $notesFullNames -ElementName "FullName" -ParentNode $xmlInternalMemberElement -Document $xmlDocument
$xmlInternalMembersElement.AppendChild($xmlInternalMemberElement) | out-null
}
else {
# user could not be resolved
Add-XmlElementValue -ElementName "UnresolvedMember" -ElementValue $member -ParentNode $xmlUnresolvedMembersElement -Document $xmlDocument
}
}
else {
# group or external?
$groupAsMemberDocument = $groupsView.GetDocumentByKey($member, $true)
if ($null -ne $groupAsMemberDocument) {
# member is a group
$groupType = Get-GroupType -GroupTypeId $groupAsMemberDocument.GetItemValue("GroupType")[0]
$xmlGroupMemberElement = $xmlDocument.CreateNode("element", "GroupMember", $null)
Add-XmlAttribute -AttributeName "Name" -AttributeValue $member -ParentNode $xmlGroupMemberElement -Document $xmlDocument
Add-XmlAttribute -AttributeName "Type" -AttributeValue $groupType -ParentNode $xmlGroupMemberElement -Document $xmlDocument
}
elseif ($member -like "*@*") {
#member is external
Add-XmlElementValue -ElementName "ExternalMember" -ElementValue $member -ParentNode $xmlExternalMembersElement -Document $xmlDocument
}
}
}
}
else {
New-LogMsg -level Info -msg "Group $groupName has no members."
}
New-LogMsg -level Info -msg "Finished processing group $groupName."
}
catch {
New-LogMsg "Error processing Group." Error
New-LogMsg "Error Message: $($_.Exception.Message)" Error
}
finally {
$xmlDocument.Save($XmlFilePath)
}
$groupDocument = $groupsView.GetNextDocument($groupDocument)
}