<# .NOTES =========================================================================== Title: KACE SMA Agent Deployment 1 Liner Created on: 9/25/2019 Created by: Eric Weintraub Organization: Quest Software Last Updated: 2024-Jun-27 =========================================================================== .DESCRIPTION Will Deploy SMAAgent using following Install String Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://isops-static.quest.com/downloads/quest/kace/smaagent/win/Install_KSMAAgent_Lab.ps1.txt')) #> Clear-Host $Error.clear() ######################################################################## # # # User Vars # # ######################################################################## # MSI File Name of AMP Agent $AgentInstallFileName = "ampagent-14.1.13-x64.msi" $AgentInstallHost = "k1000-lab.quest.com" $AgentInstallToken = "pvCoTwfxHu-vqMnD7Gvk89UYAD1asDcBwoep23V74w-P1GUlYMZSzg" # URL Path for Download of AMP Agent $url = "https://isops-static.quest.com/downloads/quest/kace/smaagent/win/$($AgentInstallFileName)" ######################################################################## # # # Functions # # ######################################################################## function write-logEntry ($logSource,$logMessage,$newLine) { if ($LogFilePath -eq $null) {$LogFilePath = "c:\temp\log.log"} if ($logSource.GetType().Name -eq "String") {$logSource = $logSource.ToUpper()} $FullLogString = "$(Get-Date -Format yyyy-MM-dd-HH:mm.ss.fff) | [$($logSource)] $($logMessage)" Write-Output $FullLogString if ($newLine -eq $true) { Write-Output $FullLogString | Out-File $LogFilePath } else { Write-Output $FullLogString | Out-File $LogFilePath -Append } } function Fix-PowerShellOutputRedirectionBug { # PowerShell v2/3 caches the output stream. Then it throws errors due # to the FileStream not being what is expected. Fixes "The OS handle's # position is not what FileStream expected. Do not use a handle # simultaneously in one FileStream and in Win32 code or another # FileStream." $poshMajorVerion = $PSVersionTable.PSVersion.Major if ($poshMajorVerion -lt 4) { write-logEntry -logSource "Fix-PowerShellOutputRedirectionBug" -logMessage "Attempting to fix PowerShell Output " try{ # http://www.leeholmes.com/blog/2008/07/30/workaround-the-os-handles-position-is-not-what-filestream-expected/ plus comments $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField" $objectRef = $host.GetType().GetField("externalHostRef", $bindingFlags).GetValue($host) $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty" $consoleHost = $objectRef.GetType().GetProperty("Value", $bindingFlags).GetValue($objectRef, @()) [void] $consoleHost.GetType().GetProperty("IsStandardOutputRedirected", $bindingFlags).GetValue($consoleHost, @()) $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField" $field = $consoleHost.GetType().GetField("standardOutputWriter", $bindingFlags) $field.SetValue($consoleHost, [Console]::Out) [void] $consoleHost.GetType().GetProperty("IsStandardErrorRedirected", $bindingFlags).GetValue($consoleHost, @()) $field2 = $consoleHost.GetType().GetField("standardErrorWriter", $bindingFlags) $field2.SetValue($consoleHost, [Console]::Error) } catch { write-logEntry -logSource "Fix-PowerShellOutputRedirectionBug" -logMessage "Unable to apply redirection fix." } } } Function Fix-SecurityProtocol () { # Attempt to set highest encryption available for SecurityProtocol. # PowerShell will not set this by default (until maybe .NET 4.6.x). This # will typically produce a message for PowerShell v2 (just an info # message though) write-logEntry -logSource "Fix-SecurityProtocol" -logMessage "Attempting to fix SSL/TLS Settings" try { # Set TLS 1.2 (3072), then TLS 1.1 (768), then TLS 1.0 (192), finally SSL 3.0 (48) # Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't # exist in .NET 4.0, even though they are addressable if .NET 4.5+ is # installed (.NET 4.5 is an in-place upgrade). [System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor 192 -bor 48 } catch { write-logEntry -logSource "Fix-SecurityProtocol" -logMessage "Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to upgrade to .NET Framework 4.5+ and PowerShell v3+" } } ######################################################################## # # # STATIC Vars # # ######################################################################## # Start Time $StartTime = Get-Date # Temp Directory Stuff and Logs if ($env:TEMP -eq $null) { $env:TEMP = Join-Path $env:SystemDrive 'temp' } $KACETempDir = Join-Path $env:TEMP "KACE" $tempDir = Join-Path $KACETempDir "SMAAgent" if (![System.IO.Directory]::Exists($tempDir)) {[void][System.IO.Directory]::CreateDirectory($tempDir)} $file = Join-Path $tempDir $AgentInstallFileName $LogFilePath = Join-Path $tempDir "KSMAPS.log" ######################################################################## # # # MAIN # # ######################################################################## write-logEntry -logSource "STARTING" -logMessage "-------------Starting Process---------------" -newLine $true #PreReqs Fix-PowerShellOutputRedirectionBug Fix-SecurityProtocol write-logEntry -logSource "Download" -logMessage "Downloading file from $($URL)" try { (New-Object System.Net.WebClient).DownloadFile($url, $file) write-logEntry -logSource "Download" -logMessage "Download Complete to: $($File)" write-logEntry -logSource "install" -logMessage "Installing Agent... " msiexec.exe /i $file /qn HOST=$AgentInstallHost TOKEN=$AgentInstallToken write-logEntry -logSource "install" -logMessage "Agent Installed!" } catch { write-logEntry -logSource "ERROR" -logMessage "Failed to Download or Install: $($_)" } ######################################################################## # # # FIN # # ######################################################################## $EndTime = Get-Date write-logEntry "FINISHING" "-------------Completed Process--------------" $TotalRunTime = $EndTime - $StartTime write-logEntry "FINISHED" "Total Time: $TotalRunTime"