-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathGet-SPUrl.ps1
123 lines (92 loc) · 3.83 KB
/
Get-SPUrl.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
<#
$Metadata = @{
Title = "Get SharePoint Url"
Filename = "Get-SPUrl.ps1"
Description = ""
Tags = "powershell, function, sharepoint, list, url"
Project = ""
Author = "Janik von Rotz"
AuthorContact = "http://janikvonrotz.ch"
CreateDate = "2013-10-09"
LastEditDate = "2013-11-27"
Version = "2.0.0"
License = @'
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Switzerland License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ch/ or
send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
'@
}
#>
function Get-SPUrl {
<#
.SYNOPSIS
Get Url from a SharePoint object.
.DESCRIPTION
This function returns a normalized url to the provided SharePoint object or http url.
.PARAMETER SPobject
SharePoint Object.
.INPUTS
System.Object
.OUTPUTS
System.Uri
.EXAMPLE
PS C:\> Get-SPUrl -Object $SharePointListObject
.EXAMPLE
PS C:\> Get-SPUrl -Object "http://sharepoint.domain.ch/Projekte/SitePages/Homepage.aspx"
#>
param(
[Parameter(Mandatory=$true)]
$SPobject
)
#--------------------------------------------------#
# main
#--------------------------------------------------#
if($SPobject.PsObject.TypeNames -contains "System.String"){
[Uri]$Url = $SPobject
if($Url -match "(/Forms/).*?\.(aspx$)"){
# Library
New-Object PSObject -Property @{
Url = (($Url.Scheme + "://" + $Url.Host + $Url.LocalPath) -replace "(/Forms/).*?\.(aspx)","")
WebUrl = (($Url.Scheme + "://" + $Url.Host + $Url.LocalPath) -replace "/([^/]*)(/Forms/).*?\.(aspx)","")
}
}elseif($Url -match "(/Lists/).*?\.(aspx$)"){
# List
New-Object PSObject -Property @{
Url = (($Url.Scheme + "://" + $Url.Host + $Url.LocalPath) -replace "/([^/]*)\.(aspx)","")
WebUrl = (($Url.Scheme + "://" + $Url.Host + $Url.LocalPath) -replace "(/Lists/).*?\.(aspx)","")
}
}elseif($Url -match "_layouts"){
# Website 2013
New-Object PSObject -Property @{
Url = ((($Url.Scheme + "://" + $Url.Host + $Url.LocalPath) -replace "(/_layouts/).+","") -replace "\\","/")
}
}elseif($Url -match "/SitePages/Homepage.aspx$" -or $Url -match "/default.aspx$"){
# Website
New-Object PSObject -Property @{
Url = (($Url.Scheme + "://" + $Url.Host + $Url.LocalPath) -replace "/SitePages/Homepage.aspx", "" -replace "/default.aspx","")
}
}elseif($Url -match "_vti_history"){
# Documentlibrary Listitem File Subversion
New-Object PSObject -Property @{
Url = ((($Url.Scheme + "://" + $Url.Host + $Url.LocalPath) -replace "_vti_history/(.*[0-9])/","") -replace "\\","/")
}
}else{
# no match
New-Object PSObject -Property @{
Url = ($Url.Scheme + "://" + $Url.Host + $Url.LocalPath)
}
}
}elseif($SPobject.PsObject.TypeNames -contains "Microsoft.SharePoint.SPList"){
New-Object PSObject -Property @{
Url = (([Uri]$SPobject.Parentweb.Url).Scheme + "://" + ([uri]$SPobject.Parentweb.Url).host + $SPobject.RootFolder.ServerRelativeUrl)
}
}elseif($SPobject.PsObject.TypeNames -contains "Microsoft.SharePoint.SPWeb"){
New-Object PSObject -Property @{
Url = $SPobject.Url
}
}elseif($SPobject.PsObject.TypeNames -contains "Microsoft.SharePoint.SPListItem"){
New-Object PSObject -Property @{
Url = ($SPobject.ParentList.ParentWeb.Url + "/" + $SPobject.Url)
}
}
}