-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateSites.ps1
87 lines (66 loc) · 2.85 KB
/
CreateSites.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
function Create-Sites {
param (
$settingsFilePath="Config.json"
)
process{
$settingsContent = Get-Content $settingsFilePath
$settingsJson = $settingsContent | ConvertFrom-Json
$inputCSV = $settingsJson.Settings.sourceCSV
$onPremUsername = $settingsJson.Settings.onPrem.userName
$onPremPassword = $settingsJson.Settings.onPrem.password
$siteUrls =Import-Csv $inputCSV
$onPremPasswordSecure = ConvertTo-SecureString $onPremPassword -AsPlainText -Force
$onPremCreds = New-Object System.Management.Automation.PSCredential ($onPremUsername, $onPremPasswordSecure)
$tenantUrl = $settingsJson.Settings.online.adminUrl
$tenantUsername = $settingsJson.Settings.online.userName
$tenantPassword = $settingsJson.Settings.online.password
$tenantpasswordSecure = ConvertTo-SecureString $tenantPassword -AsPlainText -Force
$onlineCreds = New-Object -TypeName pscredential -ArgumentList $tenantUsername, $tenantpasswordSecure
$conSer =Connect-PnPOnline -url $tenantUrl -Credentials $onlineCreds
$headers = @{}
$headers["Accept"] = "application/json;odata=verbose"
foreach($row in $siteUrls)
{
$restResult=Invoke-RestMethod -Uri "$($row.Url)/_api/web/title" -Method Get -Headers $headers -Credential $onPremCreds
#converting xml result to json
$jsonTitleResult= $restResult | ConvertTo-Json
$jsonObjectResut = $jsonTitleResult | ConvertFrom-Json
$Url = $row.Url
if($($row.Url).endsWith("/"))
{
$Url = $Url.substring(0,$url.LastIndexOf("/"))
}
$alias = $Url.substring($url.LastIndexOf("/")+1)
$onlineSiteUrl =(Get-PnPSite).Url.replace("-admin","")+"/sites/"+$alias
$tenantSite = Get-PnPTenantSite $onlineSiteUrl -ErrorAction:SilentlyContinue #check if the site already exists in tenant
if($tenantSite -eq $null)
{
$siteUrl= Create-Site -title $jsonObjectResut.d.Title -urlAlias $alias
if($siteUrl -ne $null)
{
Write-Host "The site with url $siteUrl is successfully created" -ForegroundColor green
}
else
{
Write-Host "Error in creation of $Url" -ForegroundColor Red
}
}
else
{
Write-Host "The site with url $url already exists" -ForegroundColor yellow
}
}
}
}
function Create-Site
{
param(
$urlAlias,
$title
)
process
{
$site = New-PnPSite -Type TeamSite -Title $title -Alias $urlAlias
return $site
}
}