Functions/Remove-FileFromOneDrive.ps1
<#
.SYNOPSIS This function removes a file from OneDrive. .DESCRIPTION. This function removes a file from OneDrive. #> function Remove-FileFromOneDrive { [CmdletBinding(PositionalBinding=$false)] [OutputType([Bool])] param ( # The path to the file on OneDrive. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$filePath, # The User Principal Name of the user whose OneDrive account the file will be removed from. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$userPrincipalName, # The MSPComplete Endpoint containing the Microsoft Graph credentials. [Parameter(Mandatory=$true, ParameterSetName="endpoint", ValueFromPipeline=$true)] [ValidateNotNull()] $endpoint, # The Microsoft Graph authentication token. [Parameter(Mandatory=$true, ParameterSetName="token")] [ValidateNotNullOrEmpty()] [String]$token, # Select the stream where the messages will be directed. [Parameter(Mandatory=$false)] [ValidateSet("Information", "Warning", "Error")] [String]$outputStream = "Error" ) # Retrieve the Microsoft Graph authentication token if ($PSCmdlet.ParameterSetName -eq "endpoint") { Write-Information "Retrieving the Microsoft Graph authentication token using the provided endpoint." $token = Get-MicrosoftGraphAuthenticationToken -Endpoint $endpoint if ([String]::IsNullOrWhiteSpace($token)) { Write-OutputMessage "Failed to retrieve the Microsoft Graph authentication token using the provided endpoint." -OutputStream $outputStream -ReturnMessage:$false return $false } } # Set the protocol to TLS 1.2 [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 # Remove any "\" or "/" at the start of the OneDrive file path if ($filePath.StartsWith("\")) { $filePath = $filePath.TrimStart("\") } if ($filePath.StartsWith("/")) { $filePath = $filePath.TrimStart("/") } # Verify that the file path is valid if ([String]::IsNullOrWhiteSpace($filePath)) { Write-OutputMessage "Failed to retrieve a valid file path from '$($filePath)'." -OutputStream $outputStream -ReturnMessage:$false return $false } # Verify that the UserPrincipalName is valid if (!(Test-EmailAddressValidity -EmailAddress $userPrincipalName)) { Write-OutputMessage "The UserPrincipalName '$($userPrincipalName)' is not valid." -OutputStream $outputStream -ReturnMessage:$false return $false } # Prepare the DELETE Request $invokeRestMethodParams = @{ Uri = "https://graph.microsoft.com/v1.0/users/$($userPrincipalName)/drive/root:/$($filePath)" Method = "DELETE" Headers = @{ Accept = "application/json" Authorization = "bearer $($token)" } } # Try to remove the file from OneDrive Write-Information "Removing the file '$($filePath)' from OneDrive." try { $response = Invoke-RestMethod @invokeRestMethodParams } catch { Write-OutputMessage "Exception occurred while removing the file '$($filePath)' from OneDrive. `r`n$($_.Exception.Message)" -OutputStream $outputStream -ReturnMessage:$false return $false } # If the deletion is successful, $response will be an empty string, if not, it will be null if ("" -eq $response) { return $true } # Failed to remove the file else { Write-OutputMessage "Failed to remove the file '$($filePath)'." -OutputStream $outputStream -ReturnMessage:$false return $false } } |