Skip to content

Instantly share code, notes, and snippets.

@dansmith65
Last active February 24, 2024 03:18
Show Gist options
  • 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
@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