Archive for September, 2014

How can I flush an Agent Cache using PowerShell was the question of the day… ok, plenty of ways to do that, but here’s a basic pointer in the right direction. Remember, you’ll want to stop the healthservice first, and start it after this code has run…

# The name of the Health Service State folder where the Config is downloaded to/stored on an Agent.
$MMAgentHSTFolder = “Health Service State”

# Retrieve the path of the installation folder for the SCOM Agent from the registry.
$reg = Get-ItemProperty -Path “HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup\”
$MMAgentInstallDirectory = $reg.InstallDirectory

# Populate the full path to the folder.
$MMAgentHSTPath = $MMAgentInstallDirectory + $MMAgentHSTFolder

# Delete all of the files in the Health State Folder and any subdirectories.
Remove-Item -Path $MMAgentHSTPath\* -recurse

 

Health Service State Empty

 

 

 

 

 

 

 

 

 

 

 

 

 

Of course, if you prefer to do this all on one line you could easily do that too…have fun!

A colleague today asked me how can he copy a file to approx. 150 servers using PowerShell… Its a pretty simple thing to do, so here’s the code.

We used a list of servers from a file called servers.txt and then used the Copy-Item cmdlet to copy the file to the destination server. Very, very simple.

 

$servers = get-content “C:\servers.txt”
foreach ($server in $servers)
{
Copy-Item -Path ‘C:\filetocopy.txt’ -Destination “\\$server\e$\Folder\”
}

 

 

Even though I love using Windows PowerShell, sometimes you just can’t beat diving into the database even for quick and dirty tasks.

Today I was asked to get a list of all the Websites running on Windows 2008, so since I had a connection open to my Operational Database server, I decided to use that.

 

use OperationsManager
select [Path], [Description_7FEEF780_F4BD_AF93_ED3F_F026EEAAEAEA], [LogFileDirectory_0499BCD1_1959_14CC_AEB8_0A5CC9AC09D3], [Path_21E71E34_E24F_B3EC_B0EB_4670B79C9382] from dbo.MT_Microsoft$Windows$InternetInformationServices$2008$WebSite as IIS
Inner Join BaseManagedEntity as BME on IIS.BaseManagedEntityId = BME.BaseManagedEntityId

 

Now if you’re only interested in finding out what websites a specific server monitors, then just add on a simple Where Clause…

use OperationsManager
select [Path], [Description_7FEEF780_F4BD_AF93_ED3F_F026EEAAEAEA], [LogFileDirectory_0499BCD1_1959_14CC_AEB8_0A5CC9AC09D3], [Path_21E71E34_E24F_B3EC_B0EB_4670B79C9382] from dbo.MT_Microsoft$Windows$InternetInformationServices$2008$WebSite as IIS
Inner Join BaseManagedEntity as BME on IIS.BaseManagedEntityId = BME.BaseManagedEntityId
where [Path] = ‘SERVERNAME.YOURDOMAIN.COM’

So today I needed to find out how many times a piece of text was found inside a string. I was given a large page of text and needed to find out how many times a word or phrase appeared on that page.

# All of the text I’ve placed in a ‘here string’
$text= @”
blah, blah, blah, test, test, test, blah, blah, blah
“@

# And let’s look for the word “blah”
([regex]::Matches($text, “blah” )).count

The result will be “6”.

Read more

We were preparing our environment for an upgrade to SCOM 2012 R2 and a colleague asks me if there’s any easy way to grab a list of all servers that are sitting in Pending Management and then ping each one so he can determine whether to Approve or Reject them. Basically his idea was if they ping then they’re up and he’ll approve them. If they don’t ping, then he’ll reject them.

Sounds like sound logic :), so I decided to throw a little bit of PowerShell together for fun.

Read more

If you want to grab a list of all of your Agents, you can do that a few ways.

1. In the console from the “Administration View”.

2. Use the “Get-SCOMAgent” cmdlet.

3. A SQL Query against the OperationsManager Database.

use OperationsManager
SELECT * FROM ManagedEntityGenericView
INNER JOIN ManagedTypeView ON ManagedEntityGenericView.MonitoringClassId = ManagedTypeView.Id
WHERE (ManagedTypeView.Name = ‘microsoft.systemCenter.agent’)
order by ManagedEntityGenericView.DisplayName

 

If you want to grab a list of all of your Management Servers, you can do that a few ways.

1. In the console from the “Administration View”.

2. Use the “Get-SCOMManagementServer” cmdlet.

3. A SQL Query against the OperationsManager Database.

useOperationsManager
SELECT*FROMManagedEntityGenericView
INNERJOINManagedTypeViewONManagedEntityGenericView.MonitoringClassId=ManagedTypeView.Id
WHERE (ManagedTypeView.Name=’microsoft.systemCenter.managementserver’)
orderbyManagedEntityGenericView.DisplayName