-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvert-CSUserData.ps1
153 lines (133 loc) · 5.07 KB
/
Convert-CSUserData.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
<#
.TITLE Convert-CSUserData
.VERSION 0.7
.SYNOPSIS
Extract Skype contact information from a pool Export-CSUserData and transform it for per-user import
.DESCRIPTION
Steps :
1. Export-CSUserData .\export\DocItemSet.xml
2. .\Convert-CSUserData.ps1 -InputFile .\export\DocItemSet.xml -OutPutPath .\export\
3. .\Invoke-SFBContacts.ps1 -task import -file 'c:\test\Joe@contoso.com.csv'
.EXAMPLE
#On a Single user export
Export-CS??? .\export\Joe.User.xml
#convert single user to CSV
.\Convert-CSUserData.ps1 -InputFile .\export\Joe.User.xml -OutPutPath .\export\
.\Invoke-SFBContacts.ps1 -task import -file 'c:\test\Joe@contoso.com.csv'
.EXAMPLE
#On a Pool Export
Export-CSUserData .\export\DocItemSet.xml
#convert to mutiple CSVs
.\Convert-CSUserData.ps1 -InputFile .\export\DocItemSet.xml -OutPutPath .\export\ -Base64
.\Invoke-SFBContacts.ps1 -task import -file 'c:\test\Joe@contoso.com.csv'
.Example
#Batch conversion
Foreach( $file in (dir .\Batch-05\*.xml) ) {
.\Convert-CSUserData.ps1 -InputFile $file.FullName -OutputPath '.\Batch-05'
}
.INPUTS
an xml file generated by Export-CSUserData
.OUTPUTS
a CSV file or mutiple CSV files with contact information per user
.NOTES
be aware that you handle the exported information with due care as it will contain PII
.FUNCTIONALITY
Tool to aid in migration scenarios from on-premises to O365 where the contact information
is lost for the end-users
.GUID 14c3bb5e-7531-4941-b109-0f3496035eff
.AUTHOR Jos Verlinde [MSFT]
.COMPANYNAME Microsoft
.COPYRIGHT Jos Verlinde 2017
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)]
$InputFile = '.\export\DocItemSet.xml',
#Folder to export to
[Parameter(Mandatory=$true)]
$OutputPath = '.\export\',
#Only use if group names are base64 encoded
[Switch]$Base64,
#A specific user
[String]$SIPAddress = $null
)
function DecodeBase64($Base64) {
Try {
$UTF8Plain = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($Base64))
return [string] $UTF8Plain
} Catch {
#then return the original assuming it was not encoded
return [string] $Base64
}
}
#Read the file
[xml]$CSUserData = Get-Content $InputFile
#Select Only config items with defines groups
#$CSUserData.DocItemSet.DocItem | ? { $_.Data.HomedResource.ContactGroups }
#Select Only config items with definec Contacts (and Groups)
if ($CSUserData.DocItemSet) {
#Pool Export
$ContactData = $CSUserData.DocItemSet.DocItem.Data | Where-Object { $_.HomedResource.Contacts }
}
If ($CSUserData.HomedResources.HasChildNodes) {
#Per User Export
$ContactData = $CSUserData.HomedResources | Where-Object { $_.HomedResource.Contacts }
}
if ($ContactData -eq $null) {
Throw "unknown input format"
}
#todo : Add filter to extract only specific users / SIP addresses
# $ContactData = $ContactData | Where SIP -MAtches $SIPAddress
ForEach ($CSUser in $ContactData ) {
#Get the groups for this user
$Groups= @{}
$CSUser.HomedResource.ContactGroups.ContactGroup |
ForEach-Object{
if ($Base64) {
$Name = DecodeBase64 -Base64 $_.DisplayName
} else {
$Name = $_.DisplayName
}
Write-Verbose $Name -Verbose
$Groups.Add( $_.Number , $Name )
}
#Now get the contacts in each group
[array]$UserContacts = Foreach ($ID in $Groups.Keys) {
#DEBUG t.b.d. What does the group ~ mean ; include for now
if ( $Groups[$ID] -eq '~' )
{
#Can't be imported , but might be usefull to keep in CSV
$GroupName = 'Recent Contacts'
} else {
$GroupName = $Groups[$ID]
}
Write-Verbose "Processing group $($Groups[$ID])" -Verbose
#Get all the contacts that are in this group
$GroupContacts = $CSUser.HomedResource.Contacts.Contact |Where-Object { $_.Groups.Contains($ID)}
[Array]$CSVContacts = $GroupContacts | ForEach-Object {
New-Object -TypeName PSObject -Property @{
Group = $GroupName
Key = "{0}-{1}" -f $GroupName, $_.Buddy
SIP = $_.Buddy
}
}
#OK, now output this Group
if ($CSVContacts.Count -GT 0 ) {
Write-Output $CSVContacts
}
}
if ($UserContacts.Count -gt 0) {
if ($CSUser.HomedResource.UserAtHost) {
#Per user export
$ThisUser = $CSUser.HomedResource.UserAtHost
} else {
#Pool export
#bit Tricky , assumes two colons in the name
$ThisUser = $CSUser.Name.Split(':')[2]
}
$Filename = Join-path -Path $OutputPath -ChildPath ($ThisUser + '.csv')
Write-Verbose $Filename -Verbose
#Write-Progress -Activity $Filename -CurrentOperation 'Exporting'
$UserContacts.GetEnumerator() | Export-Csv -Path $Filename -NoTypeInformation -Force
}
}