Skip to content

Instantly share code, notes, and snippets.

Last active July 26, 2022 19:53
Show Gist options
  • Save sayedihashimi/64736331b273162a1e76 to your computer and use it in GitHub Desktop.
Save sayedihashimi/64736331b273162a1e76 to your computer and use it in GitHub Desktop.
Samples showing how to download nuget in a scrpit to avoid checking it in
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="" DefaultTargets="GetNuget">
This is an MSBuild snippet that can be used to download nuget to the path
in the property NuGetExePath property.
1. Copy and paste this into your build script
2. Call the GetNuGet target before you use nuget.exe from $(NuGetExePath)
<NuGetExePath Condition=" '$(NuGetExePath)'=='' ">$(localappdata)\LigerShark\AzureJobs\tools\nuget.exe</NuGetExePath>
<NuGetDownloadUrl Condition=" '$(NuGetDownloadUrl)'=='' "></NuGetDownloadUrl>
<Target Name="GetNuget" Condition="!Exists('$(NuGetExePath)')">
<Message Text="Downloading nuget from [$(NuGetDownloadUrl)] to [$(NuGetExePath)]" Importance="high"/>
<_nugetexeitem Include="$(NuGetExePath)" />
<MakeDir Directories="@(_nugetexeitem->'%(RootDir)%(Directory)')"/>
FileName="$(NuGetExePath)" />
<PropertyGroup Condition=" '$(ls-msbuildtasks-path)'=='' ">
<ls-msbuildtasks-path Condition=" !Exists('$(ls-msbuildtasks-path)')">$(MSBuildFrameworkToolsPath)\Microsoft.Build.Tasks.v4.0.dll</ls-msbuildtasks-path>
<ls-msbuildtasks-path Condition=" !Exists('$(ls-msbuildtasks-path)')">$(windir)\Microsoft.NET\Framework\v4.0.30319\Microsoft.Build.Tasks.v4.0.dll</ls-msbuildtasks-path>
<UsingTask TaskName="DownloadFile" TaskFactory="CodeTaskFactory" AssemblyFile="$(ls-msbuildtasks-path)">
<!-- -->
<Address ParameterType="System.String" Required="true"/>
<FileName ParameterType="System.String" Required="true" />
<Reference Include="System" />
<Code Type="Fragment" Language="cs">
new System.Net.WebClient().DownloadFile(Address, FileName);
You can use this to get the path of nuget.exe. If nuget.exe is not in
the $toolsDir specfied it will be downloaded from $nugetDownloadUrl.
To use this in your script just copy/paste the Get-NuGet function below
and then call Get-NuGet to return the path to the .exe. It will take care
of downloading it if it's missing.
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\AzureJobs\tools\"),
$nugetDownloadUrl = ''
If nuget is not in the tools
folder then it will be downloaded there.
function Get-Nuget{
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"),
$nugetDownloadUrl = ''
if(!(Test-Path $toolsDir)){
New-Item -Path $toolsDir -ItemType Directory -Force | Out-Null
$nugetDestPath = Join-Path -Path $toolsDir -ChildPath nuget.exe
if(!(Test-Path $nugetDestPath)){
'Downloading nuget.exe' | Write-Verbose
(New-Object System.Net.WebClient).DownloadFile($nugetDownloadUrl, $nugetDestPath)
# double check that is was written to disk
if(!(Test-Path $nugetDestPath)){
throw 'unable to download nuget'
# return the path of the file
Get-Nuget -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment