Microsoft Monitoring Agent - powershell script problem

Occasional Contributor
Morning all. I've created a short script to help me modify the configuration of Microsoft Monitoring Agent installed on some servers. The script seems to work well with one exception (pardon the pun). If I run with an '-action remove' switch, supplying a workspaceID argument value, it successfully removes a workspaceID from the agent config but I get a timeout waiting for the response as shown below.
Any ideas on why I get the exception and how I can code for it? And of course keen to hear any views on how the script can be improved. Be gentle. 
console output....




PS C:\test> .\MMA_modify_workspace.ps1 -action remove 3109a0e6-520f-4da5-9c0e-xxxxxxxxxxxx
The wait operation timed out. (Exception from HRESULT: 0x80070102)
At C:\test\MMA_modify_workspace.ps1:43 char:5
+ $mma.ReloadConfiguration()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: 

) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Workspace 3109a0e6-520f-4da5-9c0e-xxxxxxxxxxxx removed.




5 Replies
Just wanted to confirm that I use the same commands...
Are you having this on multiple agents? Wat version are you using

@Thijs Lecomte Thanks for getting back.  So it seems so far to be confined to a couple of hosts, both of these are VMs on VMWare workstation.  I've only tested on a few servers though so not a big sample so far.  Servers are 2019, MMA agent is 10.20.18053.0 so the latest I beleive.


The issue only occurs when I attempt to remove a workspaceID.  In my tests the workspace ID I'm removing is the last one on the agent.  

Ahn that is possible.
I have a script that checks if it's the last workspace, it just removes the workspaces. Is this something you could implement?

I can share it if you want

@Thijs Lecomte Yes please, I'll try what you have and incorporate if possible.



$multipleWorkspaces = $false
        $mma = New-Object -ComObject 'AgentConfigManager.MgmtSvcCfg'
        $workspaces = $mma.GetCloudWorkspaces()
            $multipleWorkspaces = $true
            Write-Log -Message "Multiple workspaces configured" -Source $deployAppScriptFriendlyName


            Write-Log -Message "Removed workspace $workspaceId" -Source $deployAppScriptFriendlyName

            $appInstalled = Get-InstalledApplication -Name "Microsoft Monitoring Agent"
	        $appInstalledCount = $(($appInstalled | Measure-Object).Count)
	        Write-Log -Message "Detected $appInstalledCount instances of $appName" -Severity 1 -Source $deployAppScriptFriendlyName

	        foreach ($appInstallation in $appInstalled) {
		        Write-Log -Message "Processing $($appInstallation.DisplayName) $($appInstallation.DisplayVersion)" -Severity 1 -Source $deployAppScriptFriendlyName
		        Write-Log -Message "UninstallString: $($appInstallation.UninstallString)" -Severity 1 -Source $deployAppScriptFriendlyName
		        # If the uninstallstring has quotes, you need to remove them
		        $uninstallString = $appInstallation.UninstallString.Replace('"', "")
                $uninstallString = $appInstallation.UninstallString.Replace('MsiExec.exe /I', "")
                Write-Log -Message "Updated UninstallString: $($uninstallString)" -Severity 1 -Source $deployAppScriptFriendlyName
                Execute-MSI -Action Uninstall -Path $uninstallString
		        Write-Log -Message "Exit Code: $($execProcess.ExitCode)" -Severity 1 -Source $deployAppScriptFriendlyName
            Write-Log -Message "Uninstalled MMA" -Source $deployAppScriptFriendlyName


This is the script. Know that is uses PSADT so some functions will not work out of the box PS