Created
January 28, 2025 20:07
-
-
Save MyITGuy/a1bd136dd3675dab47e8156d2891e00d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Download and Install latest Production ring OneDriveSetup.exe installer for Windows | |
function Get-DownloadFileName { | |
[CmdletBinding()] | |
param( | |
[Uri] | |
$Uri | |
) | |
$Response = $null | |
# $ErrorActionPreference = 'SilentlyContinue' | |
if (!$Response) { | |
try { | |
$WebRequest = Invoke-WebRequest -Uri $Uri -Method Head | |
$Response = $WebRequest.BaseResponse | |
} catch {} | |
} | |
if (!$Response) { | |
try { | |
$HttpWebRequest = [System.Net.HttpWebRequest]::Create($Uri) | |
$HttpWebRequest.Method = "HEAD" | |
$Response = $HttpWebRequest.GetResponse() | |
} catch {} | |
} | |
if (!$Response) { | |
throw 'Could not determine download file name.' | |
} | |
# $ErrorActionPreference = 'Continue' | |
($Response.ResponseUri.LocalPath -split '/')[-1] | |
} | |
$DownloadUri = 'https://go.microsoft.com/fwlink/?linkid=844652' | |
Write-Host "DownloadUri: $($DownloadUri)" | |
$DownloadFileName = Get-DownloadFileName -Uri $DownloadUri | |
$DownloadAccessible = -not ( [System.String]::IsNullOrEmpty($DownloadFileName)) | |
# $DownloadAccessible = $false | |
Write-Host "DownloadAccessible: $($DownloadAccessible)" | |
if ($DownloadAccessible -eq $false) { $DownloadFileName = 'OneDriveSetup.exe' } | |
Write-Host "DownloadFileName: $($DownloadFileName)" | |
$DownloadFilePath = Join-Path -Path $PSScriptRoot -ChildPath $DownloadFileName | |
Write-Host "DownloadFilePath: $($DownloadFilePath)" | |
if ($DownloadAccessible) { | |
# Attempt to download the latest version | |
$global:ProgressPreference = 'SilentlyContinue' | |
$tmpDownloadFileName = [System.IO.Path]::GetFileName([System.IO.Path]::GetTempFileName()) | |
Write-Host "tmpDownloadFileName: $($tmpDownloadFileName)" | |
$tmpDownloadFilePath = Join-Path -Path $PSScriptRoot -ChildPath $tmpDownloadFileName | |
Write-Host "tmpDownloadFilePath: $($tmpDownloadFilePath)" | |
# Cleanup any previously unmanaged tmp files | |
Get-ChildItem -Path "${PSScriptRoot}\*.tmp" | Remove-Item | |
Invoke-WebRequest -Uri $DownloadUri -OutFile $tmpDownloadFilePath -UseBasicParsing | |
$global:ProgressPreference = 'Continue' | |
if ( (Test-Path -Path $tmpDownloadFilePath -PathType Leaf) -eq $true ) { | |
$tmpDownloadFileVersion = (Get-Item -Path $tmpDownloadFilePath).VersionInfo.ProductVersionRaw | |
Write-Host "tmpDownloadFileVersion: $($tmpDownloadFileVersion)" | |
if ([System.Version]::TryParse($tmpDownloadFileVersion, [ref][System.Version]'0.0.0.0') -eq $true) { | |
Move-Item -Path $tmpDownloadFilePath -Destination $DownloadFilePath -Force | |
} | |
} | |
} | |
$DownloadFileVersion = (Get-Item -Path $DownloadFilePath -ErrorAction SilentlyContinue).VersionInfo.ProductVersionRaw | |
Write-Host "DownloadFileVersion: $($DownloadFileVersion)" | |
$InstalledFileName = "OneDrive.exe" | |
Write-Host "InstalledFileName: $($InstalledFileName)" | |
$InstalledFilePath = Join-Path -Path "${env:ProgramFiles}\Microsoft OneDrive" -ChildPath $InstalledFileName | |
Write-Host "InstalledFilePath: $($InstalledFilePath)" | |
$InstalledFileVersion = (Get-Item -Path $InstalledFilePath -ErrorAction SilentlyContinue).VersionInfo.ProductVersionRaw | |
Write-Host "InstalledFileVersion: $($InstalledFileVersion)" | |
$Status = 'Unknown' | |
switch ($true) { | |
{ [System.Version]::TryParse($DownloadFileVersion, [ref][System.Version]'0.0.0.0') -eq $false } { $Status = 'DownloadMissing'; break } | |
{ [System.Version]::TryParse($InstalledFileVersion, [ref][System.Version]'0.0.0.0') -eq $false -and [System.Version]::TryParse($DownloadFileVersion, [ref][System.Version]'0.0.0.0') -eq $true } { $Status = 'Install'; break } | |
{ [System.Version]::TryParse($InstalledFileVersion, [ref][System.Version]'0.0.0.0') -eq $true -and $DownloadFileVersion -gt $InstalledFileVersion } { $Status = 'Update'; break } | |
{ [System.Version]::TryParse($InstalledFileVersion, [ref][System.Version]'0.0.0.0') -eq $true -and $DownloadFileVersion -eq $InstalledFileVersion } { $Status = 'Installed'; break } | |
{ [System.Version]::TryParse($InstalledFileVersion, [ref][System.Version]'0.0.0.0') -eq $true -and $DownloadFileVersion -lt $InstalledFileVersion } { $Status = 'NewerInstalled'; break } | |
} | |
Write-Host "Status: $($Status)" | |
$StartProcessSplat = @{ | |
FilePath = $DownloadFilePath | |
ArgumentList = "/silent /allusers" | |
WindowStyle = 'Hidden' | |
Wait = $true | |
WhatIf = $true | |
} | |
switch ($Status) { | |
{ $_ -eq 'NewerInstalled' } { | |
# Do nothing | |
break | |
} | |
{ $_ -eq 'Installed' } { | |
# Do nothing | |
break | |
} | |
{ $_ -eq 'Update' } { | |
Write-Host "Updating..." | |
$StartProcessSplat.WhatIf = $false | |
break | |
} | |
{ $_ -eq 'Install' } { | |
Write-Host "Installing..." | |
$StartProcessSplat.WhatIf = $false | |
break | |
} | |
} | |
Write-Host "StartProcessSplat: $($StartProcessSplat | ConvertTo-Json -Compress)" | |
if ($StartProcessSplat.WhatIf -eq $false) { | |
Write-Host "Executing..." | |
Start-Process @StartProcessSplat | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment