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.namenetlogon 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.namenetlogonrestart_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:
Remember 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…..
And what about using Powershell scripts instead of VBS. Of course here I refer to Windows 7 (however PS2 is available for XP as a CTP)
I’ve tried using that script but the script won’t run. I keep getting the error that reads like this.
Line: 4
Char: 46
Error: Exspected ‘)’
Code: 800A03EE
Source: Microsoft VBScript compilation error.
Any suggestions on why this is happening?
Nice post 🙂
Thanks for this post, I was looking for information like this.
Thanks for a wonderful post, l ve been looking for such information, I will join jour rss feed now.
Getting the same error as Phillip
Replace the HTML-formatted elements with actual punctuation (& with ‘&’, < with ‘<' etc.)
Script looks like this:
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
Replace the HTML-formatted elements with actual punctuation (& = &, < = <)
Script should look like:
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
Replace the HTML-formatted punctuation in the VBS file with actual punctuation characters (& = & … < = < …. etc.)
Great post, works wonderfully!
Well…that didn’t work… try
& = &
etc.Changed the code function on my blog. Should be working again.
Hi there,
Love reading your blog, it has inspired me to start one of my own documenting bits and bobs to help others (and help me remember what i did).
In my effort to create a wordpress blog (new to this) i have tried installing some syntax highlighters (wp-codebox and syntaxhighlighter evolved) but am struggling to find out how to show the markup in the RSS feed like you have done it.
Your help would be greatly appreciated.
Ivan
Note: i commented on this post because it has code on it that shows up in IE feeds
Hi Ivan. Good to hear that I inspire other.
I use a plugin called WordPress Code Snippet. I belive this will do the magic for you. 🙂
Hi,
Was looking for a remote tool to restartshutdown pc’s and found your script – and it could be exactly what we need.
but we are having issues with getting it to run as intended. basically when running from AD nothing happens, and of course just executing it will not work as no pc name is parsed through on line 2.
Can you give some advice on what to look for? we are unfortunately not experienced with scripts…
Hi Anthony.
You could remove the first 2 lines in the script and change the 3.
DELETE THIS : Set wshArguments = WScript.Arguments
DELETE THIS: Set objComputer = GetObject(wshArguments(0))
CHANGE THIS: strComputer = objComputer.dNSHostname
TO: strComputer = “yourtestcomputer.domain.loc”
If you do this you could test the script from without ADUC. I most cases this is related to the firewall on the clients.
Hopes this helps you.
Thanks, this will really help alot.
This script reboots the local computer it is excuted from, not the target/remote computer you select from ADUC. What am I missing? I had to remove the // from this statement “Set OpSysSet = GetObject(“winmgmts:{(Shutdown)}//” in order to get the script to run without any errors.
I was running it incorrectly. Everything works as dvertised. Great post.
I tried using this but I got an error ‘Host unreachable. Maybe firewall is enabled’. I’ve turned off firewall on both the dc and the client pc. Is there some security setting I should be looking at?
Hi Tony. Not quite sure what error you get. This is a very old post, and it could be changes in Windows 7 in terms of impersionation rights in WMI.
My dc is Server 2008 R2 and my client is XP SP3. I ran the script locally on the client by setting the strComputer string and it rebooted. At the client I browse to \domainnamenetlogon and I can see the script. At the dc, the script does prompt me if I wish to run the script. However I still get that ‘Host unreachable error.”
In Windows 2008, adminContextMenu does not seem to exist. I look forward to your assistance.
Hi,
Great post, nice stuff, 10x 😉
The instructions here are relatively simple to follow, however, I have a problem with the script running. I am running a server 2003 domain, I have the schema modified to allow the context menu to display the reset option verified that the reset option calls up the vbs.
My issue is that I am getting an error on line 2 stating “Subscript out of range” with an error code of 800A0009.
I know that this is an old guide, but I believe it to be applicable to my situation.
Any help you can provide would be greatly appreciated!
I tried following these instructions on a windows server 2012, but the cn-409 does not seem to contain the admincontextmenu item. Any updates for server 2012?
Hi. Sorry. Have not tried this on Server 2012 yet.