forked from byteben/OneDrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneDriveKFM_Prevent_Duplicate_Shortcuts.ps1
180 lines (139 loc) · 6.29 KB
/
OneDriveKFM_Prevent_Duplicate_Shortcuts.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
<#
.Disclaimer
This scripts is provided AS IS without warranty of any kind.
In no event shall the author be liable for any damages whatsoever
(including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss)
arising out of the use of or inability to use script,
even if the Author has been advised of the possibility of such damages
.Author
Ben Whitmore
.Created
28/04/2019
.DESCRIPTION
Script to monitor and remove duplicate desktop shortcuts created when OneDrive Known FOlder Move initialises
.EXAMPLE
OneDriveKFM_Prevent_Duplicate_Shortcuts.ps1
#>
#Unregister-Event -SourceIdentifier FileCreated if still regsitered
$EventWatcher = Get-EventSubscriber
ForEach ($Event in $EventWatcher)
{
If ($Event.SourceIdentifier -eq 'FileCreated')
{
Unregister-Event -SubscriptionId $Event.SubscriptionId
}
}
#Get user shell folder path for the desktop folder
$UserShellFolderKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
$UserShellFolderDir = (Get-ItemProperty -Path $UserShellFolderKey -Name Desktop).Desktop
#Get folder path for OneDrive for Business
If ($UserShellFolderDir -like "*OneDrive - *")
{
$OneDriveCommercialPath = $UserShellFolderDir
}
else
{
$OneDriveCommercialPath = $env:OneDriveCommercial + "\Desktop"
}
$ProgressBar = 'True'
#Specify the time in seconds the script will wait for KFM policies to redirect the desktop
$TimeToWait = 900
#Initialise wait timer
$WaitTime = 0
Do
{
$ProgressCount = ($WaitTime/$TimeToWait) * 100
If ($UserShellFolderDir -ne $OneDriveCommercialPath)
{
#Sleep 1 Second before checking if OneDrive KFM policies have redirected desktop
Start-Sleep -s 1
#Set time remaining counter
$CountDown = ($TimeToWait - $WaitTime)
#If set to 'True', display the progress bar. Use for testing only.
If ($ProgressBar -eq 'True')
{
Write-Progress -Activity "Waiting for KFM Redirection" -Status $CountDown" Seconds" -PercentComplete $ProgressCount
}
#Reset desktop variable
$UserShellFolderDir = (Get-ItemProperty -Path $UserShellFolderKey -Name Desktop).Desktop
}
}
#Continue checking for folder redirection until KFM initialises or the timer runs out
While (++$WaitTime -le $TimeToWait -and $UserShellFolderDir -ne $OneDriveCommercialPath)
#If KFM timeout interval is reached, suggest increasing the timeout
If ($WaitTime -ge $TimeToWait)
{
Write-Host "Script timed out waiting for OneDrive KFM to redirect the desktop folder. Try increasing the 'TimeToWait' variable" -ForegroundColor Yellow
}
else
{
Write-Host "User shell folder desktop is redirected to OneDrive" -ForegroundColor Green
#Specify the time in seconds the script will wait for KFM Desktop to be created
$TimeToWait2 = 1800
#Initialise wait timer2
$WaitTime2 = 0
Do
{
$ProgressCount2 = ($WaitTime2/$TimeToWait2) * 100
If (!(Test-Path -Path $UserShellFolderDir))
{
#Sleep 1 Second before checking if OneDrive KFM desktop exists
Start-Sleep -s 1
#Set time remaining counter
$CountDown2 = ($TimeToWait2 - $WaitTime2)
#If set to 'True', display the progress bar. Use for testing only.
If ($ProgressBar -eq 'True')
{
Write-Progress -Activity "Waiting OneDrive Desktop Creation" -Status $CountDown2" Seconds" -PercentComplete $ProgressCount2
}
}
}
#Continue checking for folder creation or until the timer runs out
While (++$WaitTime2 -le $TimeToWait2 -and (!(Test-Path -Path $UserShellFolderDir)))
#If KFM creates the OneDrive desktop folder before the timeout value is reached, carry on with the script
if (Test-Path -Path $UserShellFolderDir)
{
#User shell folder desktop is created on Onedrive
Write-Host "User shell folder desktop exists in OneDrive" -ForegroundColor Green
#Define a new File System Watcher
$ShortcutRadar = New-Object System.IO.FileSystemWatcher
$ShortcutRadar.Path = $UserShellFolderDir
$ShortcutRadar.Filter = '*.lnk'
$ShortcutRadar.IncludeSubdirectories = $False
$ShortcutRadar.NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
#Create a new File System Watcher to check when .lnk files are created in OneDrive\Desktop
Register-ObjectEvent $ShortcutRadar Created -SourceIdentifier FileCreated -Action {
$FileName = $Event.SourceEventArgs.Name
$FileOperation = $Event.SourceEventArgs.ChangeType
$FileOperationTimeStamp = $Event.TimeGenerated
Write-Host "The file '$FileName' was $FileOperation at $FileOperationTimeStamp" -ForegroundColor Green
#For each new shortcut created on the desktop, get the target
$ShortcutObject = New-Object -ComObject WScript.Shell
#Get all versions of the newly created shortcut
$StringSeparator = " - Copy"
$NewShortcut = $FileName -Split $StringSeparator
$NewShortcutOriginalName = $NewShortcut[0]
$OriginalOneDriveDesktopShortcut = Get-ChildItem -Path "$($UserShellFolderDir)" -Filter '*.lnk' | Where { $_.Name.StartsWith($NewShortcutOriginalName) } | Sort-Object CreationTime -Descending | Select -Last 1
$OneDriveDesktopShortcuts = Get-ChildItem -Path "$($UserShellFolderDir)" -Filter '*.lnk' | Where { $_.Name.StartsWith($NewShortcutOriginalName) -and $_.Name -ne $OriginalOneDriveDesktopShortcut.Name } | Sort-Object CreationTime -Descending
#Get target for original desktop icon
$OriginalShortcutDestination = $ShortcutObject.CreateShortcut($OriginalOneDriveDesktopShortcut.FullName)
$OriginalShortcutDestination.TargetPath
#Show original desktop shortcut (earliest creation time and doesnt contain the word "copy"
Write-Host "Original Shorcut: " $OriginalOneDriveDesktopShortcut.Name " | " $OriginalShortcutDestination.TargetPath -ForegroundColor Magenta
ForEach ($Target in $OneDriveDesktopShortcuts)
{
$ShortcutDestination = $ShortcutObject.CreateShortcut($Target.Fullname)
$ShortcutDestination.TargetPath
#Show all shortcuts that match partial name and exact target of newly created shortcut
If ($ShortcutDestination.TargetPath -eq $OriginalShortcutDestination.TargetPath)
{
Write-Host $Target.Name " | " $ShortcutDestination.TargetPath -ForegroundColor Yellow
#Delete duplicate shortcuts
Remove-Item -Path $UserShellFolderDir"\"$Target -Force
}
}
}
}
}
#To End File Watcher Run:-
#Unregister-Event -SourceIdentifier FileCreated