-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snippets.ps1
157 lines (130 loc) · 7.26 KB
/
Snippets.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
# PowerShell snippets for 2021 M365 Collab conference
# Create certificate for login
# stolen from https://www.powershellgallery.com/packages/ExchangeOnlineManagement/1.0.1/Content/Create-SelfSignedCertificate.ps1 and other places
# Google "Create-SelfSignedCertificate.ps1" for more places
.\Create-SelfSignedCertificate.ps1 -CommonName "M365Collab" -StartDate 2021-11-07 -EndDate 2022-01-20 -Password (ConvertTo-SecureString "pass@word1" -Force -AsPlainText)
# Run in Windows PowerShell 5.x in an Admin console
Install-Module PnP.PowerShell -Force –Scope AllUsers
Install-Module Microsoft.Online.SharePoint.PowerShell -Force –Scope AllUsers
Install-Module Microsoft.PowerApps.Administration.PowerShell
Install-Module Microsoft.PowerApps.PowerShell –AllowClobber
# Only gets the modules installed in the current PowerShell host
Get-InstalledModule
# Gets all of the modules installed on the machine
Get-Module -ListAvailable
# Make sure a module is installed where both PowerShells and all users can use it
Get-InstalledModule -Name Microsoft.Online.SharePoint.PowerShell | Select-Object InstalledLocation
Update-Module Microsoft.Online.SharePoint.PowerShell
# Use stored credentials
Add-PnPStoredCredential -Name "https://tenant.sharepoint.com" -Username [email protected]
Connect-PnPOnline -Url "https://tenant.sharepoint.com”
Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/hr”
$Credentials = Get-PnPStoredCredential -Name "https://tenant.sharepoint.com"
Connect-SPOService -Url https://tenant-admin.sharepoint.com -Credentials $ Credentials
# Upload a file
$web = https://tenant.sharepoint.com/sites/hr
$folder = "Shared Documents"
Connect-PnPOnline -Url $web
Add-PnPFile -Path '.\Boot fairs with Graphic design.docx' -Folder $folder
# Add a folder
Add-PnPFolder -Name "Folder 1" -Folder $folder
Add-PnPFile -Path '.\Building materials licences to budget for Storytelling.docx' -Folder "$folder\Folder 1“
# Get Internal Shared Files example
# Doesn't actually work
$doclibs = Get-PnPList -Includes DefaultViewUrl,IsSystemList | Where-Object -Property IsSystemList -EQ -Value $false | Where-Object -Property BaseType -EQ -Value "DocumentLibrary"
Foreach ($doclib in $doclibs)
{
$docs = Get-PnPListItem -List $DocLib
foreach ($doc in $docs) {
if (($doc.FieldValues).SharedWithUsers -ne $null) {
foreach ($user in (($doc.FieldValues).SharedWithUsers)) {
Write-Output "$(($doc.FieldValues).FileRef) - $($user.email)"
}
}
}
}
# Get Extended File Info example
# Doesn't work either
$doclibs = Get-PnPList -Includes DefaultViewUrl,IsSystemList | Where-Object -Property IsSystemList -EQ -Value $false | Where-Object -Property BaseType -EQ -Value "DocumentLibrary“
Foreach ($doclib in $doclibs)
{
$doclibTitle = $doclib.Title
$docs = Get-PnPListItem -List $DocLib
$docs | ForEach-Object { Get-PnPProperty -ClientObject $_ -Property File, ContentType, ComplianceInfo}
foreach ($doc in $docs) {
[pscustomobject]@{Library= $doclibTitle;Filename = ($doc.File).Name;ContentType = ($doc.ContentType).Name;Label = ($doc.ComplianceInfo).ComplianceTag}
}
# Bulk Undelete Files example
# Actually does work
Connect-PnPOnline -Url https://sadtenant.sharepoint.com/ -Credentials SadTenantAdmin
$bin = Get-PnPRecycleBinItem | Where-Object -Property Leafname -Like -Value "*.jpg" | Where-Object -Property Dirname -Like -Value “Important Photos/Shared Documents/*" | Where-Object -Property DeletedByEmail -EQ -Value [email protected]
$bin.count
$bin | ForEach-Object -begin { $a = 0} -Process {Write-Host "$a - $($_.LeafName)" ; $_ | Restore-PnPRecycleBinItem -Force ; $a++ } -End { Get-Date }
($bin[20001..30000]) | ForEach-Object -begin { $a = 0} -Process {Write-Host "$a - $($_.LeafName)" ; $_ | Restore-PnPRecycleBinItem -Force ; $a++ } -End { Get-Date }
# https://www.toddklindt.com/PoshRestoreSPOFiles
# Create sites examples
New-SPOSite
<# No Group, No Team, No Bueno
Can be Groupified later #>
New-PnPSite -Type TeamSite -Title “Modern Team Site" -Alias ModernTeamSite –IsPublic
<# Group, No Team
Can be Teamified later#>
New-Team -DisplayName “Fancy Group" -Description “Fancy Group made by PowerShell?" -Alias FancyGroup -AccessType Public
# There is no later!
# Group Membership example
# Set some values
# use Get-PnPUnifiedGroup to get Unified Group names
# Name of Unified Group whose owners and membership we want to copy
$source = "Regulations"
# Name of Unified Group whose owners and membership we want to populate
$destination = "Empty"
# Whether to overwrite Destination membership or merge them
$mergeusers = $false
# Check to see if PnP Module is loaded
$pnploaded = Get-Module PnP.PowerShell
if ($pnploaded -eq $false) {
Write-Host "Please load the PnP PowerShell and run again"
Write-Host "install-module PnP.PowerShell"
break
}
# PnP Module is loaded
# Check to see if user is connected to Microsoft Graph
try
{
$owners = Get-PnPMicrosoft365GroupOwners -Identity $source
}
catch [System.InvalidOperationException]
{
Write-Host "No connection to Microsoft Graph found" -BackgroundColor Black -ForegroundColor Red
Write-Host "No Azure AD connection, please connect first with Connect-PnPOnline -Graph" -BackgroundColor Black -ForegroundColor Red
break
}
catch [System.ArgumentNullException]
{
Write-Host "Group not found" -BackgroundColor Black -ForegroundColor Red
Write-Host "Verify connection to Azure AD with Connect-PnPOnline -Graph" -BackgroundColor Black -ForegroundColor Red
Write-Host "Use Get-PnPUnifiedGroup to get Unified Group names" -BackgroundColor Black -ForegroundColor Red
break
}
catch
{
Write-Host "Some other error" -BackgroundColor Black -ForegroundColor Red
break
}
$members = Get-PnPMicrosoft365GroupMembers -Identity $source
if ($mergeusers -eq $true) {
# Get existing owners and members of Destination so that we can combine them
$ownersDest = Get-PnPMicrosoft365GroupOwners -Identity $destination
$membersDest = Get-PnPMicrosoft365GroupMembers -Identity $destination
# Add the two lists together so we don't overwrite any existing owners or members in Destination
$owners = $owners + $ownersDest
$members = $members + $membersDest
}
# Set the owners and members of Destination
$owners | ForEach-Object -begin {$ownerlist = @() } -process {$ownerlist += $($_.UserPrincipalName) }
$members | ForEach-Object -begin {$memberlist = @() } -process {$memberlist += $($_.UserPrincipalName) }
Set-PnPMicrosoftGroup -Identity $destination -Members $memberlist -Owners $ownerlist
# https://www.toddklindt.com/PoshCopyO365GroupMembers
# Get all the Flow
Add-PowerAppsAccount
Get-AdminFlow | ForEach-Object { $ownername = (Get-MsolUser -ObjectId $_.CreatedBy.userId).DisplayName ; $owneremail = (Get-MsolUser -ObjectId $_.CreatedBy.userId).UserPrincipalName ; Write-Host $_.DisplayName, $ownername, $owneremail }