Archive for August, 2010

Powershell script monitors particular service , tries to restart it and send email if it fails

August 27, 2010

This is a simple script which monitores w32time service. It can monitor avery service but i choose n that particular cause w32time. What is the genral idea:

1.We get an object service in variable $st. $comp is tha variable takes the hostname of the current computer.

2. If the service isn’t starter when the script runs it tries to start it three times implemented with help of a loop.

while (($st.Status -eq “Stopped”) -and ($i -lt 3)). You can get various methods and Properties from the object in variable $st with $st | get-member. One Property is the Status of the service $st.Status.

The Retries of the script is equalt to 3. $i -lt 3. It can be more then three. If for example $i -lt 8 the script will try 8 times to start the service before it fails.(if the service fails to start 8 times).

At last if the service is still in stopped state the script sends an email to notifyyou.

$st=Get-Service w32time
$comp=gc env:computername
while (($st.Status -eq “Stopped”) -and ($i -lt 3))
Start-Service w32time
$st=Get-Service w32time


if($st.Status -eq “Stopped”)
$smtpclient=New-Object system.Net.Mail.SmtpClient
$mailmessage=New-Object system.Net.Mail.MailMessage
$mailmessage.Subject=”Please Check the service “+$st.Name+” on machine name “+$comp+” !!!!!!!!!!!”
$mailmessage.Headers.Add(“message-id”, “<3BD50098E401463AA228377848493927-1>”)

So you can save the file with ps1 extension. For Example: str-service.ps1. After that you can create bat file with the following line Powershell -command “& {C:\Scripts\str-service.ps1 }”. The bat file can be add in Shedule tasks after that with appropriate schedule.


Hyper-v Server R2 Remote management in Workgroup

August 27, 2010

Here I tried to make clear guide how to manage Windows Server 2008 R2 with Hype-V role installed  or Hyper-V server from Windows 7 machine while both Win 7 and Hyper-V server are in workgroup scenario. You can find detailed post here:

What I do in this post is to make some basic guide with steps 1,2 etc.

1. Install Hyper-V Server R2. Nothing strange just as a usual Windows Server 2008 R2

2. Create two identical users on both machines with the same password. For example:

a. On Win 7 machine 

b. On Hyper-V Server issue a command

net user Deen * /add

In my cause I add Deen in local administrators group on both machines but isn’t necessary to be in local administrators group on the Server.

3. Issue a command in a Hyper-V server again

Netsh advfirewall firewall set rule group=”Windows Management Instrumentation (WMI)” new enable=yes

Net localgroup “Distributed COM Users” /add  HV-Server\Deen

(HV-Server is the name of HyperV Server machine)

4. Right click on the computer management console (top) and select connect to another computer.

Then connect to Hyper-V Server

You have to set two times the security. Once for Root\CIMV2 and then for the ROOT\virtualization.

For CIMv2 only the permission Enable Account is sufficient.

For \Root Virtualization enable account and remote enable is enough.

You can add the requested permissions in the whole root three but is less secure.

5. Work with AZman on Hyper-V Server.

Net use * \\HV-Server\c$

After that you have to load one xml file from the server.

Navigate to mapped drive  \ProgramData\Microsoft\Windows\Hyper-V directory on the Hyper-V server and add Deen to Role Assignments Administrator container.

6. Give the following permissions on the Win 7 machine.