Microsoft Monitoring Agent - powershell script problem

%3CLINGO-SUB%20id%3D%22lingo-sub-1894247%22%20slang%3D%22en-US%22%3EMicrosoft%20Monitoring%20Agent%20-%20powershell%20script%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1894247%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22kvgmc6g5%20cxmmr5t8%20oygrvhab%20hcukyx3x%20c1et5uql%20ii04i59q%22%3EMorning%20all.%20I've%20created%20a%20short%20script%20to%20help%20me%20modify%20the%20configuration%20of%20Microsoft%20Monitoring%20Agent%20installed%20on%20some%20servers.%20The%20script%20seems%20to%20work%20well%20with%20one%20exception%20(pardon%20the%20pun).%20If%20I%20run%20with%20an%20'-action%20remove'%20switch%2C%20supplying%20a%20workspaceID%20argument%20value%2C%20it%20successfully%20removes%20a%20workspaceID%20from%20the%20agent%20config%20but%20I%20get%20a%20timeout%20waiting%20for%20the%20response%20as%20shown%20below.%3CDIV%20class%3D%22o9v6fnle%20cxmmr5t8%20oygrvhab%20hcukyx3x%20c1et5uql%20ii04i59q%22%3EAny%20ideas%20on%20why%20I%20get%20the%20exception%20and%20how%20I%20can%20code%20for%20it%3F%20And%20of%20course%20keen%20to%20hear%20any%20views%20on%20how%20the%20script%20can%20be%20improved.%20Be%20gentle.%26nbsp%3B%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23Powershell%20script%20to%20add%20or%20remove%20a%20Log%20Analytics%20workspace%20to%20the%20Microsoft%20Monitoring%20Agent%0A%23Usage%20-%20MMA-modify-workspace%20-action%20(list%2Fadd%2Fremove)%20-workspaceID%20-workspaceKey%0A%0A%23initialise%20the%20script%0Aparam(%0A%20%20%20%20%5Bstring%5D%20%24action%20%3D%20%22list%22%2C%0A%20%20%20%20%5Bstring%5D%20%24workspaceID%20%3D%20%22%22%2C%0A%20%20%20%20%5Bstring%5D%20%24workspaceKey%20%3D%20%22%22%0A)%0A%24mma%20%3D%20New-Object%20-ComObject%20'AgentConfigManager.MgmtSvcCfg'%0A%24usage%20%3D%20%22Usage%20-%20MMA-modify-workspace%20-action%20(list%2Fadd%2Fremove)%20-workspaceID%20-workspaceKey%E2%80%99%22%0A%0ASwitch%20(%24action)%20%23choose%20code%20based%20on%20the%20script%20action%20selected%20-%20default%20is%20list%0A%7B%0A%20list%20%7B%0A%20%20%20%20%24currentWorkspaces%20%3D%20%24mma.GetCloudWorkspaces()%0A%20%20%20%20if%20(%24currentWorkspaces.length%20-ne%200%20)%7B%0A%20%20%20%20%20%20%20%20%24currentWorkspaces%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20else%20%7B%0A%20%20%20%20%20%20%20%20Write-Output%20'No%20workspaces%20currently%20configured.'%0A%20%20%20%20%7D%0A%20%7D%0A%20add%20%7B%20%0A%20%20%20%20%0A%20%20%20%20%23test%20for%20arguments%20workspaceID%20and%20workspaceKey%0A%20%20%20%20if%20((%24workspaceID%20-ne%20%22%22)%20-and%20(%24workspaceKey%20-ne%20%22%22))%7B%0A%20%20%20%20%20%20%20%20%24mma.AddCloudWorkspace(%24workspaceId%2C%20%24workspaceKey)%0A%20%20%20%20%20%20%20%20%23Start-Sleep%20-Seconds%205%0A%20%20%20%20%20%20%20%20%24mma.ReloadConfiguration()%0A%20%20%20%20%20%20%20%20%24message%20%3D%20'New%20workspace%20'%20%2B%20%24workspaceID%20%2B%20'%20added.'%0A%20%20%20%20%20%20%20%20Write-Output%20%24message%0A%20%20%20%20%7D%0A%20%20%20%20else%20%7B%0A%20%20%20%20%20%20%20%20Write-Output%20%24usage%0A%20%20%20%20%7D%0A%0A%20%7D%0A%20remove%20%7B%0A%20%20%20%20%24mma.RemoveCloudWorkspace(%24workspaceId)%0A%20%20%20%20%23Start-Sleep%20-Seconds%205%0A%20%20%20%20%24mma.ReloadConfiguration()%0A%20%20%20%20%24message%20%3D%20'Workspace%20'%20%2B%20%24workspaceID%20%2B%20'%20removed.'%0A%20%20%20%20Write-Output%20%24message%0A%20%7D%0A%20Default%20%7B%20%24usage%20%7D%E2%80%8B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%20class%3D%22o9v6fnle%20cxmmr5t8%20oygrvhab%20hcukyx3x%20c1et5uql%20ii04i59q%22%3E%3CDIV%3Econsole%20output....%3CDIV%20class%3D%22o9v6fnle%20cxmmr5t8%20oygrvhab%20hcukyx3x%20c1et5uql%20ii04i59q%22%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EPS%20C%3A%5Ctest%26gt%3B%20.%5CMMA_modify_workspace.ps1%20-action%20remove%203109a0e6-520f-4da5-9c0e-xxxxxxxxxxxx%0AThe%20wait%20operation%20timed%20out.%20(Exception%20from%20HRESULT%3A%200x80070102)%0AAt%20C%3A%5Ctest%5CMMA_modify_workspace.ps1%3A43%20char%3A5%0A%2B%20%24mma.ReloadConfiguration()%0A%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%2B%20CategoryInfo%20%3A%20OperationStopped%3A%20%0A%0A%0A)%20%5B%5D%2C%20COMException%0A%2B%20FullyQualifiedErrorId%20%3A%20System.Runtime.InteropServices.COMException%0AWorkspace%203109a0e6-520f-4da5-9c0e-xxxxxxxxxxxx%20removed.%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1902716%22%20slang%3D%22en-US%22%3ERe%3A%20Microsoft%20Monitoring%20Agent%20-%20powershell%20script%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1902716%22%20slang%3D%22en-US%22%3EJust%20wanted%20to%20confirm%20that%20I%20use%20the%20same%20commands...%3CBR%20%2F%3EAre%20you%20having%20this%20on%20multiple%20agents%3F%20Wat%20version%20are%20you%20using%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1902952%22%20slang%3D%22en-US%22%3ERe%3A%20Microsoft%20Monitoring%20Agent%20-%20powershell%20script%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1902952%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F186539%22%20target%3D%22_blank%22%3E%40Thijs%20Lecomte%3C%2FA%3E%26nbsp%3BThanks%20for%20getting%20back.%26nbsp%3B%20So%20it%20seems%20so%20far%20to%20be%20confined%20to%20a%20couple%20of%20hosts%2C%20both%20of%20these%20are%20VMs%20on%20VMWare%20workstation.%26nbsp%3B%20I've%20only%20tested%20on%20a%20few%20servers%20though%20so%20not%20a%20big%20sample%20so%20far.%26nbsp%3B%20Servers%20are%202019%2C%20MMA%20agent%20is%2010.20.18053.0%20so%20the%20latest%20I%20beleive.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20issue%20only%20occurs%20when%20I%20attempt%20to%20remove%20a%20workspaceID.%26nbsp%3B%20In%20my%20tests%20the%20workspace%20ID%20I'm%20removing%20is%20the%20last%20one%20on%20the%20agent.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New 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.

@Mike_Elliott 

 

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

if($multipleWorkspaces){
            $mma.RemoveCloudWorkspace($WorkspaceId)
            $mma.ReloadConfiguration()

            Write-Log -Message "Removed workspace $workspaceId" -Source $deployAppScriptFriendlyName
        }
        else{

            $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