MMA-Deploy.ps1


<#PSScriptInfo
 
.VERSION 1.15
 
.GUID f66d3cd7-3ff5-4a00-acc3-7caa07d2c706
 
.AUTHOR anisio@ja.eti.br
 
.COMPANYNAME JA
 
.COPYRIGHT
 
.TAGS PSEdition_Desktop
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
Workspace03 added
Validate WMF Version
 
.PRIVATEDATA
 
#>
 











<#
.DESCRIPTION
 Install, configure and Update MMA-Agent
 If parameters not informed, the script read from registry: HKLM:\SOFTWARE\JA\KA-01034<Key same name of parameters>
 Normally, I call from a central point to run on all servers objects from AD:
 ================== Example ======================
     $AllServers = Get-ADComputer -Filter 'operatingSystem -like "*Server*" -and Enabled -eq "True"' -Properties operatingSystem
    foreach ($Server in $AllServers)
    {
        Invoke-Command -ComputerName $Server.DNSHostName -FilePath {C:\JA\KA-01034\MMA-Deploy.ps1}
    }
================= /Example ======================
#>
 
Param(
    # ID of first wWorkspace
    [Parameter(Mandatory=$false)][string]$workspaceId01,
    # Key of first workspace
    [Parameter(Mandatory=$false)][string]$workspaceKey01,
    # ID of second wWorkspace
    [Parameter(Mandatory=$false)][string]$workspaceId02,
    # Key of second workspace
    [Parameter(Mandatory=$false)][string]$workspaceKey02,
    # ID of second wWorkspace
    [Parameter(Mandatory=$false)][string]$workspaceId03,
    # Key of second workspace
    [Parameter(Mandatory=$false)][string]$workspaceKey03,
    #If this parameter is $True or "1" in Registry all existent workspaces will be removed
    [Parameter(Mandatory=$false)][bool]$UpdateConfig
)
<#
KA-01034
Versão 1.5 2019-07-31
www.ja.eti.br
References:
https://invoke-thebrain.com/2018/12/comparing-version-numbers-powershell/
https://searchenterprisedesktop.techtarget.com/tip/Using-WMI-and-the-CIM-standard-with-Windows-PowerShell
#>


# param(

# )
if ($PSVersionTable.PSVersion.Major -lt 5){
    Write-Host "Please upgrade the Windows Management Framework - KB2819745"
    Exit
}
if ($workspaceId01){}
else {
$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
$workspaceId03 = Get-ItemPropertyValue -Path HKLM:\SOFTWARE\JA\KA-01034 -Name workspaceId03
$workspaceKey03 = Get-ItemPropertyValue -Path HKLM:\SOFTWARE\JA\KA-01034 -Name workspaceKey03
[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-CimInstance Win32_Product -Filter "name='Microsoft Monitoring Agent'").Version
Write-Host $LastMMAVersion "Versao nuvem"
Write-Host $LocalMMAVersion "Versao em" $env:COMPUTERNAME
function fnUpdateConfig {
    param (
    )
    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()
    }
    if ($workspaceId03){
        Start-Sleep -Seconds 600  
        $mma.AddCloudWorkspace($workspaceId03, $workspaceKey03)
        $mma.ReloadConfiguration()
    }
    Start-Sleep -Seconds 15
    Write-Host "Listando Workspaces configurados em:" $env:COMPUTERNAME -ForegroundColor Green
    $mma.GetCloudWorkspaces()
    Set-ItemProperty -Path HKLM:\SOFTWARE\JA\KA-01034 -Name UpdateConfig -Value 0
}

$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 -UseBasicParsing
        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
        fnUpdateConfig
    }
    else {
            $mma = New-Object -ComObject 'AgentConfigManager.MgmtSvcCfg'
            $workspaceList = $mma.GetCloudWorkspaces()
            if (($workspaceList[0].workspaceId) -ne $workspaceId01) {
                fnUpdateConfig
            }
            if (($workspaceList[1].workspaceId) -ne $workspaceId02) {
                fnUpdateConfig
            }
    }
}
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 -UseBasicParsing
    .\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 600
    $mma = New-Object -ComObject 'AgentConfigManager.MgmtSvcCfg'
    if ($workspaceId02) {
        Start-Sleep -Seconds 600  
        $mma.AddCloudWorkspace($workspaceId02, $workspaceKey02)
        $mma.ReloadConfiguration()
    }
    if ($workspaceId03){
        Start-Sleep -Seconds 600  
        $mma.AddCloudWorkspace($workspaceId03, $workspaceKey03)
        $mma.ReloadConfiguration()
    }
    Start-Sleep -Seconds 15
    Write-Host "Listando Workspaces configurados em:" $env:COMPUTERNAME -ForegroundColor Green
    $mma.GetCloudWorkspaces()
}