Saturday, September 17, 2016

Monitoring Windows Services using PowerShell

Recently I was interested in finding a way to monitor a windows service. What I needed was to check whether the service is running and if not running get a notification and try to restart the service. Following PowerShell script does exactly that, it checks for Microsoft CRM Asynchronous Service and the Microsoft CRM Asynchronous Maintenance Service activity and send 2 emails to Admin and Dev. This needs to be then scheduled using Windows Task Scheduler or SQL Server Job.

### Checking for CRM Async and Maintenance service failure and try restarting, if failing send an email notification.

 

## Function to send mail notification.

function Send_Email ([string]$strEmailSubject, [string]$strEmailBody)

{

       $EmailFrom = "Arjuna@Email.com"

       $EmailTo = "Admin@Email.com, Dev@Email.com"

       $EmailSubject = $strEmailSubject

       $EmailBody = $strEmailBody

       $EmailSMTPServer = "SMTP.server.com"

       ## Creating Mail Message object.

       $SMTPMessage = New-Object System.Net.Mail.MailMessage $EmailFrom, $EmailTo, $EmailSubject, $EmailBody

       ## Enabling HTML mail body.

       $SMTPMessage.IsBodyHtml = $true

       ## Creating SMTP client object.

       $SMTPClient = New-Object System.Net.Mail.SMTPClient $EmailSMTPServer

       ## Sending mail.

       $SMTPClient.Send($SMTPMessage)

       ## Sending mail method 2.

       ##send-mailmessage -from "Arjuna@Email.com" -to "Admin@Email.com, Dev@Email.com" -subject "CRM Async Service Failed" -body "Please check." -smtpserver "SMTP.server.com"

       ## Sending mail method 2 using parameters.

       ##send-mailmessage -from $EmailFrom -to $EmailTo -subject $EmailSubject -body $EmailBody -smtpserver $EmailSMTPServer

}

 

## Function to check the service activity.

function Check_Service

{

       ## Get all services which has a Name like MSCRMAsyncService, Start Mode is Auto and service State is Running.

       $FailedAsyncService = Get-WmiObject Win32_Service | Where-Object {$_.Name -like 'MSCRMAsyncService' -and $_.StartMode -eq 'Auto' -and $_.State -ne 'Running'} | Select-Object DisplayName

       ## For Testing.

       ##Write-Host "A: " $FailedAsyncService

 

       ## Get all services which has a Name like MSCRMAsyncService$maintenance, Start Mode is Auto and service State is Running.

       $FailedAsyncMainteService = Get-WmiObject Win32_Service | Where-Object {$_.Name -like 'MSCRMAsyncService$maintenance' -and $_.StartMode -eq 'Auto' -and $_.State -ne 'Running'} | Select-Object DisplayName

       ## For Testing.

       ##Write-Host "B: " $FailedAsyncMainteService

 

       ## Checking whether the Async Service has failed.

       if ($FailedAsyncService -ne $NULL)

       {

              ## Trying to start the failed Async Service.

              Start-Service -displayname "Microsoft Dynamics CRM Asynchronous Processing Service"

              ## Service Name can also be used to start the servie.

              ##Start-Service MSCRMAsyncService

              ## Get all services which has a Name like MSCRMAsyncService, Start Mode is Auto and service State is Running.

              $AsyncServiceStarted = Get-WmiObject Win32_Service | Where-Object {$_.Name -like 'MSCRMAsyncService' -and $_.StartMode -eq 'Auto' -and $_.State -ne 'Running'} | Select-Object Name

              ## Checking the service to see whether it started.

              if ($AsyncServiceStarted -ne $NULL)

              {

                     ## Calling Send_Mail function to notify.

                     Send_Email ("CRM Async Service Failed.") ("System has detected that the following CRM Async Service has failed. System automatically tried restarting the service but it was unsuccessful. Try manual start. <BR/><BR/>" + $FailedAsyncService)

              }

              else

              {

                     ## Calling Send_Mail function to notify.

                     Send_Email ("CRM Async Service Restarted.") ("System has detected that the following CRM Async Service has failed. System automatically tried restarting the service and it was successful. <BR/><BR/>" + $FailedAsyncService)

              }

       }

 

       ## Checking whether the Async Maintenance Service has failed.

       if ($FailedAsyncMainteService -ne $NULL)

       {

              ## Trying to start the failed Async Maintenance Service.

              Start-Service -displayname "Microsoft Dynamics CRM Asynchronous Processing Service (maintenance)"

              ## Get all services which has a Name like MSCRMAsyncService$maintenance, Start Mode is Auto and service State is Running.

              $AsyncMainteServiceStarted = Get-WmiObject Win32_Service | Where-Object {$_.Name -like 'MSCRMAsyncService$maintenance' -and $_.StartMode -eq 'Auto' -and $_.State -ne 'Running'} | Select-Object Name

              ## Checking the service to see whether it started.

              if ($AsyncMainteServiceStarted -ne $NULL)

              {

                     ## Calling Send_Mail function to notify.

                     Send_Email ("CRM Async Service Failed.") ("System has detected that the following CRM Async Service has failed. System automatically tried restarting the service but it was unsuccessful. Try manual start. <BR/><BR/>" + $FailedAsyncMainteService)

              }

              else

              {

                     ## Calling Send_Mail function to notify.

                     Send_Email ("CRM Async Service Restarted.") ("System has detected that the following CRM Async Service has failed. System automatically tried restarting the service and it was successful. <BR/><BR/>" + $FailedAsyncMainteService)

              }

       }

}

 

