Onedrive-client.psm1

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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
function Get-ODClients {
    [CmdletBinding()]
    
    $returnObj = [PSCustomObject]@{}

    $globalConfigs = Get-ChildItem "$env:LOCALAPPDATA\Microsoft\Onedrive\Settings\*\global.ini" | Get-Content -Encoding unicode
    $clientIDmatches = $globalConfigs | Select-String -Pattern "^cid = (?<ClientID>.+)$" -AllMatches

    $clientIDMatches.Matches.Captures | Select-Object -ExpandProperty Groups | Where-Object { $_.Name -notmatch "\d+" } | ForEach-Object {
        Add-Member -InputObject $returnObj -MemberType NoteProperty -Name $_.Name -Value $_.Value
    }

    Return $returnObj
}
function Get-ODConfig {
    [CmdletBinding()]

    param (
        $ClientID
    )
    $clientIDConfigs = Get-ChildItem "$env:LOCALAPPDATA\Microsoft\Onedrive\Settings\*\$($ClientID).ini" | Get-Content -Encoding unicode
    return $clientIDConfigs
}
function Get-ODSyncedLibraryConfig {
    [CmdletBinding()]

    param (
        [parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true)]    
        $ClientID
    )
    $clientIDConfig = Get-ODConfig -ClientID $ClientID
    $LibraryScopeRegex = "^libraryScope = (?<LibraryScopeID>\d+) (?<SubscriptionID>(?:\w{32}|\w{8}-\w{4}-\w{4}-\w{4}-\w{12})\+\d+) \d+ \`"(?<SiteName>[\w- ]+)\`" \`"(?<FolderName>\w+)\`" \d+ \`"(?<SiteURL>.*?)\`" \`"(?<TenantID>\w{32}|\w{8}-\w{4}-\w{4}-\w{4}-\w{12})\`" (?<SiteID>\w{32}|\w{8}-\w{4}-\w{4}-\w{4}-\w{12}) (?<WebID>\w{32}|\w{8}-\w{4}-\w{4}-\w{4}-\w{12}) (?<ListID>\w{32}|\w{8}-\w{4}-\w{4}-\w{4}-\w{12}) \d+ \`"(?<FilePath>.*?)\`" \d+ (\w{32}|\w{8}-\w{4}-\w{4}-\w{4}-\w{12}).*?$"
    $libScopeMatches = $clientIDConfig | Select-String -Pattern $LibraryScopeRegex -AllMatches
    $libScopeMatches | ForEach-Object {
        $returnObj = [PSCustomObject]@{
            ClientID = $ClientID
        }
        $_.Matches.Captures | Select-Object -ExpandProperty Groups |  ForEach-Object {
            If ($_.Name -notmatch "\d+" ) {
                Add-Member -InputObject $returnObj -NotePropertyName $_.Name -NotePropertyValue $_.Value
            }
        }
        Add-Member -InputObject $returnObj  -NotePropertyName "SourceLine" -NotePropertyValue ($_.Matches.groups.Item('0').value)
        
        Add-Member -InputObject $returnObj  -NotePropertyName "SourceScopeLine" -NotePropertyValue ($_.Matches.groups.Item('0').value)
        Add-Member -InputObject $returnObj -NotePropertyName 'SourceFile' -NotePropertyValue (Get-ChildItem "$env:LOCALAPPDATA\Microsoft\Onedrive\Settings\*\$($ClientID).ini")

        
        if ([string]::IsNullOrWhiteSpace($returnObj.FilePath)) {
            $folders = $returnObj | Get-ODSyncedFolderConfig
            Add-Member -InputObject $returnObj -NotePropertyName 'SyncedFolders' -NotePropertyValue $folders
        }
        $returnObj
    }
}

function Get-ODClientEXE {
    [CmdletBinding()]

    $OnedriveEXEs = Get-Item "C:\Program Files*\Microsoft OneDrive\OneDrive.exe" , "$env:LOCALAPPDATA\Microsoft\Onedrive\onedrive.exe" -ea SilentlyContinue
    
    if ($null -eq $OnedriveEXEs) {
        Write-Error "No OneDrive EXE Found. Please ensure you are running as the user you want to act on, not System"
    }
    elseif (@($OnedriveEXEs).Count -gt 1) {
        Write-Warning "Hi you managed to apparently have both a per-user and per-machine install"
        Write-Warning "Congratulations! I couldn't manage to replicate or rule this out with my testing, so no promises if this works at all"
        
        Return ($OnedriveEXEs | Select-Object -first 1)
    }
    else {
        Return $OnedriveEXEs
    }

}

function Get-ODSyncedFolderConfig {
    [CmdletBinding()]

    param (
        [parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true)]    
        $ClientID,
        [parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true)]    
        $LibraryScopeID        
    )
    #$SyncedLibrary = Get-ODSyncedLibraryConfig -ClientID $ClientID

    $LibraryFolderRegex = "^libraryFolder = (?<LibraryFolderID>\d+) (?<LibraryScopeID>\d+) (?:(?:\w{32}|\w{8}-\w{4}-\w{4}-\w{4}-\w{12})\+\d+) \d+ \`"(?<FilePath>.*?)\`" \d+ \`"(?<FolderName>\w+)\`" .*?$"

    $clientIDConfig = Get-ODConfig -ClientID $ClientID
    $matchFolders = $clientIDConfig | Select-String -Pattern $LibraryFolderRegex -AllMatches
    $returnFolders = $matchFolders | ForEach-Object {
        $libraryFolder = [PSCustomObject]@{
            ClientID = $ClientID
        }
        $_.Matches.Captures | Select-Object -ExpandProperty Groups |  ForEach-Object {
            If ($_.Name -notmatch "\d+" ) {
                Add-Member -InputObject $libraryFolder -NotePropertyName $_.Name -NotePropertyValue $_.Value -Force
            }
        }

        Add-Member -InputObject $libraryFolder -NotePropertyName "SourceFolderLine" -NotePropertyValue ($_.Matches.groups.Item('0').value)
        Add-Member -InputObject $libraryFolder -NotePropertyName 'SourceFile' -NotePropertyValue (Get-ChildItem "$env:LOCALAPPDATA\Microsoft\Onedrive\Settings\*\$($ClientID).ini")

        $libraryFolder
    } 
    If (-not [string]::IsNullOrWhiteSpace($LibraryScopeID)) {
        $returnFolders | Where-Object { $_.LibraryScopeID -eq $LibraryScopeID }
    }
    else {
        $returnFolders
    }
}

function Stop-ODClients {
    [CmdletBinding()]

    $OneDriveEXE = Get-ODClientEXE
    $process = Start-Process -FilePath $OneDriveEXE -ArgumentList "/shutdown" -PassThru
    
    try {
        $process | Wait-Process -TimeoutSec 60 
    }
    catch {
        $_.ErrorDetails
    }
}

function Start-ODClients {
    [CmdletBinding()]
    param (
        [switch]
        $PassThru
    )
    $OneDriveEXE = Get-ODClientEXE | Select-Object -ExpandProperty FullName
    $process = Start-Process -FilePath $OneDriveEXE -ArgumentList "/background" -PassThru

    # $process
    # Start-Sleep -Seconds 10
    # $process

    if ($PassThru) {
        Return $process
    }
}

function Remove-ODSyncedItemConfig {
    <#
    .SYNOPSIS
        Remove Synced Library/Folder from OneDrive
    .DESCRIPTION
        Removes a Synced Library and/or Folder from OneDrive's Synced list and optionally also removes the local files from the disk.
    .EXAMPLE
        PS C:\> Get-ODClients | Get-ODSyncedLibraryConfig | where {$_.SiteName -like "OldTeam*"} | Remove-ODSyncedItemConfig
        Select a Team by SiteName and Remove it.
        If multiple folders are Synced under a library it will remove all folders synced to this library
    .EXAMPLE
        PS C:\> Get-ODClients | Get-ODSyncedLibraryConfig | where {$_.SiteID -eq "f496b4697f1d4b968542d065c7dd261b"} | Remove-ODSyncedItemConfig
        Select a Team by SiteID and Remove it.
        If multiple folders are Synced under a library it will remove all folders synced to this library
    .EXAMPLE
        PS C:\> Get-ODClients | Get-ODSyncedLibraryConfig | Out-GridView -PassThrough | Remove-ODSyncedItemConfig
        Open a GUID GridView and allow interactive Selection of Library items to remove.
        If multiple folders are Synced under a library it will remove all folders synced to this library
    .EXAMPLE
        PS C:\> Get-ODClients | Get-ODSyncedLibraryConfig | select -expandProperty SyncedFolders | Out-GridView -PassThrough | Remove-ODSyncedItemConfig
        Open a GUID GridView and allow interactive Selection of Synced Folder items to remove.
        If you select all folders under a library, but did not select the library itself, the folders will be removed, but the library config entry will not be removed directly, but may be cleaned up by OneDrive later on
    .EXAMPLE
        PS C:\> Get-ODClients | Get-ODSyncedLibraryConfig | select -expandProperty SyncedFolders | where {$_.FolderName -eq 'OldDeadChannel'} | Remove-ODSyncedItemConfig
        Select a folder to remove by the name of the SharePoint folder it is mapped to, not the name of the Local Folder it is mapped to.
        However, this would unmap all synced folders if there were entries for multiple Sites/Teams. Example it would match "Company - OldDeadChannel" and "TestTeam - OldDeadChannel"
    .EXAMPLE
        PS C:\> Get-ODClients | Get-ODSyncedLibraryConfig | where {$_.SiteName -like "OldTeam"} | select -expandProperty SyncedFolders | where {$_.FolderName -eq 'OldDeadChannel'} | Remove-ODSyncedItemConfig
        Select a specific synced Folder within a specific synced library and unmap it. Here I show selecting Site:`OldTeam` Channel:`OldDeadChannel` and remove just that folder, but not the entire "OldTeam" Library.
        NOTE: This will only work if that folder is directly Synced down separately, it will not let you remove sub-folders of folders that are synced
    .INPUTS
        Provide a SharePoint Library from Get-ODSyncedLibraryConfig or a Synced Folder from Get-ODSyncedFolderConfig / (Get-ODSyncedLibraryConfig).SyncedFolders
    .OUTPUTS
        Output (if any)
    .NOTES
        Needs to be reconstructed later into separate Parameter sets. LibraryScope, LibraryFolder, and Generic (sourceLine)
        NOTE: Has not been tested with Folders/Libraries related to Sites/Site Folders/items added as "shortcuts" to a User's OneDrive
    #>

    [CmdletBinding()]
    param (
        [parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true)]    
        $SourceFile,
        [parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true)]    
        $SyncedFolders,
        [parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true)]    
        $SourceScopeLine,
        [parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true)]    
        $SourceFolderLine,
        [parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true)]    
        $FilePath,
        [parameter(
            Mandatory = $false)]    
        [bool]
        $RemoveLocalFiles = $true,
        [parameter(
            Mandatory = $false)]    
        [bool]
        $UnpinBeforeRemoval = $true
    )

    #Empty Case
    if ($null -eq $SourceFile -and ($null -eq $SourceScopeLine -or $null -eq $SourceFolderLine ) ) {
        Return
    }

    #Library with Non-root Synced Folders
    #means $filePath should already be empty
    if ($null -ne $SyncedFolders) {
        $SyncedFolders | ForEach-Object {
            $_ | Remove-ODSyncedItemConfig -RemoveLocalFiles $RemoveLocalFiles -UnpinBeforeRemoval $UnpinBeforeRemoval
        }
    }
    else {
    }

    if (-not [String]::IsNullOrWhiteSpace($SourceFolderLine)) {
        $SourceLine = $SourceFolderLine
    }
    elseif (-not [String]::IsNullOrWhiteSpace($SourceScopeLine)) {
        $SourceLine = $SourceScopeLine
    }
    else {
        Write-Error "What line would you like to remove?"
    }

    if (-not [string]::IsNullOrWhiteSpace($FilePath) -and $UnpinBeforeRemoval) {
        Write-Verbose "Force Unpinning directory (""Free Up Space"")"
        attrib.exe -P +U ($FilePath.FullName) /S /D | Write-Verbose 
    }

    Stop-ODClients
    (Get-Content $SourceFile -Encoding unicode ) -replace [regex]::Escape($SourceLine) | Out-File -FilePath $SourceFile -Encoding unicode -Force 
    Start-ODClients
    if (-not [string]::IsNullOrWhiteSpace($FilePath) -and $RemoveLocalFiles) {
        $FilePath | Get-ChildItem -Force | Remove-Item -Recurse -Force
        $FilePath | Get-Item -Force -EA SilentlyContinue | Remove-Item -Force    
    }
}