{"id":583,"date":"2023-06-02T20:25:22","date_gmt":"2023-06-02T19:25:22","guid":{"rendered":"https:\/\/blog.van-daag.nl\/?p=583"},"modified":"2023-09-11T15:24:49","modified_gmt":"2023-09-11T14:24:49","slug":"using-1-intune-to-rule-all-winget-apps","status":"publish","type":"post","link":"https:\/\/van-daag.nl\/?p=583","title":{"rendered":"Using 1 Intune to rule all Winget Apps"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>08-09-2023 &#8211; Updated the Scripts. I found 1 issue about source agreements<\/strong><br><strong>11-09-2023 &#8211; Script is not working with Zoom (System) <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the internet I see a lot of scipts to use Winget with the use of Task scheduler to update all.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the company that I work for there apps that may not be updated. So I need to specify the update apps.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With the use of the 2 sites below I created 1 Script to .intunewin<br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.nielskok.tech\/intune\/use-winget-with-intune\/\">Use Winget with Intune &#8211; NielsKok.Tech<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/call4cloud.nl\/2021\/05\/cloudy-with-a-chance-of-winget\/\">Install | Deploy | Use Winget | System context | Intune (call4cloud.nl)<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You need the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/Microsoft\/Microsoft-Win32-Content-Prep-Tool\" target=\"_blank\">Win32App packaging tool<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Generate with IntuneWinAppUtil this script below.<br>The rest is explained on the Internet<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then you can use this Install Command: <strong>powershell -executionpolicy bypass -file IntuneWinget.ps1 -Install<\/strong> <strong>&lt;Winget.ID&gt;<\/strong> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To Uninstall command: <strong>powershell -executionpolicy bypass -file IntuneWinget.ps1 -Uninstall &lt;Winget.ID&gt;<\/strong> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<br><strong>powershell -executionpolicy bypass -file IntuneWinget.ps1 -Install VideoLAN.VLC<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>powershell -executionpolicy bypass -file IntuneWinget.ps1 -Uninstall VideoLAN.VLC<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre title=\"IntuneWinget.ps1\" class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell line-numbers\">Param(\n    [switch]$Install,\n    [switch]$Uninstall,\n    $PackageName\n)\n\nif ($Install) {\n$AppInstaller = Get-AppxProvisionedPackage -Online | Where-Object DisplayName -eq Microsoft.DesktopAppInstaller\n\n#Start Logging\nStart-Transcript -Path \"C:\\ProgramData\\Microsoft\\IntuneManagementExtension\\Logs\\$($PackageName)_Install.log\" -Append\n\nIf($AppInstaller.Version -lt \"2022.506.16.0\") {\n\n    Write-Host \"Winget is not installed, trying to install latest version from Github\" -ForegroundColor Yellow\n\n    Try {\n    Write-Host \"Creating Winget Packages Folder\" -ForegroundColor Yellow\n\n        if (!(Test-Path -Path C:\\ProgramData\\WinGetPackages)) {\n            New-Item -Path C:\\ProgramData\\WinGetPackages -Force -ItemType Directory\n        }\n\n        Set-Location C:\\ProgramData\\WinGetPackages\n\n#Downloading Packagefiles\n        #Microsoft.UI.Xaml.2.7.0\n        Invoke-WebRequest -Uri \"https:\/\/www.nuget.org\/api\/v2\/package\/Microsoft.UI.Xaml\/2.7.0\" -OutFile \"C:\\ProgramData\\WinGetPackages\\microsoft.ui.xaml.2.7.0.zip\"\n        Expand-Archive C:\\ProgramData\\WinGetPackages\\microsoft.ui.xaml.2.7.0.zip -Force\n        #Microsoft.VCLibs.140.00.UWPDesktop\n        Invoke-WebRequest -Uri \"https:\/\/aka.ms\/Microsoft.VCLibs.x64.14.00.Desktop.appx\" -OutFile \"C:\\ProgramData\\WinGetPackages\\Microsoft.VCLibs.x64.14.00.Desktop.appx\"\n        #Winget\n        Invoke-WebRequest -Uri \"https:\/\/github.com\/microsoft\/winget-cli\/releases\/latest\/download\/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle\" -OutFile \"C:\\ProgramData\\WinGetPackages\\Winget.msixbundle\"\n        #Installing dependencies + Winget\n        Add-ProvisionedAppxPackage -online -PackagePath:.\\Winget.msixbundle -DependencyPackagePath .\\Microsoft.VCLibs.x64.14.00.Desktop.appx,.\\microsoft.ui.xaml.2.7.0\\tools\\AppX\\x64\\Release\\Microsoft.UI.Xaml.2.7.Appx -SkipLicense\n\n        Write-Host \"Starting sleep for Winget to initiate\" -Foregroundcolor Yellow\n        Start-Sleep 2\n    }\n    Catch {\n        Throw \"Failed to install Winget\"\n        Break\n    }\n\n    }\nElse {\n    Write-Host \"Winget already installed, moving on\" -ForegroundColor Green\n}\n\n\n#Trying to install Package with Winget\nIF ($PackageName){\n    try {\n        Write-Host \"Installing $($PackageName) via Winget\" -ForegroundColor Green\n\n        $ResolveWingetPath = Resolve-Path \"C:\\Program Files\\WindowsApps\\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\"\n        if ($ResolveWingetPath){\n               $WingetPath = $ResolveWingetPath[-1].Path\n        }\n    \n        $config\n        cd $wingetpath\n        $InstalledApps = .\\winget.exe list --id $PackageName --accept-source-agreements --accept-package-agreements\n\n        if ($InstalledApps -eq \"No installed package found matching input criteria.\") {\n             Write-Host \"$($PackageName) is not installed\"\n               .\\winget.exe install $PackageName --silent --accept-source-agreements --accept-package-agreements }\n        else {}\n       \n        \n        \n\n\n$content = @\"\n# Goto Winget install dir\ncd \"C:\\Program Files\\WindowsApps\\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\"\n.\\winget.exe upgrade $PackageName --silent --force --accept-package-agreements --accept-source-agreements \n#### Remove shortcut on public desktop.\n    Get-childitem -Path \"C:\\Users\\Public\\Desktop\\*.lnk\" -recurse | where-object {$_.lastwritetime -gt (get-date).addDays(-1) -and -not $_.PSIsContainer} | Remove-Item -Force\n    \n\"@\n\n# create custom folder and write PS script\n$path = $(Join-Path $env:ProgramData IntuneScripts)\nif (!(Test-Path $path))\n{\nNew-Item -Path $path -ItemType Directory -Force -Confirm:$false\n}\nOut-File -FilePath $(Join-Path $env:ProgramData IntuneScripts\\$PackageName.ps1) -Encoding unicode -Force -InputObject $content -Confirm:$false\n \n# register script as scheduled task\n$Time = New-ScheduledTaskTrigger -AtLogOn\n$User = \"SYSTEM\"\n$Action = New-ScheduledTaskAction -Execute \"powershell.exe\" -Argument \"-ex bypass -file `\"C:\\ProgramData\\IntuneScripts\\$PackageName.ps1`\"\"\nRegister-ScheduledTask -TaskPath \"INTUNE\" -TaskName \"Upgrade$PackageName\" -Trigger $Time -User $User -Action $Action -Force\nStart-ScheduledTask -TaskPath \"INTUNE\" -TaskName \"Upgrade$PackageName\"\n\n    }\n    Catch {\n        Throw \"Failed to install package $($_)\"\n    }\n#### Remove shortcut on public desktop.\nGet-childitem -Path \"C:\\Users\\Public\\Desktop\\*.lnk\" -recurse | where-object {$_.lastwritetime -gt (get-date).addDays(-1) -and -not $_.PSIsContainer} | Remove-Item -Force\n\n\n}\nElse {\n    Write-Host \"Package $($PackageName) not available\" -ForegroundColor Yellow\n}\nStop-Transcript\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nif ($Uninstall) {\n\n#Start Logging\nStart-Transcript -Path \"C:\\ProgramData\\Microsoft\\IntuneManagementExtension\\Logs\\$($PackageName)_Uninstall.log\" -Append\n\n#Detect Apps\n$InstalledApps = winget list --id $PackageName\n\nif ($InstalledApps) {\n    \n    Write-Host \"Trying to uninstall $($PackageName)\"\n    \n    try {        \n        $ResolveWingetPath = Resolve-Path \"C:\\Program Files\\WindowsApps\\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\"\n        if ($ResolveWingetPath){\n               $WingetPath = $ResolveWingetPath[-1].Path\n        }\n    \n        $config\n        cd $wingetpath\n\n        .\\winget.exe uninstall $PackageName --silent --accept-source-agreements --accept-package-agreements\n\n        \n    ## Remove scheduled Task\n    Write-Host \"Trying to remove Upgrade$($PackageName) Scheduled Task\"\n    Unregister-ScheduledTask -TaskName \"Upgrade$PackageName\" -Confirm:$false\n\n    ## Remove PS \n    Write-Host \"Trying to remove Powershell Script\"\n    $todelete = Join-Path $env:ProgramData IntuneScripts\\$PackageName.ps1 \n    Remove-Item -Path $todelete -Recurse -Force\n\n    }\n    catch {\n        Throw \"Failed to uninstall $($PackageName)\"\n    }\n}\nelse {\n    Write-Host \"$($PackageName) is not installed or detected\"\n}\n\nStop-Transcript\n\n\n\n}\nRemove-Variable * -ErrorAction SilentlyContinue<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The only script that needs to be change is the detection script.<br>Change the <strong>$PackageName = &#8220;&lt;Winget.id&gt;&#8221;<\/strong> to the correct one<\/p>\n\n\n\n<pre title=\"detect.ps1\" class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">#Fill this variable with the Winget package ID\n$PackageName = \"VideoLAN.VLC\"\n\n#Start Logging\nStart-Transcript -Path \"C:\\ProgramData\\Microsoft\\IntuneManagementExtension\\Logs\\$($PackageName)_detect.log\" -Append \n\ntry {\nWrite-host \"Checking Winget Path\"\n$ResolveWingetPath = Resolve-Path \"C:\\Program Files\\WindowsApps\\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\"\n    if ($ResolveWingetPath){\n           $WingetPath = $ResolveWingetPath[-1].Path\n    }\n\n$config\nWrite-host $ResolveWingetPath\nWrite-Host $WingetPath\ncd $wingetpath\n\n\n$CheckPSUpdate = Join-Path $env:ProgramData IntuneScripts\\$PackageName.ps1\n$CheckDir = Test-Path $CheckPSUpdate\n\nWrite-Host $CheckDir \n\n\n$InstalledApps = .\\winget.exe list --id $PackageName --accept-source-agreements --accept-package-agreements\n}\nCatch { \n    Throw \"Failed to detect $($PackageName)\"\n    }\n\nif ($InstalledApps -eq \"No installed package found matching input criteria.\") {\n    Write-Host \"$($PackageName) is not installed\"\n    Stop-Transcript\n    Exit 1\n}\n\n\nelse {\nIf ($CheckDir -eq $False)\n        {\n        Write-Host \"$($PackageName).ps1 not found\"\n        Stop-Transcript\n        Exit 1\n        }\n\n\n\n    \n}\n\n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you need more Info let me know.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>08-09-2023 &#8211; Updated the Scripts. I found 1 issue about source agreements11-09-2023 &#8211; Script is not working with Zoom (System) On the internet I see a lot of scipts to use Winget with the use of Task scheduler to update all. For the company that I work for there apps that may not be updated. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[53,48,40,11],"tags":[],"class_list":["post-583","post","type-post","status-publish","format-standard","hentry","category-blogs","category-intune","category-m365","category-powershell-script"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/van-daag.nl\/index.php?rest_route=\/wp\/v2\/posts\/583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/van-daag.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/van-daag.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/van-daag.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/van-daag.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=583"}],"version-history":[{"count":13,"href":"https:\/\/van-daag.nl\/index.php?rest_route=\/wp\/v2\/posts\/583\/revisions"}],"predecessor-version":[{"id":690,"href":"https:\/\/van-daag.nl\/index.php?rest_route=\/wp\/v2\/posts\/583\/revisions\/690"}],"wp:attachment":[{"href":"https:\/\/van-daag.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/van-daag.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/van-daag.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}