## Calling the Check_Service function.

Check_Service

Thursday, August 04, 2016

Replacing Vanity and Cargo Lights – Toyota RAV4 2011

It seems to be easy to replace the internal lights of your car if you would like to do it your self. Recently I did change my Halogen lights to LED. Following steps are for a 2011 Toyota RAV4, but some of the steps may be applicable to other vehicle models as well.

Vanity Lights

You can remove the vanity light by inserting a flat head screw driver or a panel remover tool from the left side of the light. If you are using a screw driver, you can use a cloth to minimise the damage caused. Be careful not to try from the right side, since the wire connections are there on the right side. So if you try forcing it out from the right side, it will break. You will get the idea from the below images.

Vanity 1Vanity 2

Cargo Area Light

Cargo light is the light on the back door of the RAV4, this can be easily removed by inserting a tool from the side where the switch is located. Do not try from the other side since it will break.

Once the light assembly is removed you need to remove the plastic cover to access the light. If you are replacing the globe with a LED, remember to turn the LED to the other side, so the light will emit to the cargo area. You will be able to get a better understanding by the following pictures.

Cargo (1)Cargo (7)Cargo (8)Cargo (9)

Cargo (10)Cargo (11)

Replacing Dome Lights – Toyota RAV4 2011

Recently I did upgrade the interior lights of my car, since there are not much articles with the steps,  thought this will be helpful to the other readers.

1. Use a car trim remover tool or a flat head screwdriver to gently remove the light cover. If using a screwdriver, it is better use a cloth to reduce the damage to the plastic components.

You just need to insert the tool in the below marked areas to get it undone. Note that we re only removing the lens cover, not the entire dome light assembly. So insert the tool between the lens cover and the plastic.

image

image

2. You will be able to pull the existing bulb without much effort.

image

After replacing the globe, remember to check before fitting the cover.

image

Replacing Map Lights – Toyota RAV4 2011

Recently I was interested in replacing the Halogen map light bulbs with LEDs, I thought this might be useful for other readers as well if I let you know the steps to do it. Even though I did it on a Toyota RAV4 2011 model, some of the steps may be helpful to do the same on other vehicles. But be careful since if you do it wrong things might break.

It is always better to use a trim panel remover tool to reduce the damage caused by just using a flat head screw driver. If you have to use a screwdriver, use a cloth to minimise the damage.

image

1. Using a panel remover, flip the top and bottom of the lens covers to remove the lens covers.

Dome 1

Below images will show you the mechanism holding the lens cover, so you just need to insert the tool at the right location and gently tilt the tool to get it removed. For example if you insert the tool from the right or left sides and use force, then the small plastic piece will break, needing you to buy a new one.

imageimage

2. Once the covers are removed you will see the light globes, here you need to be careful. To remove the globes you just need to pull the globe out from the holder. If you turn the globes then the holder will get detached. If you are not too careful the released holder will fall in.

In case you rotate the holder, very carefully rotate it back to the place, before it falls inside.

3. After taking the globe out you can put the new globe in and fit the lens cover back. remember to check the light before fixing the lens cover.

image

In case your holder fell inside, then you can try few ways to get the holder back.

a. Use a magnet to try to get it out.

b. Carefully use a cable to get it.

c. Remove the entire map light assembly.

Since both a. and b. will have less percentage of success, I will explain how to remove the entire assembly. Since most Toyota’s do not use screws many components are held in place by clips. Mostly they will be plastic. So you need to know where to pull / push so you are not going to break any.

