Browsed by
Month: December 2015

Remove List of SCOM Orphaned Agents in PowerShell

Remove List of SCOM Orphaned Agents in PowerShell

I faced this need for a customer need, so I wanted to share with you this little article. In order to remove a list of SCOM orphaned agents (e.g. computer doesn’t exist anymore) you can use this script. Obviously, it will only remove agents from SCOM console, but it will not uninstall agent from the computer (we will see how to manage this in another article).

You can actually use either a plain list of gray agents or .csv file as input of the array.

Access SQL Server (Integrated Security) with Different Windows Credentials in PowerShell

Access SQL Server (Integrated Security) with Different Windows Credentials in PowerShell

I needed to get data out of a SQL Server instance that used integrated windows security through a PowerShell script. But the user that has access to the instance is a domain user account different to the user logged on when running the script. So I developed a mechanism to invoke my SQL commands as a script block of a job because a job can be started with different credentials.

You can find below my PowerShell script that connects to a SQL database using different Windows credentials, runs a query and format the result as a table. This is provided as an example to illustrate this mechanism. You can especially use this mechanism to replace SQL integration pack activities of Orchestrator.

Dates and Time Zones in PowerShell

Dates and Time Zones in PowerShell

If you have already played with dates in PowerShell, you’ve probably realized that it was not so simple. If you are a beginner, I recommend you to read this article to get some basics about dates and times in PowerShell. On this article, we will focus on time zones, time offsets, and daylight-saving times. Actually, you can retrieve upcoming daylight saving time clock changes here.

In this case, my computer time is based on UTC +01:00 (Central European Standard Time) timezone. Let’s make a test.

You can see that if I add one month to the actual date using native PowerShell commands, it adds one month like expected but time remains the same. Why should time change? Because in my timezone, the upcoming daylight saving time change will happen on March the 27th. In fact, the time should have changed to Friday, April 15, 2016, 6:05:52 PM.

So next question is, how can I handle this?

Actually, the simplest way I found, is to use UTC time conversion. Let me explain, if you take for example this date “Tuesday, March 15, 2016, 5:05:52 PM” based on UTC +01:00 Central European Standard Time time zone. You will need first to convert it to Universal Time, add what you need (minutes, hours, days, months, etc.) and then reconvert it again to your time zone to get expected result. To accomplish this I wrote these two functions.

You can see that the time changed as expected by adding one day and one hour to the next date.

Restart Windows Computer Remotely Through a SCOM Agent Task

Restart Windows Computer Remotely Through a SCOM Agent Task

One of the needs of my customer was to be able to reboot any Windows computer (with SCOM agent) remotely without having local administrator rights. I figured out that the best way to accomplish this would be to create a SCOM agent task (not a console task), that you can call from anywhere (script, runbook, etc.). The main advantage is that tasks run under the credential of the Default Action Account on the agent computer. This account typically has sufficient privileges for accessing most application components and running actions, even if the user running the tanks does not have these user rights.

To create this agent task you can use your favorite tools, in my case I will create this task in a new Management Pack fragment on my visual studio solution. You can find below the XML source code for this agent task.

Once you have imported your management pack containing this agent task, you can launch the task from one of your MS for example, through these PowerShell lines.

You will have an associate output in your Powershell windows with the status of the task. You can also retrieve the information directly in the Task Status view on your SCOM console.