Skip to content

Instantly share code, notes, and snippets.

@dansmith65
Last active February 24, 2024 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dansmith65/a862f301fce553b26db9689ad0f87b6a to your computer and use it in GitHub Desktop.
Save dansmith65/a862f301fce553b26db9689ad0f87b6a to your computer and use it in GitHub Desktop.
Install latest 64-bit version of Nodepad++ via PowerShell
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$homeUrl = 'https://notepad-plus-plus.org'
$res = Invoke-WebRequest -UseBasicParsing $homeUrl
if ($res.StatusCode -ne 200) {throw ("status code to getDownloadUrl was not 200: "+$res.StatusCode)}
$tempUrl = ($res.Links | Where-Object {$_.outerHTML -like "*Current Version *"})[0].href
if ($tempUrl.StartsWith("/")) { $tempUrl = "$homeUrl$tempUrl" }
$res = Invoke-WebRequest -UseBasicParsing $tempUrl
if ($res.StatusCode -ne 200) {throw ("status code to getDownloadUrl was not 200: "+$res.StatusCode)}
$dlUrl = ($res.Links | Where-Object {$_.href -like "*x64.exe"})[0].href
if ($dlUrl.StartsWith("/")) { $dlUrl = "$homeUrl$dlUrl" }
$installerPath = Join-Path $env:TEMP (Split-Path $dlUrl -Leaf)
Invoke-WebRequest $dlUrl -OutFile $installerPath
Start-Process -FilePath $installerPath -Args "/S" -Verb RunAs -Wait
Remove-Item $installerPath
@dansmith65
Copy link
Author

NOTE: can now get latest version from GitHub instead: https://api.github.com/repos/notepad-plus-plus/notepad-plus-plus/releases/latest

@cjerrington
Copy link

cjerrington commented Dec 1, 2020

I have created a update based on the GitHub API for those who need it.

# Set TLS support for Powershell and parse the JSON request
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$npp = Invoke-WebRequest -UseBasicParsing 'https://api.github.com/repos/notepad-plus-plus/notepad-plus-plus/releases/latest' | ConvertFrom-Json

# Get the download URL from the JSON object
$dlUrl = $npp.assets[2].browser_download_url 

# Get the file name
$outfile = $npp.assets[2].name

# Get the current directory and build the installer path
$cwd = (Get-Location).Path 
$installerPath = Join-Path $cwd $outfile

Write-Host "Silently Installing $($npp.name)... Please wait..."

# Start the download and save the file to the installerpath
Invoke-WebRequest -UseBasicParsing $dlUrl -OutFile $installerPath

# Silently install NotepadPlusPlus then remove the downloaded item
Start-Process -FilePath $installerPath -Args "/S" -Verb RunAs -Wait
Remove-Item $installerPath

@dansmith65
Copy link
Author

I have created a update based on the GitHub API for those who need it.

Awesome; thanks for sharing. I'll try this one out next time I need it.

@sodtom
Copy link

sodtom commented Sep 29, 2023

And to get the correct platform binary (x64, arm64, ...) & packet (exe, zip, ...) from the assets, you can add following lines to the above great code:

$nppPackage = "x64.exe"

and replace the $dlUrl and $outfile lines with

$dlUrl = $npp.assets | where { $_.name.Contains($nppPackage) -and !$_.name.Contains(".sig")} | Select -ExpandProperty browser_download_url
$outfile = $npp.assets | where { $_.name.Contains($nppPackage) -and !$_.name.Contains(".sig")} | Select -ExpandProperty name

@tomasgursky
Copy link

How do you set up detection rules in Intune if script downloads a new version when available?

@Crimsonize
Copy link

How do you set up detection rules in Intune if script downloads a new version when available?

You can possibly run this as a reoccurring remediation script and have the detection script invoke a web request that outputs the current available version and then compare that to the installed one. I don't have the code for that off hand, but I am looking into scripting this for my company so I will upload it here once done.

@Crimsonize
Copy link

Crimsonize commented Feb 24, 2024

@tomasgursky That actually did not take as much effort as I expected. Here is my detection script if you put it in a remediation. This matches when the notepad++ exe is either missing or not up to date. If you want to run this at the end of a script rather than as a separate detection script, then change $RemediationScript to $false

try {
	# Try checking to see if App is installed on the machine.
	# If there's an error when running the command, keep running the script.
	
	# Toggle if this output is for a detection script
	$RemediationScript = $true
	
	# Set outputs based on if this is a remediation script
	if ($RemediationScript -eq $true) {
		$Output1 = [int32]1
		$Output2 = [int32]0
	} else {
		$Output1 = [int32]0
		$Output2 = [int32]1
	}

	# Target App Info
	$AppName = "Notepad++"
	$AppExePath = "C:\Program Files\Notepad++\notepad++.exe"
	$npp = Invoke-WebRequest -UseBasicParsing 'https://api.github.com/repos/notepad-plus-plus/notepad-plus-plus/releases/latest' | ConvertFrom-Json

	$App = (Get-Item $AppExePath -ErrorAction $ErrorActionPreference).VersionInfo

	# If App is indeed present and up to date, that's great! End the script and exit.
	# When exiting, exit with an Output and an Exit code of $SuccessOutput to tell Intune the app was detected.
	if ($App.ProductName -eq $AppName) {
		Write-Output "$AppName detected."
		if ($npp.tag_name = "v" + $App.FileVersion) {
			Write-Output "$AppName is on the latest version"
			return [int32]$Output1
			[Environment]::Exit($Output1)
		} else {
			# If App isn't up to date, End the script and Exit
			# When exiting, exit with an Output and an Exit code of $FailOutput to tell Intune the app wasn't updated.
			Write-Output "$AppName not up to date. Update app"
			return [int32]$Output2
			[Environment]::Exit($Output2)
		}
	} else {
		# If App isn't present, End the script and Exit
		# When exiting, exit with an Output and an Exit code of $FailOutput to tell Intune the app wasn't detected, and needs installed.
		Write-Output "$AppName not detected. Install app"
		return [int32]$Output2
		[Environment]::Exit($Output2)
	}
}

# If anything else unexpected happens or the code doesn't run properly, exit with an error
catch {
	$errMsg = $_.Exception.Message
	# Return error message and exit
	return $errMsg
	[Environment]::Exit(1)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment