It is interesting to know that you can easily add scripts to context menu in Active directory users and computers. I wanted to be able to right click on any machine and choose restart computer. And guess what? I did it……..
Here’s how you do it.
First things first. You have to have a script. I used a script I have used many times before to initiate computer restart and customized it. Here’s the script:
Set wshArguments = WScript.Arguments
Set objComputer = GetObject(wshArguments(0))
strComputer = objComputer.dNSHostname
intAnswer = Msgbox("Do you want to Restart: " & strComputer,vbYesNo, "Restart Computer")
If intAnswer = vbYes Then
Call Restart(strComputer)
Else
wscript.quit
End If
Function Restart(comp)
'On Error Resume Next
Err.Clear
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address = '" & comp & "'")
For Each objStatus in objPing
If objStatus.StatusCode = 0 Then
'Host was reachable
' Connect to computer
Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//" & comp & "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
' Actual shutdown
for each OpSys in OpSysSet
OpSys.Reboot()
next
Set OpSysSet = nothing
If Err <> 0 Then
WScript.Echo " Reboot failed." & Err.Number & " " & Err.Description
Else
WScript.Echo " Reboot initiated..."
End If
Else
WScript.Echo " Host unreachable. Maybe firewall is enabled"
'Host was unreachable
End If
Next
End Function
I saved this script in the netlogon folder of the DC: \\domain.name\netlogon and named the file restart_computer.vbs
Next we have to change the menu in ADUC. For this we need ADSI Edit. In 2008 server this pops up in the menu under administrative tools when you setup DC role on the server. In 2003 server you have to install 2003 support tools. Well then, start up ADSI Edit and go under the Configuration partition. Browse down to CN=Configuration,DC=Domain,DC=Local –> CN=Display Specifiers –> CN=409:

Then you edit the CN=Computer-Display by right-click and properties. Here you will find all the attributes.

We have to change the adminContextMenu (Mark it and Edit)


Here we will add the following line (already added in my picture) :
2,&Restart Computer,\\domain.name\netlogon\restart_computer.vbs
Note that 2 is next available.
When this is done click your way out of ADSI Edit and close and open Active directory users and computers. When you right click any computer object you will get this menu:

Remeber in order to get this to work you have to have a firewall rule that allows WMI on the client.
You can also use the same procedure to do other things. Like a ping script or maybe a script that finds out logged in user on the computer.
Good luck!
UPDATE:
There was a problem with the code function on my blog. Please use the view code function and use copy paste…..


