Unlinking Servers from an Azure Workspace with PowerShell
The Remove-MMAWorkspace.ps1 script, provides a powerful way to unlink servers from an Azure workspace using PowerShell. In this blog post, we'll dive into the code and explore how it works.
Prerequisites
The Script
The Remove-MMAWorkspace function takes three parameters: WorkspaceId, ComputerName, and Credential. The function uses the AgentConfigManager.MgmtSvcCfg COM object to remove a cloud workspace from one or more computers.
function Remove-MMAWorkspace {
[cmdletbinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $workspaceId,
# add parameters for computername and credentials:
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string[]]
$ComputerName,
[PSCredential]
$Credential
)
How It Works
Using the AgentConfigManager.MgmtSvcCfg COM Object
The script uses the AgentConfigManager.MgmtSvcCfg COM object to interact with the Azure workspace. This object is responsible for managing the configuration of Microsoft Management Agent.
if ($PSCmdlet.ShouldProcess($computername, $workspaceId)) {
$mma = New-Object -ComObject 'AgentConfigManager.MgmtSvcCfg'
$mma.RemoveCloudWorkspace($workspaceId)
$mma.ReloadConfiguration()
}
Key Code Snippets
function Remove-MMAWorkspace {
[cmdletbinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $workspaceId,
# add parameters for computername and credentials:
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string[]]
$ComputerName,
[PSCredential]
$Credential
)
if ($PSCmdlet.ShouldProcess($computername, $workspaceId)) {
$mma = New-Object -ComObject 'AgentConfigManager.MgmtSvcCfg'
$mma.RemoveCloudWorkspace($workspaceId)
$mma.ReloadConfiguration()
}
Usage Examples
To use the script, you need to provide the WorkspaceId and ComputerName parameters. You can also specify credentials if needed. Here's an example:
Remove-MMAWorkspace -workspaceid "acbd1234-0000-1a2b-1234-abc1d2345678" -computername [yourserver]
Conclusion
The Remove-MMAWorkspace.ps1 script provides a powerful way to unlink servers from an Azure workspace using PowerShell. With its ability to remove cloud workspaces and reload the configuration, it's a useful tool for any IT professional working with Azure.