To remove the map light assembly you just need to pull hard to detach the 4 plastic clips see below. It may be really hard so some tools might help.

image

There is a small black connector which connects the lights to the vehicle’s electricity system. This can be removed by pressing the lock and pulling.

imageimage

Hope this helps you to stay out of trouble or save some hours while replacing your globes.

Monday, March 14, 2016

Port Willunga Beach – Port Willunga South Australia

While we were travelling with some of my friends, we did visit Port Willunga beach which I thought would be a good place to visit by any beach lover. It is about 45 km away from Adelaide city, which will take about 50 minutes if you are travelling by car.

There re two ways to get to the beach,

a. Go past the restaurant and head down to beach. – This is the quickest and shortest way to get to the beach. No stairs are there in this path so is wheel chair friendly.

b. Turn left before the restaurant – You need to walk for about 5 to 10 minutes depending on the pace you are walking to get to the beach and at the end there are some steps you need to go down. The short walk will reward you with the view of jetty pylons and the man made caves.

Facilities – There are toilet and shower facilities but no sitting areas, so portable table and chairs will be handy.

Port Willunga Beach

Port Willunga Beach

VIEW SLIDE SHOW
DOWNLOAD ALL

Map

Sunday, April 12, 2015

Changing Registry Key Values using PowerShell

As you may be knowing, you can use Windows PowerShell to change registry values. In this article I am going to do five things.

I have created few registry entries to use in this example as seen below. In real world you can use whatever entries in your registry. It is always advisable to backup your registry before changing it.

clip_image002[13]

1. Set a registry key value.

To set a value you need to use the “Set-ItemProperty” cmdlet as below.

Set-ItemProperty -Path "HKLM:\Software\Test\Live" -Name "TestValue2" –Value “TestData2”

Above command will put “TestData2” in the registry key “TestValue2” located in HKEY_LOCAL_MACHINE\Software\Test\Live.

 

2. Read a registry key value.

Reading from the registry can be done by using the cmdlet “Get-ItemProperty”.

Below command will get the value in the “TestValue1” key.

Get-ItemProperty -Path "HKLM:\Software\Test\Live" -Name "TestValue1"

 

3. Using variables in PowerShell.

Here I am going to read a registry key value and put it to another registry key. This can be done using a variable. First you need to read the value into a variable using the “Get-ItemProperty” cmdlet and that value can be saved using the “Set-ItemProperty” cmdlet.





  1. # Check for the existance of the registry key.
  2. IF (Get-ItemProperty -Path "HKLM:\Software\Test\Live" -Name "TestValue1" -ea 0)
  3. {
  4.     # Fetching the value from TestValue1.
  5.     $OldValue = Get-ItemProperty -Path "HKLM:\Software\Test\Live" -Name "TestValue1"
  6. }
  7. ELSE
  8. {
  9. # Inserting a blank, if the registry key is not present.
  10.     $OldValue = ""
  11. }
  12. # Printing the value in the variable.
  13. Write-Host $OldValue.TestValue1
  14. # Setting the value to TestValue2.
  15. Set-ItemProperty -Path "HKLM:\Software\Test\Live" -Name "TestValue2" -Value $OldValue.TestValue1




4. Working with registry keys with spaces.

In case your registry keys contain spaces, you need to use double quotes in your script as seen below.





  1. # Check for the existance of the registry key.
  2. IF (Get-ItemProperty -Path "HKLM:\Software\Test\Live" -Name "Test Value 1" -ea 0)
  3. {
  4.     # Fetching the value from Test Value 1.
  5.     $OldValue = Get-ItemProperty -Path "HKLM:\Software\Test\Live" -Name "Test Value 1"
  6. }
  7. ELSE
  8. {
  9.     # Inserting a blank, if the registry key is not present.
  10.     $OldValue = ""
  11. }
  12. # Printing the value in the variable.
  13. Write-Host $OldValue."Test Value 1"
  14. # Setting the value to Test Value 2.
  15. Set-ItemProperty -Path "HKLM:\Software\Test\Live" -Name "Test Value 2" -Value $OldValue."Test Value 1"




 

5. Saving PowerShell commands as scripts and running them.

Both above can be saved as a PowerShell script by saving it in a file with the extension ps1. For example I did save it as “ChangeReg.ps1” in my C drive inside the folder “new”. Then the script can be run by browsing to the folder and using the command “.\ChangeReg.ps1”.

