# OneDrive redirection for XenApp Script
# Olivier MARCHETTA - IT Contractor - London - 2018
# This script will redirect XenApp folders to the OneDrive folders on the connected client device
# If the client device OneDrive folder is not available, the script will redirect the folders on the local XenApp server
# The script will also create missing folders in OneDrive (fixing a previous old migration from Windows XP profiles)
# 1. Init variables
$remoteonedrive = "\\client\c$\users\" + $env:UserName + "\OneDrive - Company Name"
$testremoteonedrive = Test-Path -Path $remoteonedrive
$usershellfolders = Get-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders"
$restartexplorer = 0
# 2. Redirecting XenApp profile folders to the client OneDrive folders if available
If ($testremoteonedrive -eq $true){
#Init OneDrive paths variables
$mydesktoppath = Join-Path $remoteonedrive -ChildPath "Desktop"
$mydocumentspath = Join-Path $remoteonedrive -ChildPath "Documents"
$mypicturespath = Join-Path $remoteonedrive -ChildPath "Pictures"
$mymusicpath = Join-Path $remoteonedrive -ChildPath "Music"
$myvideospath = Join-Path $remoteonedrive -ChildPath "Videos"
$mydownloadspath = Join-Path $remoteonedrive -ChildPath "Downloads"
$mylinkspath = Join-Path $remoteonedrive -ChildPath "Links"
#Init Test-Path variables
$mydesktoptest = Test-Path -Path $mydesktoppath
$mydocumentstest = Test-Path -Path $mydocumentspath
$mypicturestest = Test-Path -Path $mypicturespath
$mymusictest = Test-Path -Path $mymusicpath
$myvideostest = Test-Path -Path $myvideospath
$mydownloadstest = Test-Path -Path $mydownloadspath
$mylinkstest = Test-Path -Path $mylinkspath
#Testing OneDrive subfolders and creating missing directories
If ($mydesktoptest -eq $false){
New-Item -Path $remoteonedrive -Name "Desktop" -ItemType "directory" -ErrorAction SilentlyContinue
Sleep -Milliseconds 200}
If ($mydocumentstest -eq $false){
New-Item -Path $remoteonedrive -Name "Documents" -ItemType "directory" -ErrorAction SilentlyContinue
Sleep -Milliseconds 200}
If ($mypicturestest -eq $false){
New-Item -Path $remoteonedrive -Name "Pictures" -ItemType "directory" -ErrorAction SilentlyContinue
Sleep -Milliseconds 200}
If ($mymusictest -eq $false){
New-Item -Path $remoteonedrive -Name "Videos" -ItemType "directory" -ErrorAction SilentlyContinue
Sleep -Milliseconds 200}
If ($myvideostest -eq $false){
New-Item -Path $remoteonedrive -Name "Music" -ItemType "directory" -ErrorAction SilentlyContinue
Sleep -Milliseconds 200}
If ($mydownloadstest -eq $false){
New-Item -Path $remoteonedrive -Name "Downloads" -ItemType "directory" -ErrorAction SilentlyContinue
Sleep -Milliseconds 200}
If ($mylinkstest -eq $false){
New-Item -Path $remoteonedrive -Name "Links" -ItemType "directory" -ErrorAction SilentlyContinue
Sleep -Milliseconds 200}
#Comparing and updating User Shell Folders in registry
If ($mydesktoppath -ne $usershellfolders.Desktop){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "Desktop" -value $mydesktoppath -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mydocumentspath -ne $usershellfolders.Personal){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "Personal" -value $mydocumentspath -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mydocumentspath -ne $usershellfolders.'{F42EE2D3-909F-4907-8871-4C22FC0BF756}'){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "{F42EE2D3-909F-4907-8871-4C22FC0BF756}" -value $mydocumentspath -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mypicturespath -ne $usershellfolders."My Pictures"){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "My Pictures" -value $mypicturespath -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mypicturespath -ne $usershellfolders.'{0DDD015D-B06C-45D5-8C4C-F59713854639}'){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "{0DDD015D-B06C-45D5-8C4C-F59713854639}" -value $mypicturespath -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mymusicpath -ne $usershellfolders."My Music"){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "My Music" -value $mymusicpath -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mymusicpath -ne $usershellfolders.'{A0C69A99-21C8-4671-8703-7934162FCF1D}'){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "{A0C69A99-21C8-4671-8703-7934162FCF1D}" -value $mymusicpath -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($myvideospath -ne $usershellfolders."My Video"){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "My Video" -value $myvideospath -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($myvideospath -ne $usershellfolders.'{35286A68-3C57-41A1-BBB1-0EAE73D76C95}'){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "{35286A68-3C57-41A1-BBB1-0EAE73D76C95}" -value $myvideospath -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mydownloadspath -ne $usershellfolders.'{374DE290-123F-4565-9164-39C4925E467B}'){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "{374DE290-123F-4565-9164-39C4925E467B}" -value $mydownloadspath -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mylinkspath -ne $usershellfolders.Favorites){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "Favorites" -value $mylinkspath -ErrorAction SilentlyContinue
$restartexplorer = 1}
}
# 3. Fallback procedure: Redirecting user folders to the local XenApp profile on the server if the remote OneDrive is not available
# Note: we assume that the local user profile default folders exist and do not need to be tested or created
If ($testremoteonedrive -eq $false){
#Init local profile paths variables
$mydesktoppath = Join-Path $env:userprofile -ChildPath "Desktop"
$mydocumentspath = Join-Path $env:userprofile -ChildPath "Documents"
$mypicturespath = Join-Path $env:userprofile -ChildPath "Pictures"
$mymusicpath = Join-Path $env:userprofile -ChildPath "Music"
$myvideospath = Join-Path $env:userprofile -ChildPath "Videos"
$mydownloadspath = Join-Path $env:userprofile -ChildPath "Downloads"
$mylinkspath = Join-Path $env:userprofile -ChildPath "Links"
#Comparing and updating User Shell Folders in registry
If ($mydesktoppath -ne $usershellfolders.Desktop){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "Desktop" -value "%USERPROFILE%\Desktop" -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mydocumentspath -ne $usershellfolders.Personal){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "Personal" -value "%USERPROFILE%\Documents" -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mydocumentspath -ne $usershellfolders.'{F42EE2D3-909F-4907-8871-4C22FC0BF756}'){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "{F42EE2D3-909F-4907-8871-4C22FC0BF756}" -value "%USERPROFILE%\Documents" -ErrorAction Stop
$restartexplorer = 1}
If ($mypicturespath -ne $usershellfolders."My Pictures"){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "My Pictures" -value "%USERPROFILE%\Pictures" -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mypicturespath -ne $usershellfolders.'{0DDD015D-B06C-45D5-8C4C-F59713854639}'){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "{0DDD015D-B06C-45D5-8C4C-F59713854639}" -value "%USERPROFILE%\Pictures" -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mymusicpath -ne $usershellfolders."My Music"){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "My Music" -value "%USERPROFILE%\Music" -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mymusicpath -ne $usershellfolders.'{A0C69A99-21C8-4671-8703-7934162FCF1D}'){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "{A0C69A99-21C8-4671-8703-7934162FCF1D}" -value "%USERPROFILE%\Music" -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($myvideospath -ne $usershellfolders."My Video"){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "My Video" -value "%USERPROFILE%\Videos" -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($myvideospath -ne $usershellfolders.'{35286A68-3C57-41A1-BBB1-0EAE73D76C95}'){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "{35286A68-3C57-41A1-BBB1-0EAE73D76C95}" -value "%USERPROFILE%\Videos" -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mydownloadspath -ne $usershellfolders.'{374DE290-123F-4565-9164-39C4925E467B}'){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "{374DE290-123F-4565-9164-39C4925E467B}" -value "%USERPROFILE%\Downloads" -ErrorAction SilentlyContinue
$restartexplorer = 1}
If ($mylinkspath -ne $usershellfolders.Favorites){
Set-ItemProperty "hkcu:\software\microsoft\windows\currentversion\explorer\User Shell Folders" -Name "Favorites" -value "%USERPROFILE%\Favorites" -ErrorAction SilentlyContinue
$restartexplorer = 1}
}
#Restart explorer
If ($restartexplorer = 1){
$proc = @(Get-CimInstance -Query "Select * from Win32_Process where name = 'explorer.exe'")
for ($i=0; $i -lt $proc.length; $i++){
$owner = ($proc[$i] | Invoke-CimMethod -MethodName GetOwner).User
if ($owner -eq $env:UserName){
$processid = ($proc[$i]).ProcessId
Stop-Process -Id $processid}}