Clear Gray Agents
#Variable Defination
Write-host "Defining Variables"
#sleep 3
$date=get-date -Format M
$string=$date
$service= "Health Service"
$getservice= "HealthService"
#$running = get-service -displayname $service
#$status = $running.status
#sleep 3
Write-host "Variables Defined"
#sleep 3
$GetManagementServerName = Get-SCOMManagementGroup | Select Name | ft -hide
#Write-host "$service"
#Write-host "$running"
#Write-host "$status"
Function GetGrayAgents
{
#sleep 3
Write-host "Checking for gray agents in Management Group:"
#sleep 3
$GetManagementServerName
#sleep 3
$agent = Get-SCClass -displayname "Health Service"
#sleep 3
$objects = Get-SCOMMonitoringObject -class:$agent| where {$_.IsAvailable -eq $false} | Select displayname| sort-object Displayname | Export-csv -path C:\utilities\clearcache\server.csv -force -notypeinformation
#sleep 3
Write-host "Checking to see if there are any gray agents.."
$objects
if ((Get-Content "C:\utilities\clearcache\server.csv") -eq $Null)
{
Write-host "All agents are Green" -foreground Green
Write-host "Sending mail"
emptysendmail
#sleep 3
Write-host "Mail Sent"
#sleep 3
Write-host "Exiting from code"
Break
}
Else
{
Write-host "Gray agent found" -foreground Red
Write-host "Here are the Gray Agents: "
#sleep 3
CreateCSV
Write-host "Sending mail on Gray Agents: "
#sleep 3
Sendmail
}
}
Function CreateCSV
{
$csv= get-content C:\utilities\clearcache\server.csv
$csv = $csv[1..($csv.count - 1)]
$csv > C:\utilities\clearcache\serverNoHeader.csv
#sleep 3
$csv= Get-Content C:\utilities\clearcache\serverNoHeader.csv | % {$_ -replace '"', ""} | out-file -FilePath C:\utilities\clearcache\serverNoHeaderNoQuotes.csv -Force -Encoding ascii
$csv
#sleep 3
#sleep 3
$info = get-content "C:\Utilities\ClearCache\serverNoHeaderNoQuotes.csv"
$info
}
Function Sendmail
{
$objects = Get-SCOMMonitoringObject -class:$agent| where {$_.IsAvailable -eq $false} | Select displayname|sort-object Displayname |ConvertTo-HTML | Out-File C:\utilities\clearcache\healthStatus.html
Send-MailMessage -from praveeniyer7@live.com -To praveeniyer7@live.com -cc -subject "Gray Agents Found" -bodyasHTML (Get-Content C:\utilities\clearcache\healthStatus.html | out-string) -smtpServer smtpserver.com
Write-host "Mail sent"
}
Function checkping
{
$strFileName = "c:\utilities\clearcache\successping.txt"
If (Test-Path $strFileName)
{
Remove-Item $strFileName
}
Write-host "Checking if the Gray agents can be pinged"
#sleep 3
$info = get-content "C:\Utilities\ClearCache\serverNoHeaderNoQuotes.csv"
foreach ($Server in $info)
{
if (test-Connection -ComputerName $Server -Count 2 -Quiet )
{
write-Host "$Server is alive and Pinging " -ForegroundColor Green
Write-host "Ping is Successful"
Add-Content c:\utilities\clearcache\successping.txt $server
}
else
{
Write-Warning "$Server seems dead not pinging"
Send-MailMessage -from praveeniyer7@live.com -To praveeniyer7@live.com -cc -subject "URGENT $Server Not Pingable" -bodyasHTML -smtpServer smtpserver.com
}
}
}
Function CheckServiceAvailability
{
$servicename = "HealthService"
$strFileName = "c:\utilities\clearcache\ServiceFound.txt"
$strFileName1 = "c:\utilities\clearcache\ServiceNotFound.txt"
Write-host "Checking if Health Service is available in each of the servers that were successfully pinged"
$info = get-content "c:\utilities\clearcache\successping.txt"
$info
If (Test-Path $strFileName)
{
Remove-Item $strFileName
}
Else
{
write-host "File does not exist. Creating file"
}
If (Test-Path $strFileName1)
{
Remove-Item $strFileName1
}
Else
{
write-host "File does not exist. Creating file"
}
foreach($info in $info)
{
if (Get-Service $servicename -Computername $info -ErrorAction SilentlyContinue)
{
Write-host "True"
Add-Content c:\utilities\clearcache\ServiceFound.txt $info
$date=get-date -Format M
$string=$date
Sleep 3
Write-host "Starting to clear cache..."
$info = get-content C:\Utilities\ClearCache\ServiceFound.txt
$info
Sleep 3
Write-host "Starting For Loop..."
Sleep 3
$info
Write-host "Entering For loop"
$check = Get-service -name "HealthService" -Computername $info
$status= $check.status
$status
Write-host "Status Printed $status" -foreground cyan
if( $status -eq "Running")
{
Stop-service -inputobject $(get-service -name "HealthService" -ComputerName $info)
Write-host "Stopping service $info..." -foreground Yellow
Sleep 3
Write-host "Service Stopped $info" -foreground red
}
Sleep 3
Write-host "Renaming Folder"
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $info)
$regKey= $reg.OpenSubKey("SOFTWARE\\Microsoft\\Microsoft Operations Manager\\3.0\\Setup")
$fn=$regkey.GetValue("InstallDirectory")
$fnr= $fn -replace ":" , "$"
$it = "\\"+$info+"\"+$fnr+"Health Service State"
Rename-item $it "$it+$string"
Write-host "Folder Renamed"
Write-host "Starting service $info..." -foreground Yellow
Start-service -inputobject $(get-service -Name "HealthService" -ComputerName $info)
Write-host "Service Started $info" -foreground Green
Write-host "-------------------------------------------------------------------------------------------------------------------------------" -foreground Green
Return
}
Else
{
Write-host "False"
Add-Content c:\utilities\clearcache\servicenotfound.txt $info
Write-host "-------------------------------------------------------------------------------------------"
Write-Host "Waiting...."
$info = get-content "C:\Utilities\ClearCache\servicenotfound.txt"
$info
foreach($server in $info)
{
Write-host "Repairing $info"
Get-SCOMAgent -Name $info | Repair-SCOMAgent -PassThru
Write-host "Check $info in Pending Management"
}
}
}
}
Function emptySendmail
{
$objects = Get-SCOMMonitoringObject -class:$agent| where {$_.IsAvailable -eq $false} | Select displayname|ConvertTo-HTML | Out-File C:\utilities\clearcache\healthStatus.html
Send-MailMessage -from praveeniyer7@live.com -To praveeniyer7@live.com -cc -subject "All Agents Green" -bodyasHTML (Get-Content C:\utilities\clearcache\healthStatus.html | out-string) -smtpServer smtpserver.com
}
GetGrayAgents
Checkping
CheckServiceAvailability
SCOM Agent Version from Servers
$server = get-content "E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\allagents.txt"
$error1 = "System Not Upgraded"
$error2 = "Access Denied"
Add-Content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AgentVersions.csv "ServerName,Version"
Add-Content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AgentError.csv "ServerName,Status"
Add-content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AccessDenied.csv "ServerName,Error"
foreach ($Server in $server)
{
#$service = Get-WmiObject Win32_Service | Where-Object name -eq "HealthService" | select displayname | ft -hide -auto
#$service = $service | Out-String -stream | Select-Object -Skip 1
$server
$test= Get-WmiObject -ComputerName $server -Class win32_product | where {$_.name -eq "Microsoft Monitoring Agent"} | select version | ft -hide -auto
$test = $test | Out-String -stream | Select-Object -Skip 1
#$test
#$error
if($test)
{
Add-Content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AgentVersions.csv "$server,$test"
}
elseif($error -like "*enied*")
{
add-content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AccessDenied.csv "$server,$error2"
}
elseif($error -like "*RPC*")
{
add-content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AccessDenied.csv "$server,$error2"
}
else
{
Add-Content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AgentError.csv "$server,$error1"
}
}
#Variable Defination
Write-host "Defining Variables"
#sleep 3
$date=get-date -Format M
$string=$date
$service= "Health Service"
$getservice= "HealthService"
#$running = get-service -displayname $service
#$status = $running.status
#sleep 3
Write-host "Variables Defined"
#sleep 3
$GetManagementServerName = Get-SCOMManagementGroup | Select Name | ft -hide
#Write-host "$service"
#Write-host "$running"
#Write-host "$status"
Function GetGrayAgents
{
#sleep 3
Write-host "Checking for gray agents in Management Group:"
#sleep 3
$GetManagementServerName
#sleep 3
$agent = Get-SCClass -displayname "Health Service"
#sleep 3
$objects = Get-SCOMMonitoringObject -class:$agent| where {$_.IsAvailable -eq $false} | Select displayname| sort-object Displayname | Export-csv -path C:\utilities\clearcache\server.csv -force -notypeinformation
#sleep 3
Write-host "Checking to see if there are any gray agents.."
$objects
if ((Get-Content "C:\utilities\clearcache\server.csv") -eq $Null)
{
Write-host "All agents are Green" -foreground Green
Write-host "Sending mail"
emptysendmail
#sleep 3
Write-host "Mail Sent"
#sleep 3
Write-host "Exiting from code"
Break
}
Else
{
Write-host "Gray agent found" -foreground Red
Write-host "Here are the Gray Agents: "
#sleep 3
CreateCSV
Write-host "Sending mail on Gray Agents: "
#sleep 3
Sendmail
}
}
Function CreateCSV
{
$csv= get-content C:\utilities\clearcache\server.csv
$csv = $csv[1..($csv.count - 1)]
$csv > C:\utilities\clearcache\serverNoHeader.csv
#sleep 3
$csv= Get-Content C:\utilities\clearcache\serverNoHeader.csv | % {$_ -replace '"', ""} | out-file -FilePath C:\utilities\clearcache\serverNoHeaderNoQuotes.csv -Force -Encoding ascii
$csv
#sleep 3
#sleep 3
$info = get-content "C:\Utilities\ClearCache\serverNoHeaderNoQuotes.csv"
$info
}
Function Sendmail
{
$objects = Get-SCOMMonitoringObject -class:$agent| where {$_.IsAvailable -eq $false} | Select displayname|sort-object Displayname |ConvertTo-HTML | Out-File C:\utilities\clearcache\healthStatus.html
Send-MailMessage -from praveeniyer7@live.com -To praveeniyer7@live.com -cc -subject "Gray Agents Found" -bodyasHTML (Get-Content C:\utilities\clearcache\healthStatus.html | out-string) -smtpServer smtpserver.com
Write-host "Mail sent"
}
Function checkping
{
$strFileName = "c:\utilities\clearcache\successping.txt"
If (Test-Path $strFileName)
{
Remove-Item $strFileName
}
Write-host "Checking if the Gray agents can be pinged"
#sleep 3
$info = get-content "C:\Utilities\ClearCache\serverNoHeaderNoQuotes.csv"
foreach ($Server in $info)
{
if (test-Connection -ComputerName $Server -Count 2 -Quiet )
{
write-Host "$Server is alive and Pinging " -ForegroundColor Green
Write-host "Ping is Successful"
Add-Content c:\utilities\clearcache\successping.txt $server
}
else
{
Write-Warning "$Server seems dead not pinging"
Send-MailMessage -from praveeniyer7@live.com -To praveeniyer7@live.com -cc -subject "URGENT $Server Not Pingable" -bodyasHTML -smtpServer smtpserver.com
}
}
}
Function CheckServiceAvailability
{
$servicename = "HealthService"
$strFileName = "c:\utilities\clearcache\ServiceFound.txt"
$strFileName1 = "c:\utilities\clearcache\ServiceNotFound.txt"
Write-host "Checking if Health Service is available in each of the servers that were successfully pinged"
$info = get-content "c:\utilities\clearcache\successping.txt"
$info
If (Test-Path $strFileName)
{
Remove-Item $strFileName
}
Else
{
write-host "File does not exist. Creating file"
}
If (Test-Path $strFileName1)
{
Remove-Item $strFileName1
}
Else
{
write-host "File does not exist. Creating file"
}
foreach($info in $info)
{
if (Get-Service $servicename -Computername $info -ErrorAction SilentlyContinue)
{
Write-host "True"
Add-Content c:\utilities\clearcache\ServiceFound.txt $info
$date=get-date -Format M
$string=$date
Sleep 3
Write-host "Starting to clear cache..."
$info = get-content C:\Utilities\ClearCache\ServiceFound.txt
$info
Sleep 3
Write-host "Starting For Loop..."
Sleep 3
$info
Write-host "Entering For loop"
$check = Get-service -name "HealthService" -Computername $info
$status= $check.status
$status
Write-host "Status Printed $status" -foreground cyan
if( $status -eq "Running")
{
Stop-service -inputobject $(get-service -name "HealthService" -ComputerName $info)
Write-host "Stopping service $info..." -foreground Yellow
Sleep 3
Write-host "Service Stopped $info" -foreground red
}
Sleep 3
Write-host "Renaming Folder"
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $info)
$regKey= $reg.OpenSubKey("SOFTWARE\\Microsoft\\Microsoft Operations Manager\\3.0\\Setup")
$fn=$regkey.GetValue("InstallDirectory")
$fnr= $fn -replace ":" , "$"
$it = "\\"+$info+"\"+$fnr+"Health Service State"
Rename-item $it "$it+$string"
Write-host "Folder Renamed"
Write-host "Starting service $info..." -foreground Yellow
Start-service -inputobject $(get-service -Name "HealthService" -ComputerName $info)
Write-host "Service Started $info" -foreground Green
Write-host "-------------------------------------------------------------------------------------------------------------------------------" -foreground Green
Return
}
Else
{
Write-host "False"
Add-Content c:\utilities\clearcache\servicenotfound.txt $info
Write-host "-------------------------------------------------------------------------------------------"
Write-Host "Waiting...."
$info = get-content "C:\Utilities\ClearCache\servicenotfound.txt"
$info
foreach($server in $info)
{
Write-host "Repairing $info"
Get-SCOMAgent -Name $info | Repair-SCOMAgent -PassThru
Write-host "Check $info in Pending Management"
}
}
}
}
Function emptySendmail
{
$objects = Get-SCOMMonitoringObject -class:$agent| where {$_.IsAvailable -eq $false} | Select displayname|ConvertTo-HTML | Out-File C:\utilities\clearcache\healthStatus.html
Send-MailMessage -from praveeniyer7@live.com -To praveeniyer7@live.com -cc -subject "All Agents Green" -bodyasHTML (Get-Content C:\utilities\clearcache\healthStatus.html | out-string) -smtpServer smtpserver.com
}
GetGrayAgents
Checkping
CheckServiceAvailability
SCOM Agent Version from Servers
$server = get-content "E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\allagents.txt"
$error1 = "System Not Upgraded"
$error2 = "Access Denied"
Add-Content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AgentVersions.csv "ServerName,Version"
Add-Content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AgentError.csv "ServerName,Status"
Add-content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AccessDenied.csv "ServerName,Error"
foreach ($Server in $server)
{
#$service = Get-WmiObject Win32_Service | Where-Object name -eq "HealthService" | select displayname | ft -hide -auto
#$service = $service | Out-String -stream | Select-Object -Skip 1
$server
$test= Get-WmiObject -ComputerName $server -Class win32_product | where {$_.name -eq "Microsoft Monitoring Agent"} | select version | ft -hide -auto
$test = $test | Out-String -stream | Select-Object -Skip 1
#$test
#$error
if($test)
{
Add-Content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AgentVersions.csv "$server,$test"
}
elseif($error -like "*enied*")
{
add-content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AccessDenied.csv "$server,$error2"
}
elseif($error -like "*RPC*")
{
add-content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AccessDenied.csv "$server,$error2"
}
else
{
Add-Content E:\Microsoft\SCOM\Utilities\SCOMAgentInternalVersion\AgentError.csv "$server,$error1"
}
}
No comments:
Post a Comment