clip_image002[10]

After the script is run my registry keys looked like this.

clip_image002[3]

In case you need to retrieve values from other registry hives (locations), following table may be helpful.

 

Registry Hive

Abbreviation

1. HKEY_CLASSES_ROOT HKCR
2. HKEY_CURRENT-USER HKCU
3. HKEY_LOCAL_MACHINE HKLM
4. HKEY_USERS HKU
5. HKEY_CURRENT_CONFIG HKCC

 

In case you need to read more on “Get-ItemProperty” and “Set-ItemProperty”, use the links to visit official documentation from Microsoft TechNet.

Sunday, February 01, 2015

Deleting Service Application Pools in SharePoint 2010

Recently while deleting a service application my system got unresponsive and while trying to create the service application again i constantly got the following error.

“An unhandled exception occurred in the user interface.Exception Information: An object of the type Microsoft.SharePoint.Administration.SPIisWebServiceApplicationPool named "SearchServiceApplication" already exists under the parent Microsoft.SharePoint.Administration.SPIisWebServiceSettings named "SharePoint Web Services". Rename your object or delete the existing object.”

This seems to be happening because one of the service application pools were not removed while deleting the service application. This can be removed by using PowerShell.

First load the SharePoint 2010 Management Shell by navigating to Start –> Microsoft SharePoint 2010 Products –> SharePoint 2010 Management Shell.

SPStartMenu

Then use the “Get-SPServiceApplicationPool” command to view all the existing service application pools.

Get-SPServiceApplicationPool

Now use the “Remove-SPServiceApplicationPool -Identity SearchServiceApplicationPoolName” command to remove the service application you want removed. If the service application pool’s name is having spaces remember to use double quotes.

Remove-SPServiceApplicationPool -Identity SearchServiceApplicationPool1

PowerShell will ask to confirm the deletion, after confirming the action, it will remove the service application pool.

Saturday, January 24, 2015

Magnetic Hill – Black Rock South Australia

If you like to experience nature’s amazing things it has to offer, one place you should not miss is the magnetic mountain in Black Rock South Australia. What is interesting in this place is that natural (magnetic) forces will take your car up the hill without the engine power.

Normally if you switch your car off, put it to neutral while at the middle of a hill and release the brake, your car will start moving downhill, but in this mountain your car will start moving up the hill. When I read about the place prior to my visit I thought it is a illusion in the eye. But after visiting the place I got amazed by seeing the way it moved my vehicle up the hill. To experience this I would encourage you to visit this mountain.

Magnetic Hill is located about 260 km from Adelaide and will take about 3 hour 30 minutes. The road to the magnetic hill is a unsealed road, but you can use a 2WD car to reach there.

Until you visit Magnetic Hill your self, enjoy me travelling up the hill while my engine is off.

Map


View Larger Map

Monday, January 05, 2015

Harben Vale Cherry Farm – Balhannah South Australia

If you like Cherries like me, another orchard you should visit is Harben Vale Cherries in Balhannah SA 5242. Located on Altmanns road, this orchard is about 100 hectares and produce Beef cattle, Potatoes and Cherries. This is a family owned farm which was with Altmann’s family for some 160 years.

They have started growing Cherries for about 10 to 12 years now and has a very open rural outlook rather than the steeper more traditional areas around Lenswood. You will find ample free parking in the property. This orchard has about 8 main Cherry varieties grown in 3 Hectares. Picking is easy since the trees are well trained and you can easily pick Cherries without using ladders.

Cherry season is a short 2 month period normally from December to January each year. Produce might even finish before the end of the season depending of the number of people visiting farms, So always call or check their web site before visiting. Current farm managers, Wayne and Julie Altman are very friendly people and they would be happy to help if you contact them.

When visiting, it is better to wear shoes in which you can easily walk on grass. Since it will be summer, hats are also recommended. Buckets will be provided to all visitor groups to pick Cherries into so you do not want to take anything for collecting what you pick. At the time I visited, they did not charge any admission fee and one kilo of Cherries were sold for $ 5. These prices can always change so I recommend you to check with the owners before picking since whatever you pick, you should buy. Since these Cherries are fresh you can keep them for few weeks without any problems, if you freeze you may even be able to keep them for few months. But I like to consume them within 2 – 3 weeks from picking.

Address : Altmanns Road, Balhannah 5242.
Phone : 04 3900 1962
EMail : wjaltman@adam.com.au
Web : http://harbenvale.com/

 

Map