File: C:/panopta_temp/panopta_agent_windows.ps1
function CheckForKeys {
param(
[string]$manifestFile
)
if(!(Test-Path -Path $manifestFile -PathType Leaf)){
Write-Host "Manifest file $manifestFile does not exist."
return $false
}
$items = Get-Content $manifestFile
$foundCustomerKey = $false
$foundServerKey = $false
foreach($item in $items){
Write-Host $item
$stuff = $item -split '='
#Write-Host First item is $stuff[0]
if($stuff[0].Contains("customer_key")) {
$foundCustomerKey = ($stuff.Length -eq 2)
break
} elseif ($stuff[0].Contains("server_key")){
$foundServerKey = ($stuff.Length -eq 2)
}
}
if($foundCustomerKey -or $foundServerKey){
return $true
}
Write-Host "customer_key or server_key were not found in $manifestFile"
return $false
}
function GetInstallLogBackupName{
$dateTime = Get-Date
$newLogName = "PanoptaAgentSetup_$($dateTime.ToString("yyyyMMddHHmmss")).log"
return $newLogName
}
function InstallAgent{
param(
[string]$uriPath,
[string]$installFile,
[string]$manifestFile
)
if(Test-Path PanoptaAgentSetup.log -PathType Leaf){
Write-Host Backing up existing log file
$newLogName = GetInstallLogBackupName
Rename-Item -Path PanoptaAgentSetup.log -NewName $newLogName -ErrorAction SilentlyContinue
}
try {
Invoke-WebRequest https://packages.panopta.com/win/$uriPath/$installFile -OutFile $installFile
}
catch {
Write-Host "Invoke-Webrequest failed with error: $PSItem"
exit
}
Start-Process -FilePath msiexec -Wait -ArgumentList "/q INSTALLACTION=`"Install`" /i $installFile MANIFESTFILE=`"$manifestFile`""
if(Test-Path PanoptaAgentSetup.log -PathType Leaf){
$newLogName = GetInstallLogBackupName
Copy-Item PanoptaAgentSetup.log -Destination $newLogName -ErrorAction SilentlyContinue
}
$errorCount = 0
if(!(Test-Path 'c:\Program Files (x86)\PanoptaAgent')){
++$errorCount;
Write-Host "Agent directory is not present."
}
$panoptaRegKey = 'HKLM:\SOFTWARE\PanoptaAgent'
$panoptaKey = (Get-Item -Path $panoptaRegKey -ErrorVariable keyError -ErrorAction SilentlyContinue)
if(!$panoptaKey){
++$errorCount
Write-Host Panopta key $panoptaRegKey does not exist
}
$panoptaKeyValue = Get-ItemProperty -Path $panoptaRegKey -ErrorVariable keyError -ErrorAction SilentlyContinue -Name 'Home'
if(!$panoptaKeyValue){
++$errorCount
Write-Host Could not locate Panopta Home registry value
}
$wmi_obj = get-wmiobject Win32_product | where-object {$_.Name -eq 'Panopta Agent' }
if(!$wmi_obj){
++$errorCount
Write-Host No installed Panopta product found.
}
$serviceStatus = Get-Service PanoptaAgent -ErrorVariable serviceError -ErrorAction SilentlyContinue
if(!$serviceStatus) {
++$errorCount
Write-Host Unable to obtain service status
} elseif($serviceStatus.Status -ne "Running"){
Write-Host Service is in status $serviceStatus.Status
++$errorCount
}
if($errorCount -gt 0){
Write-Host "Agent did not appear to install correctly. Please check the file PanoptaAgentSetup.log for errors"
} else {
Write-Host Installation complete.
}
}
If(!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Host User does not have Administrator privileges
Exit
}
$manifestFile = 'C:\PanoptaAgent.manifest'
$transformedArgs = @{}
for($i =0; $i -lt $args.count; $i++)
{
$argsName = $args[$i].TrimStart("-")
if("countermeasures" -eq $argsName){
$argsName = "enable_countermeasures"
$argVal = "true"
}
elseif("autoupdate" -eq $argsName) {
$argVal = "true"
}
elseif('ServerKey' -eq $argsName){
$argsName = 'server_key'
$argVal = $args[++$i]
}
elseif('dem' -eq $argsName){
$argsName = 'digital_experience'
$argVal = 'true'
}
else {
$argVal = $args[++$i] -join ','
}
if(($argsName -eq 'ManifestFile') -or ($argsName -eq 'manifest_file')){
$manifestFile = $argVal
}
else {
$transformedArgs[$argsName] = $argVal
}
}
if ([System.IO.File]::Exists($manifestFile)) {
Write-Output "Found existing manifest file in $manifestFile"
}
else
{
$createdManifest = New-Item -Path $manifestFile -ItemType File -ErrorAction SilentlyContinue -ErrorVariable fileError
if(!$createdManifest) {
Write-Host Unable to create manifest file $manifestFile
if($fileError){
Write-Host $fileError
}
Exit
}
Write-Host Created manifest file $manifestFile
}
foreach($taKey in $transformedArgs.Keys){
$_argName = $taKey
$_argValue = $transformedArgs[$taKey]
if (Select-String -Pattern $_argName -Quiet $manifestFile) {
Write-Output "Updating $_argName in manifest file with value $_argValue"
(Get-Content $manifestFile) | ForEach-Object {$_ -replace "^$_argName = .*$", "$_argName = $_argValue"} | Out-File $manifestFile
}
else {
Write-Output "Adding $_argName in manifest file with value $_argValue"
Write-Output "$_argName = $_argValue" | Out-File -Append $manifestFile
}
}
if(!(CheckForKeys $manifestFile)){
Exit
}
Write-Host Manifest validated
$netversions = gci 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse | sort pschildname -des
$DOTNETVER = 0
foreach ($netversion in $netversions) {
If ($netversion.GetValue("Version") -gt $DOTNETVER) {
$DOTNETVER = $netversion.GetValue("Version")
}
}
If ($DOTNETVER -ge 4.5) {
Write-Output ".NET 4.5+, running .NET4.5 installer"
InstallAgent dotnet45 panopta-agent-prod-NET4.5.latest.msi $manifestFile
}
ElseIf ($DOTNETVER -ge 4) {
Write-Output "This machine is running .NET4, running .NET4 installer"
InstallAgent dotnet4 panopta-agent-prod-NET4.0.latest.msi $manifestFile
}
Else {
Write-Output "Warning: Machine is not running compatible .NET version! Exiting"
Exit
}