PowerShell one-liner for Christmas…and a challenge for the new year
How about a PowerShell 1-liner to determine if a particular system is a virtual machine?
>get-monitoringclass|where{$_.displayname -eq “Windows Server”}| `
get-monitoringobject|where{$_.displayname -eq “Oxford.contoso.com”}| `
get-monitoringobjectproperty|where{$_.displayname -eq “Virtual Machine”}|foreach{$_.value}
True PowerShell can be pretty intimidating, especially when you might come across a long command like the one above. Well, PowerShell is here to stay, and that has been confirmed by its inclusion in OpsMgr 2007 R2.
So, here’s a challenge for 2009 for you… Come up with the best challenge by asking me to write a script to automate something for you…
For the best challenge (received before March 1st, 2009), I’ll give you at least $100US worth of Microsoft related products (not for resale and legit). I may even consider sending $50US worth of products to 2nd place and even 3rd…
Go ahead… Do you think you can challenge me?
(Pete Zerger has already asked for something in PowerShell, which I’ve struggled with for a few weeks so far. I believe it is a difference with how C# and PowerShell handle generic classes, but I’m still trying…)
Have a challenge for me? Leave your challenge as a comment on this post.
December 19th, 2008 at 5:55 pm
Marco,
I got one for you. I recently noticed earlier this week while looking at an OPS Mgr trace. When you create a new service monitor using the Server Monitor Management Pack Template the template sets the Discovery frequency to 1 minute. We have over 250 services that we monitor. Discovering 250 every minute on 600+ servers creates a lot of unneeded load. Can you create a Powershell script to change the discovery frequency of all of these services? The services are store in different management packs based upon application name. I would prefer to just change the frequency directly being that they are unsealed, rather than create overrides.
Here is some sample C# code that might help because there is not a function to change discovery frequency.
private string RetrieveDiscoveryFrequency(ManagementPackDiscovery managementPackDiscovery)
{
XmlDocument document = new XmlDocument();
string innerText = string.Empty;
string xml = string.Format(”{0}”, managementPackDiscovery.DataSource.Configuration);
document.LoadXml(xml);
XmlNode node = document.SelectSingleNode(”//config/Frequency”);
if (node == null)
{
node = document.SelectSingleNode(”//config/IntervalSeconds”);
}
if (node != null)
{
innerText = node.InnerText;
}
return innerText;
}
December 19th, 2008 at 8:00 pm
Ooh Marco, looks like Tim’s bringin’ his A game tonight. It’s on!
January 8th, 2009 at 4:36 pm
Marco destroyed my challenge without breaking a sweat.
Thanks for the help Marco.
January 9th, 2009 at 11:08 am
The oneliner for the VirtualMachine will only work if the VM is running MSVPC, correct?
Previously, I’ve been creating a discovery that looks for a RegKey that should always be present if the the guest is running in VMware and then grouping upon that VMware Virtual Machine Key. From there, one just lists the group membership.
Has the system changed up the way it determines if an agent managed object is VM?
January 12th, 2009 at 11:47 pm
Here’s the answer to Tim’s challenge
http://www.systemcenterforum.org/news/powershell-challenge-2009%e2%80%94challenge-1-modifying-discovery-frequency-with-powershell/
January 20th, 2009 at 8:28 am
Hi Peter,
can you provide me a script to test cluster failover??
Regards,
varun
January 20th, 2009 at 6:55 pm
A Windows 2008 cluster?
There is an example at http://blogs.msdn.com/clustering/archive/2008/12/20/9243367.aspx
January 20th, 2009 at 8:26 pm
@Varun
Can you be a bit more specific? That link Pete provided is for Windows Server 2008 R2 (currently beta).
You can interact with the older clusters via PowerShell.
What would you like to see? Some PowerShell script to failover, try to access the clustered resources, then write to the Windows Event log?
A special entry could be written and have OpsMgr pick up that event log entry to determine if the event was a success or failure.
January 28th, 2009 at 1:31 pm
Powershell Master (Marco),
I have another one for you. One thing that has always been a challenge is when we work with a new application team. We usually need to deploy out a few custom state based script monitors. We tell the application team not to close these alerts or really any alerts, as closing them will not change the state of the monitor unless it is really fixed (this even puzzles me why it works like this).
So we created a new state called acknowledged. We have the application team put it in the acknowledged state so that the alert will not escalate. After the problem is fixed the alert closes automatically and everything is good. The big problem is when someone forgets and closes the alert. This creates a very bad scenario where an alert is in a bad state and will stay in a bad state forever but no one will ever notice unless they go into health explorer. Even the person who closed it thinks its fixed.
Is it possible to create a powershell script that looks for monitors in an error state but the alert associated with it has been closed? If its closed reset the state back to good so that it will refire an alert.
February 3rd, 2009 at 10:40 am
It looks like Marius Sutara just created a tool that does exactly what I was looking to acomplish with the powershell script. When a monitor is closed it automatically forces a reset state on the the monitor.
http://blogs.msdn.com/mariussutara/archive/2009/02/02/how-to-reset-monitor-when-closing-alert.aspx