Skip to content

Commit d56bc85

Browse files
authored
Split up into 2 sections
1 parent 76b5c82 commit d56bc85

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed
Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,65 @@
1-
$ManagementGroup = '<ManagementGroup>'; $ManagementServer = '<ManagementServer>'; [string]$OutFile = "c:\temp\Automate-ChangeSCOMAgent-MS.log"; Stop-Service HealthService -Force; "Removing management group '$ManagementGroup' on agent '$env:computername'" | Out-File -FilePath $OutFile -Append; try { $Agent = New-Object -ComObject AgentConfigManager.MgmtSvcCfg }catch { "Unable to get Agent configuration" | Out-File -FilePath $OutFile -Append; break }; try { $Agent.RemoveManagementGroup($ManagementGroup); $agent.ReloadConfiguration() } catch { "Unable to remove MG, error: " + $_.Exception.Message | Out-File -FilePath $OutFile -Append }; "Successfully removed management group '$ManagementGroup' on agent '$env:computername'" | Out-File -FilePath $OutFile -Append; $regpath1 = "HKLM:\SYSTEM\CurrentControlSet\Services\HealthService\Parameters\Management Groups\$ManagementGroup"; "Removing Registry Key: $regpath1" | Out-File -FilePath $OutFile -Append; Remove-Item "$regpath1" -Force -Recurse -ErrorAction SilentlyContinue; $regpath2 = "HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Agent Management Groups\$ManagementGroup"; "Removing Registry Key: $regpath2" | Out-File -FilePath $OutFile -Append; Remove-Item "$regpath2" -Force -Recurse -ErrorAction SilentlyContinue; $regpath3 = "$(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup" -ErrorAction Stop | Select-Object -Property "InstallDirectory" -ExpandProperty "InstallDirectory")\Health Service State"; "Clearing Health Service State folder: $regpath3" | Out-File -FilePath $OutFile -Append; Remove-Item "$regpath3" -Force -Recurse -ErrorAction SilentlyContinue; Start-Service HealthService; "Adding management group '$ManagementGroup' with server '$ManagementServer' on agent '$env:computername'" | Out-File -FilePath $OutFile -Append; try { $Agent = New-Object -ComObject AgentConfigManager.MgmtSvcCfg } catch { "Unable to get Agent configuration" | Out-File -FilePath $OutFile -Append; break }; try { $Agent.AddManagementGroup($ManagementGroup, $ManagementServer, "5723"); $agent.ReloadConfiguration() } catch { "Unable to add MG, error: " + $_.Exception.Message | Out-File -FilePath $OutFile -Append }; "Successfully added management group '$ManagementGroup' with server '$ManagementServer' on agent '$env:computername'" | Out-File -FilePath $OutFile -Append
1+
#region OneLiner
2+
$ManagementGroup = '<ManagementGroup>'; $ManagementServer = '<ManagementServer>'; [string]$OutFile = "c:\temp\Automate-ChangeSCOMAgent-MS.log"; Stop-Service HealthService -Force; "Removing management group '$ManagementGroup' on agent '$env:computername'" | Out-File -FilePath $OutFile -Append; try { $Agent = New-Object -ComObject AgentConfigManager.MgmtSvcCfg }catch { "Unable to get Agent configuration" | Out-File -FilePath $OutFile -Append; break }; try { $Agent.RemoveManagementGroup($ManagementGroup); $agent.ReloadConfiguration() }catch { "Unable to remove MG, error: " + $_.Exception.Message | Out-File -FilePath $OutFile -Append }; "Successfully removed management group '$ManagementGroup' on agent '$env:computername'" | Out-File -FilePath $OutFile -Append; $regpath1 = "HKLM:\SYSTEM\CurrentControlSet\Services\HealthService\Parameters\Management Groups\$ManagementGroup"; "Removing Registry Key: $regpath1" | Out-File -FilePath $OutFile -Append; Remove-Item "$regpath1" -Force -Recurse -ErrorAction SilentlyContinue; $regpath2 = "HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Agent Management Groups\$ManagementGroup"; "Removing Registry Key: $regpath2" | Out-File -FilePath $OutFile -Append; Remove-Item "$regpath2" -Force -Recurse -ErrorAction SilentlyContinue; $regpath3 = "$(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup" -ErrorAction Stop | Select-Object -Property "InstallDirectory" -ExpandProperty "InstallDirectory")\Health Service State"; "Clearing Health Service State folder: $regpath3" | Out-File -FilePath $OutFile -Append; Remove-Item "$regpath3" -Force -Recurse -ErrorAction SilentlyContinue; Start-Service HealthService; "Adding management group '$ManagementGroup' with server '$ManagementServer' on agent '$env:computername'" | Out-File -FilePath $OutFile -Append; try { $Agent = New-Object -ComObject AgentConfigManager.MgmtSvcCfg }catch { "Unable to get Agent configuration" | Out-File -FilePath $OutFile -Append; break }; try { $Agent.AddManagementGroup($ManagementGroup, $ManagementServer, "5723"); $agent.ReloadConfiguration() }catch { "Unable to add MG, error: " + $_.Exception.Message | Out-File -FilePath $OutFile -Append }; "Successfully added management group '$ManagementGroup' with server '$ManagementServer' on agent '$env:computername'" | Out-File -FilePath $OutFile -Append
3+
#endregion Oneliner
4+
5+
# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
6+
7+
#region Multiline
8+
$ManagementGroup = '<ManagementGroup>'
9+
$ManagementServer = '<ManagementServer>'
10+
11+
[string]$OutFile = "c:\temp\Automate-ChangeSCOMAgent-MS.log"
12+
13+
Stop-Service HealthService -Force
14+
"Removing management group '$ManagementGroup' on agent '$env:computername'" | Out-File -FilePath $OutFile -Append
15+
try
16+
{
17+
$Agent = New-Object -ComObject AgentConfigManager.MgmtSvcCfg
18+
}
19+
catch
20+
{
21+
"Unable to get Agent configuration" | Out-File -FilePath $OutFile -Append
22+
break
23+
}
24+
try
25+
{
26+
$Agent.RemoveManagementGroup($ManagementGroup)
27+
$agent.ReloadConfiguration()
28+
}
29+
catch
30+
{
31+
"Unable to remove MG, error: " + $_.Exception.Message | Out-File -FilePath $OutFile -Append
32+
}
33+
"Successfully removed management group '$ManagementGroup' on agent '$env:computername'" | Out-File -FilePath $OutFile -Append
34+
$regpath1 = "HKLM:\SYSTEM\CurrentControlSet\Services\HealthService\Parameters\Management Groups\$ManagementGroup"
35+
"Removing Registry Key: $regpath1" | Out-File -FilePath $OutFile -Append
36+
Remove-Item "$regpath1" -Force -Recurse -ErrorAction SilentlyContinue
37+
$regpath2 = "HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Agent Management Groups\$ManagementGroup"
38+
"Removing Registry Key: $regpath2" | Out-File -FilePath $OutFile -Append
39+
Remove-Item "$regpath2" -Force -Recurse -ErrorAction SilentlyContinue
40+
$regpath3 = "$(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup" -ErrorAction Stop | Select-Object -Property "InstallDirectory" -ExpandProperty "InstallDirectory")\Health Service State"
41+
"Clearing Health Service State folder: $regpath3" | Out-File -FilePath $OutFile -Append
42+
Remove-Item "$regpath3" -Force -Recurse -ErrorAction SilentlyContinue
43+
Start-Service HealthService
44+
"Adding management group '$ManagementGroup' with server '$ManagementServer' on agent '$env:computername'" | Out-File -FilePath $OutFile -Append
45+
try
46+
{
47+
$Agent = New-Object -ComObject AgentConfigManager.MgmtSvcCfg
48+
}
49+
catch
50+
{
51+
"Unable to get Agent configuration" | Out-File -FilePath $OutFile -Append
52+
break
53+
}
54+
try
55+
{
56+
$Agent.AddManagementGroup($ManagementGroup, $ManagementServer, "5723")
57+
$agent.ReloadConfiguration()
58+
}
59+
catch
60+
{
61+
"Unable to add MG, error: " + $_.Exception.Message | Out-File -FilePath $OutFile -Append
62+
}
63+
64+
"Successfully added management group '$ManagementGroup' with server '$ManagementServer' on agent '$env:computername'" | Out-File -FilePath $OutFile -Append
65+
#endregion Multiline

0 commit comments

Comments
 (0)