Skip to content

Instantly share code, notes, and snippets.

@andyzib
Last active September 8, 2020 20:51
Show Gist options
  • Save andyzib/b3d4e28a7b0b949e49ae4e2127452b33 to your computer and use it in GitHub Desktop.
Save andyzib/b3d4e28a7b0b949e49ae4e2127452b33 to your computer and use it in GitHub Desktop.
#Requires -Modules CredentialManager
#requires -version 5
<#
.SYNOPSIS
Checks to see if OneDrive credential is saved, if so starts OneDrive with /background switch.
.DESCRIPTION
Checks to see if OneDrive credential is saved, if so starts OneDrive with /background switch. Otherwise starts OneDrive normally. Intended to be used in a Citrix XenApp/Virtual Apps environment. Reccomented to run from Task Scheduler using the at logon trigger or using WEM.
.NOTES
Created by: Andrew Zbikowski <andrew@itouthouse.com>
.EXAMPLE
Just run the script.
#>
Function Start-OneDriveXenApp {
Param(
[Parameter(Mandatory=$false,HelpMessage="Start OneDrive with background parameter.")]
[switch]$backgroud=$false
)
# C:\Program Files (x86)\Microsoft OneDrive\OneDrive.exe - ${env:ProgramFiles(x86)}
# C:\Program Files\Microsoft OneDrive\OneDrive.exe - $env:ProgramFiles
if ( Test-Path -Path $(Join-Path -Path $env:ProgramFiles -ChildPath "Microsoft OneDrive\OneDrive.exe" ) ) {
$OneDrivePath = Join-Path -Path $env:ProgramFiles -ChildPath "Microsoft OneDrive\OneDrive.exe"
} elseif ( Test-Path -Path $(Join-Path -Path ${env:ProgramFiles(x86)} -ChildPath "Microsoft OneDrive\OneDrive.exe" ) ) {
$OneDrivePath = Join-Path -Path ${env:ProgramFiles(x86)} -ChildPath "Microsoft OneDrive\OneDrive.exe"
} else {
Exit # OneDrive Not Found.
}
# Start OneDrive.
if ($backgroud) {
Start-Process -FilePath $OneDrivePath -ArgumentList "/background"
} else {
Start-Process -FilePath $OneDrivePath
}
Exit # Exit Script.
}
# Requires CredentialManager module from the PowerShell Gallery.
# Install by running Install-Module CredentialManager
Import-Module CredentialManager -ErrorAction Stop
Try {
$AllCredentials = Get-StoredCredential -AsCredentialObject -ErrorAction SilentlyContinue
}
Catch {
Start-OneDriveXenApp # If there are no saved credentials, start OneDrive.
}
# Double check. Error handeling in the CredentialManager module seems spotty.
if (!$AllCredentials) {
Start-OneDriveXenApp # If there are no saved credentials, start OneDrive.
}
# Check to see if Office 2016 has a saved credential.
$OneDriveCred = $AllCredentials | Where-Object {$_.TargetName -Like "*OneDrive*" }
if (!$OneDriveCred) {
Start-OneDriveXenApp # If there are no saved credentials, start OneDrive.
} else {
Start-OneDriveXenApp -backgroud # If there are saved credentials, start OneDrive with /background parameter.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment