通过命令行安装翼子板(powershell)



我正在尝试编写一个PowerShell脚本来设置windows开发机器。我想使用winget,但我看不到任何简单的方法来使用命令行安装winget。您必须使用windows商店或从github下载msxibundle。

Invoke-WebRequest -Uri https://github.com/microsoft/winget-cli/releases/download/v1.3.2691/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle -OutFile .MicrosoftDesktopAppInstaller_8wekyb3d8bbwe.msixbundle

这两者都需要用户的交互,而不仅仅是运行脚本然后离开。有更好的方法吗?谢谢

我设计了一种不同的方法,它安装了最新版本:

# get latest download url
$URL = "https://api.github.com/repos/microsoft/winget-cli/releases/latest"
$URL = (Invoke-WebRequest -Uri $URL).Content | ConvertFrom-Json |
Select-Object -ExpandProperty "assets" |
Where-Object "browser_download_url" -Match '.msixbundle' |
Select-Object -ExpandProperty "browser_download_url"
# download
Invoke-WebRequest -Uri $URL -OutFile "Setup.msix" -UseBasicParsing
# install
Add-AppxPackage -Path "Setup.msix"
# delete file
Remove-Item "Setup.msix"

使用Windows PowerShell,而不是PowerShell Core,有一个命令可以帮助:Add-AppXPackage:

Add-AppXPackage -Path .MicrosoftDesktopAppInstaller_8wekyb3d8bbwe.msixbundle

应该允许您安装程序包。

希望这能帮助

这里有一个直接来自Microsoft的脚本。脚本还可以用于将winget安装到Windows沙箱中。

原始页面

$progressPreference = 'silentlyContinue'
$latestWingetMsixBundleUri = $(Invoke-RestMethod https://api.github.com/repos/microsoft/winget-cli/releases/latest).assets.browser_download_url | Where-Object {$_.EndsWith(".msixbundle")}
$latestWingetMsixBundle = $latestWingetMsixBundleUri.Split("/")[-1]
Write-Information "Downloading winget to artifacts directory..."
Invoke-WebRequest -Uri $latestWingetMsixBundleUri -OutFile "./$latestWingetMsixBundle"
Invoke-WebRequest -Uri https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx -OutFile Microsoft.VCLibs.x64.14.00.Desktop.appx
Add-AppxPackage Microsoft.VCLibs.x64.14.00.Desktop.appx
Add-AppxPackage $latestWingetMsixBundle
Install-Module -Name Microsoft.WinGet.Client

这对我来说非常有效

# get latest download url
$URL = "https://api.github.com/repos/microsoft/winget-cli/releases/latest"
$URL = (Invoke-WebRequest -Uri $URL).Content | ConvertFrom-Json |
Select-Object -ExpandProperty "assets" |
Where-Object "browser_download_url" -Match '.msixbundle' |
Select-Object -ExpandProperty "browser_download_url"
$LicenseFileURL = 'https://github.com/microsoft/winget-cli/releases/download/v1.2.10271/b0a0692da1034339b76dce1c298a1e42_License1.xml'
# download
Invoke-WebRequest -Uri $URL -OutFile "Setup.msix" -UseBasicParsing
Invoke-WebRequest -Uri $LicenseFileURL -OutFile  'license.xml' 
# install
#Add-AppxPackage -Path "Setup.msix" -LicensePath .license.xml
Add-AppxProvisionedPackage -PackagePath "Setup.msix" -LicensePath 'license.xml' -online 
# delete file
Remove-Item "Setup.msix"

我想指出,上面链接的由微软提供的代码并不准确。使用正确的PowerShell更新:

$progressPreference = 'silentlyContinue'
$latestWingetMsixBundleUri = $( Invoke-RestMethod https://api.github.com/repos/microsoft/winget-cli/releases/latest ).assets.browser_download_url | Where-Object { $_.EndsWith( ".msixbundle" ) }
$latestWingetMsixBundle = $latestWingetMsixBundleUri.Split("/")[-1]
Write-Information "Downloading winget to artifacts directory..."
Invoke-WebRequest -Uri $latestWingetMsixBundleUri -OutFile "./$latestWingetMsixBundle" 
Add-AppxPackage $latestWingetMsixBundle

最新更新