Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cameron-huntress committed Dec 1, 2023
1 parent b4a18a4 commit 0987fe5
Showing 1 changed file with 64 additions and 13 deletions.
77 changes: 64 additions & 13 deletions Datto-RMM/scripts/InstallHuntress.dattormm.comstore.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<<<<<<< HEAD
=======
# Copyright (c) 2023 Huntress Labs, Inc.
# All rights reserved.
#
Expand Down Expand Up @@ -33,25 +31,32 @@
param (
[string]$acctkey,
[string]$orgkey,
[string]$tags,
[switch]$reregister,
[switch]$reinstall,
[switch]$uninstall,
[switch]$repair
)

$TagsKey = "__TAGS__"

# The account key should be stored in the DattoRMM account variable HUNTRESS_ACCOUNT_KEY
$AccountKey = "__ACCOUNT_KEY__"
if ($env:HUNTRESS_ACCOUNT_KEY) {
$AccountKey = $env:HUNTRESS_ACCOUNT_KEY
}

# Use the CS_PROFILE_NAME environment variable as the OrganizationKey
# This should always be set by the DattoRMM agent. If not, there is likely
# an issue with the agent.
$OrganizationKey = $env:CS_PROFILE_NAME
if (!$env:CS_PROFILE_NAME) { $OrganizationKey = 'MISSING_CS_PROFILE_NAME' }


##############################################################################
## Begin user modified variables
##############################################################################


# Set to "Continue" to enable verbose logging.
$DebugPreference = "SilentlyContinue"

Expand All @@ -70,8 +75,8 @@ $estimatedSpaceNeeded = 200111222
##############################################################################

# These are used by the Huntress support team when troubleshooting.
$ScriptVersion = "Version 2, major revision 8, 2023 Jul 24, "
$ScriptType = "DattoRMM"
$ScriptVersion = "Version 2, major revision 7, 2023 May 1, "
$ScriptType = "PowerShell"

# variables used throughout this script
$X64 = 64
Expand Down Expand Up @@ -122,6 +127,10 @@ if ( ! [string]::IsNullOrEmpty($orgkey) ) {
$OrganizationKey = $orgkey
}

# Check for tags specified on the command line.
if ( ! [string]::IsNullOrEmpty($tags) ) {
$TagsKey = $tags
}

# pick the appropriate file to download based on the OS version
if ($LegacyCommandsRequired -eq $true) {
Expand Down Expand Up @@ -210,6 +219,33 @@ function Test-Parameters {
LogMessage "Parameters verified."
}

# Force kill a process by process name
function KillProcessByName {
param(
[Parameter(Mandatory = $true)]
[string]$ProcessName
)

$processes = Get-Process | Where-Object { $_.ProcessName -eq $ProcessName }
$processCount = $processes | Measure-Object | Select-Object -ExpandProperty Count

if ($processCount -eq 0) {
LogMessage "No processes with the name '$ProcessName' are currently running."
}
else {
foreach ($process in $processes) {
try {
$processID = $process.Id
Stop-Process -Id $processID -Force
LogMessage "Killed process '$ProcessName' (ID $processID) successfully."
}
catch {
LogMessage "Failed to kill process '$ProcessName' (ID $processID): $($_.Exception.Message)"
}
}
}
}

# check to see if the Huntress service exists (agent or updater)
function Confirm-ServiceExists ($service) {
if (Get-Service $service -ErrorAction SilentlyContinue) {
Expand Down Expand Up @@ -330,8 +366,12 @@ function Install-Huntress ($OrganizationKey) {
verifyInstaller($InstallerPath)

LogMessage "Executing installer..."

$process = Start-Process $InstallerPath "/ACCT_KEY=`"$AccountKey`" /ORG_KEY=`"$OrganizationKey`" /S" -PassThru
# if $Tags value exists install using the provided tags, otherwise no tags
if (($Tags) -or ($TagsKey -ne "__TAGS__")) {
$process = Start-Process $InstallerPath "/ACCT_KEY=`"$AccountKey`" /ORG_KEY=`"$OrganizationKey`" /TAGS=`"$TagsKey`" /S" -PassThru
} else {
$process = Start-Process $InstallerPath "/ACCT_KEY=`"$AccountKey`" /ORG_KEY=`"$OrganizationKey`" /S" -PassThru
}

try {
$process | Wait-Process -Timeout $timeout -ErrorAction Stop
Expand Down Expand Up @@ -375,7 +415,7 @@ function Test-Installation {
Start-Sleep -Milliseconds 250
}
if ( ! $didAgentRegister) {
$err = "WARNING: It does not appear the agent has succesfully registered. Check 3rd party AV exclusion lists to ensure Huntress is excluded."
$err = "WARNING: It does not appear the agent has successfully registered. Check 3rd party AV exclusion lists to ensure Huntress is excluded."
Write-Output $err -ForegroundColor white -BackgroundColor red
LogMessage ($err + $SupportMessage)
}
Expand Down Expand Up @@ -589,6 +629,11 @@ function uninstallHuntress {
Stop-Service "huntressupdater" -ErrorAction SilentlyContinue
Stop-Service "huntressagent" -ErrorAction SilentlyContinue

# Force kill the executables so they're not hangin around
KillProcessByName "HuntressAgent.exe"
KillProcessByName "HuntressUpdater.exe"
KillProcessByName "HuntressRio.exe"

# attempt to use the built in uninstaller, if not found use the uninstallers built into the Agent and Updater
if (Test-Path $agentPath) {
# run uninstaller.exe, if not found run the Agent's built in uninstaller and the Updater's built in uninstaller
Expand Down Expand Up @@ -810,9 +855,15 @@ function copyLogAndExit {
Start-Sleep 1
$agentPath = getAgentPath
$logLocation = Join-Path $agentPath "HuntressPoShInstaller.log"
if (!(Test-Path -path $agentPath)) {New-Item $agentPath -Type Directory}
Copy-Item -Path $DebugLog -Destination $logLocation -Force
Write-Output "$($DebugLog) copied to $agentPath"

# If this is an unistall, we'll leave the log in the C:\temp dir otherwise,
# we'll copy the log to the huntress directory
if (!$uninstall){
if (!(Test-Path -path $agentPath)) {New-Item $agentPath -Type Directory}
Copy-Item -Path $DebugLog -Destination $logLocation -Force
Write-Output "$($DebugLog) copied to $agentPath"
}

Write-Output "Script complete"
exit 0
}
Expand Down Expand Up @@ -875,6 +926,7 @@ function main () {
$masked = $AccountKey.Substring(0,4) + "************************" + $AccountKey.SubString(28,4)
LogMessage "AccountKey: '$masked'"
LogMessage "OrganizationKey: '$OrganizationKey'"
LogMessage "Tags: $($Tags)"
}

# reregister > reinstall > uninstall > install (in decreasing order of impact)
Expand Down Expand Up @@ -918,5 +970,4 @@ try {
$ErrorMessage = $_.Exception.Message
LogMessage $ErrorMessage
copyLogAndExit
}
>>>>>>> parent of d661565 (Update deployment ps1 for DattoRMM)
}

0 comments on commit 0987fe5

Please sign in to comment.