MMA-Deploy.ps1


<#PSScriptInfo
 
.VERSION 1.5
 
.GUID 13bb6a64-84e1-4f78-aac0-d781e62cf603
 
.AUTHOR www.ja.eti.br
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Install, configure and Update MMA-Agent
 
#>
 

Param()


<#
KA-01034
Versão 1.5 2019-07-31
www.ja.eti.br
https://invoke-thebrain.com/2018/12/comparing-version-numbers-powershell/
#>


# param(
# [Parameter(Mandatory=$true)][string]$workspaceId01,
# [Parameter(Mandatory=$true)][string]$workspaceKey01,
# [Parameter(Mandatory=$false)][string]$workspaceId02,
# [Parameter(Mandatory=$false)][string]$workspaceKey02,
# [Parameter(Mandatory=$false)][bool]$UpdateConfig
# )
$workspaceId01 = Get-ItemPropertyValue -Path HKLM:\SOFTWARE\JA\KA-01034 -Name workspaceId01
$workspaceKey01 = Get-ItemPropertyValue -Path HKLM:\SOFTWARE\JA\KA-01034 -Name workspaceKey01
$workspaceId02 = Get-ItemPropertyValue -Path HKLM:\SOFTWARE\JA\KA-01034 -Name workspaceId02
$workspaceKey02 = Get-ItemPropertyValue -Path HKLM:\SOFTWARE\JA\KA-01034 -Name workspaceKey02
[bool]$UpdateConfig = Get-ItemPropertyValue -Path HKLM:\SOFTWARE\JA\KA-01034 -Name UpdateConfig
[version]$LastMMAVersion = (Invoke-WebRequest -Uri www.ja.eti.br/data/mma-version.txt -UseBasicParsing).content
[version]$LocalMMAVersion = (Get-WmiObject win32_Product -Filter "name='Microsoft Monitoring Agent'").Version
Write-Host $LastMMAVersion "Versao nuvem"
Write-Host $LocalMMAVersion "Versao Local"
$WrkDir = "C:\JA\"
if ([IO.Directory]::Exists($WrkDir)) {}
else{
    mkdir "$WrkDir" -ErrorAction Stop
}
Set-Location -Path "$WrkDir"

if ($LocalMMAVersion) {
    if ($LocalMMAVersion -lt $LastMMAVersion) {#Realiza a atualizacao de versao do MMA
        Invoke-WebRequest -Uri "https://go.microsoft.com/fwlink/?LinkId=828603" -OutFile .\MMASetup-AMD64.exe
        Write-Host "Atualizando o MMA em" $env:COMPUTERNAME
        .\MMASetup-AMD64.exe /Q:A /R:N /C:"setup.exe /qn AcceptEndUserLicenseAgreement=1"
    }
    else {}
    if ($UpdateConfig){ #Quando este parametro estiver True todos workspaces sao removidos e atualizados
        Write-Host "Atualizando a configuracao em" $env:COMPUTERNAME
        $mma = New-Object -ComObject 'AgentConfigManager.MgmtSvcCfg'
        $workspaceList = $mma.GetCloudWorkspaces()
        foreach ($workspace in $workspaceList){
            $mma.RemoveCloudWorkspace($workspace.workspaceId)
            Write-Host $workspace.workspaceId
        }
        $mma.AddCloudWorkspace($workspaceId01, $workspaceKey01)
        $mma.ReloadConfiguration()
        if ($workspaceId02){
            Start-Sleep -Seconds 600  
            $mma.AddCloudWorkspace($workspaceId02, $workspaceKey02)
            $mma.ReloadConfiguration()
        }
        Start-Sleep -Seconds 15
        Write-Host "Listando Workspaces configurados em:" $env:COMPUTERNAME -ForegroundColor Green
        $mma.GetCloudWorkspaces()
    }
}
else {
    #Constatado que o MMA nao esta instaldo, excuta a instalacao
    Invoke-WebRequest -Uri "https://go.microsoft.com/fwlink/?LinkId=828603" -OutFile .\MMASetup-AMD64.exe
    .\MMASetup-AMD64.exe /Q:A /R:N /C:"setup.exe /qn ADD_OPINSIGHTS_WORKSPACE=1 OPINSIGHTS_WORKSPACE_ID=$workspaceId01 OPINSIGHTS_WORKSPACE_KEY=$workspaceKey01 AcceptEndUserLicenseAgreement=1"
    Write-Host "Instalando o MMA em" $env:COMPUTERNAME
    Start-Sleep -Seconds 15
    Wait-Process -Name "setup"
    $mma = New-Object -ComObject 'AgentConfigManager.MgmtSvcCfg'
    if ($workspaceId02) {
        Start-Sleep -Seconds 600  
        $mma.AddCloudWorkspace($workspaceId02, $workspaceKey02)
        $mma.ReloadConfiguration()
    }
    Start-Sleep -Seconds 15
    Write-Host "Listando Workspaces configurados em:" $env:COMPUTERNAME -ForegroundColor Green
    $mma.GetCloudWorkspaces()
}