-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuditFolderStructureToXML.ps1
205 lines (154 loc) · 6.62 KB
/
AuditFolderStructureToXML.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
function Get-folders()
{
param (
[Parameter(Mandatory=$true,Position=0)]
$higherLevelFolder,
[Parameter(Mandatory=$true,Position=1)]
[string]$OriginalLibrary
)
$Host.Runspace.ThreadOptions = “ReuseThread”
$global:foldersCollection+=$higherLevelFolder.ServerRelativeUrl #optional. Serves only to output a list of folders after script execution
$ctx.Load($higherLevelFolder.Folders)
$ctx.ExecuteQuery()
Write-Host $higherLevelFolder.ServerRelativeUrl $higherLevelFolder.Folders.Count
#Add the current folder to XML file with attributes "name" and "number of subfolders". You can expand the number of attributes
Add-XMLNode -folderServerRelativeUrl $higherLevelFolder.ServerRelativeUrl -XMLPath $script:XMLPath -folderName $higherLevelFolder.Name -noOfSubfolders $higherLevelFolder.Folders.Count -LibraryTitle $OriginalLibrary -folderItemCount $higherLevelFolder.ItemCount
#loop through all subfolders
foreach($lowerLevelFolder in $higherLevelFolder.Folders)
{
Get-folders -higherLevelFolder $lowerLevelFolder -OriginalLibrary $OriginalLibrary
}
}
function Get-Webfolders()
{
param (
#title of the library you are auditing
[Parameter(Mandatory=$true,Position=0)]
[string]$OriginalLibrary
)
$Host.Runspace.ThreadOptions = “ReuseThread”
$OriginalList=$ctx.Web.Lists.GetByTitle($OriginalLibrary)
# get first level folders from a library
$folderCollectionFirstLevel=$OriginalList.RootFolder.Folders
$ctx.load($OriginalList)
$ctx.Load($folderCollectionFirstLevel)
$ctx.ExecuteQuery()
$OriginalLibrary="/"+$OriginalLibrary+"/"
foreach($folder in $folderCollectionFirstLevel)
{
Get-folders -higherLevelFolder $folder -OriginalLibrary $OriginalLibrary
}
}
function Create-XML()
{
param (
# Path to XML file
[Parameter(Mandatory=$true,Position=0)]
[string]$XMLPath
)
$xmlWriter = New-Object System.XMl.XmlTextWriter($XMLPath,$Null)
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteComment('Get the Information about the folder structure')
$xmlWriter.WriteStartElement('Folders')
$xmlWriter.Flush()
$xmlWriter.Close()
$xmlDoc = [System.Xml.XmlDocument](Get-Content $XMLPath);
$xmlDoc.Save($XMLPath)
}
function Add-XMLNode()
{
param (
[Parameter(Mandatory=$true,Position=1)]
[string]$folderServerRelativeUrl,
[Parameter(Mandatory=$true,Position=0)]
[string]$XMLPath,
[Parameter(Mandatory=$true,Position=2)]
[string]$folderName,
[Parameter(Mandatory=$true,Position=3)]
[int]$noOfSubfolders,
[Parameter(Mandatory=$true,Position=4)]
[string]$libraryTitle,
[Parameter(Mandatory=$true,Position=5)]
[int]$folderItemCount
)
$xmlDoc = [System.Xml.XmlDocument](Get-Content $XMLPath);
$stringtoReplace = $libraryTitle
$perspectivenode = "//Folders/"+ $folderServerRelativeUrl.Replace($stringtoReplace,"")
Write-Host "Creating perspective node: " $perspectivenode
$root = $xmlDoc.DocumentElement;
#how many slashes are in the relative url apart from 3 slashes here: //Folders/ That allows later to decide at what level the folder is and how deeply we iterate through the nodes
$countOfLevels = ($perspectivenode.Length - ($perspectivenode.replace("/","").Length))-3
Write-Host "Levels: " $countOfLevels
if($countOfLevels -eq 0)
{
Write-Output $xmlDoc
$folderNode = $xmlDoc.CreateElement("Folder")
$xmlDoc.SelectSingleNode("/Folders").AppendChild($folderNode)
#if you want less attributes in the xml file, remove some of these lines
$folderNode.SetAttribute("Name", $folderName)
$folderNode.SetAttribute("NumberOfSubfolders", $noOfSubfolders)
$folderNode.SetAttribute("ItemCount", $folderItemCount)
$folderNode.SetAttribute("ServerRelativeUrl", $folderServerRelativeUrl)
$xmlDoc.Save($XMLPath)
}
elseif($countOfLevels -gt 0)
{
$nodePath="//Folders/"
$tempRelativeUrl=$perspectivenode.Replace("//Folders/","")
$node = $XMLDoc.SelectSingleNode("Folders")
#get the parent node. Assume that both folder names and entire paths may repeat
for($level=0; $level -lt $countOfLevels; $level++)
{
$nameOfTheUpperFolder = $tempRelativeUrl.Remove($tempRelativeUrl.IndexOf("/"))
Write-Host "nameoftheupperfolder " $nameOfTheUpperFolder
$node=$node.SelectNodes("Folder[@Name='$nameOfTheUpperFolder']")
$tempRelativeUrl = $tempRelativeUrl.Remove(0,($tempRelativeUrl.IndexOf("/")+1))
Write-Host "temprelativerl :" $tempRelativeUrl
}
$folderNode = $xmlDoc.CreateElement("Folder")
$node.AppendChild($folderNode)
#if you want less attributes in the xml file, remove some of these lines
$folderNode.SetAttribute("Name", $folderName)
$folderNode.SetAttribute("NumberOfSubfolders", $noOfSubfolders)
$folderNode.SetAttribute("ItemCount", $folderItemCount)
$folderNode.SetAttribute("ServerRelativeUrl", $folderServerRelativeUrl)
$xmlDoc.Save($XMLPath)
}
else
{
Write-Error "Bubu was made"
}
}
function Connect-SPO()
{
param (
[Parameter(Mandatory=$true,Position=1)]
[string]$Username,
[Parameter(Mandatory=$true,Position=2)]
[string]$Url,
[Parameter(Mandatory=$true,Position=3)]
$AdminPassword
)
$global:ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
$ctx.Load($ctx.Web)
$ctx.ExecuteQuery()
}
# Paths to SDK. Please verify location on your computer.
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
# Enter your data
$admin="ana@etr56.onmicrosoft.com"
$site="https://etr56.sharepoint.com"
$libraryTitle="test"
$script:XMLPath = "C:\Users\Public\foolders2.xml"
#Enter your data above!
#Do not modify lines below
$global:ctx
$global:foldersCollection=@() #optional. Serves only to output a list of folders after script execution
$pass=Read-Host "Enter Password: " -AsSecureString
Create-XML -XMLPath $XMLPath
Connect-SPO -Username $admin -Url $site -AdminPassword $pass
Get-Webfolders -OriginalLibrary $libraryTitle -ErrorAction Continue
#optional
Write-Output $global:foldersCollection